Home / File/ cli-clipboard.ts — astro Source File

cli-clipboard.ts — astro Source File

Architecture documentation for cli-clipboard.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 3 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  c4fcd5ce_4a96_8071_9d35_f40a368f2892["cli-clipboard.ts"]
  d3861967_b647_84d2_ff48_15013353bd56["../core/logger/core.js"]
  c4fcd5ce_4a96_8071_9d35_f40a368f2892 --> d3861967_b647_84d2_ff48_15013353bd56
  7db42c05_e873_0887_a39f_34451c181149["../definitions.js"]
  c4fcd5ce_4a96_8071_9d35_f40a368f2892 --> 7db42c05_e873_0887_a39f_34451c181149
  f458772b_5926_6ea0_4412_a27162311db0["../definitions.js"]
  c4fcd5ce_4a96_8071_9d35_f40a368f2892 --> f458772b_5926_6ea0_4412_a27162311db0
  style c4fcd5ce_4a96_8071_9d35_f40a368f2892 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Logger } from '../../../core/logger/core.js';
import type { CommandExecutor, OperatingSystemProvider } from '../../definitions.js';
import type { Clipboard, Prompt } from '../definitions.js';

async function getExecInputForPlatform({
	platform,
	commandExecutor,
}: {
	commandExecutor: CommandExecutor;
	platform: NodeJS.Platform;
}): Promise<[command: string, args?: Array<string>] | null> {
	if (platform === 'darwin') {
		return ['pbcopy'];
	}
	if (platform === 'win32') {
		return ['clip'];
	}
	// Unix: check if a supported command is installed
	const unixCommands: Array<[string, Array<string>]> = [
		['xclip', ['-selection', 'clipboard']],
		['wl-copy', []],
	];
	for (const [unixCommand, unixArgs] of unixCommands) {
		try {
			const { stdout } = await commandExecutor.execute('which', [unixCommand]);
			if (stdout.trim()) {
				return [unixCommand, unixArgs];
			}
		} catch {
			continue;
		}
	}
	return null;
}

export class CliClipboard implements Clipboard {
	readonly #operatingSystemProvider: OperatingSystemProvider;
	readonly #commandExecutor: CommandExecutor;
	readonly #logger: Logger;
	readonly #prompt: Prompt;

	constructor({
		operatingSystemProvider,
		commandExecutor,
		logger,
		prompt,
	}: {
		operatingSystemProvider: OperatingSystemProvider;
		commandExecutor: CommandExecutor;
		logger: Logger;
		prompt: Prompt;
	}) {
		this.#operatingSystemProvider = operatingSystemProvider;
		this.#commandExecutor = commandExecutor;
		this.#logger = logger;
		this.#prompt = prompt;
	}

	async copy(text: string): Promise<void> {
		text = text.trim();
		const platform = this.#operatingSystemProvider.name;
		const input = await getExecInputForPlatform({
			platform,
			commandExecutor: this.#commandExecutor,
		});
		if (!input) {
			this.#logger.warn('SKIP_FORMAT', 'Clipboard command not found!');
			this.#logger.info('SKIP_FORMAT', 'Please manually copy the text above.');
			return;
		}

		if (
			!(await this.#prompt.confirm({
				message: 'Copy to clipboard?',
				defaultValue: true,
			}))
		) {
			return;
		}

		try {
			const [command, args] = input;
			await this.#commandExecutor.execute(command, args, {
				input: text,
				stdio: ['pipe', 'ignore', 'ignore'],
			});
			this.#logger.info('SKIP_FORMAT', 'Copied to clipboard!');
		} catch {
			this.#logger.error(
				'SKIP_FORMAT',
				'Sorry, something went wrong! Please copy the text above manually.',
			);
		}
	}
}

Domain

Subdomains

Classes

Dependencies

  • ../core/logger/core.js
  • ../definitions.js
  • ../definitions.js

Frequently Asked Questions

What does cli-clipboard.ts do?
cli-clipboard.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in cli-clipboard.ts?
cli-clipboard.ts defines 1 function(s): getExecInputForPlatform.
What does cli-clipboard.ts depend on?
cli-clipboard.ts imports 3 module(s): ../core/logger/core.js, ../definitions.js, ../definitions.js.
Where is cli-clipboard.ts in the architecture?
cli-clipboard.ts is located at packages/astro/src/cli/info/infra/cli-clipboard.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/cli/info/infra).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free