From 357270f2a3d0bf2aa634ba7e52e9d17618eff4a7 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 3 Oct 2023 15:57:31 -0500 Subject: [PATCH] Improve `astro info` compatability (#8730) * Improve `astro info` compatability * Update packages/astro/src/cli/info/index.ts Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> * chore: add changeset * feat(info): add copy to clipboard support on Unix machines with xclip installed --------- Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --- .changeset/witty-fishes-heal.md | 5 +++++ packages/astro/src/cli/info/index.ts | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 .changeset/witty-fishes-heal.md diff --git a/.changeset/witty-fishes-heal.md b/.changeset/witty-fishes-heal.md new file mode 100644 index 000000000..0fb1d79f7 --- /dev/null +++ b/.changeset/witty-fishes-heal.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Improve `astro info` copy to clipboard compatability diff --git a/packages/astro/src/cli/info/index.ts b/packages/astro/src/cli/info/index.ts index 2ee9ffd0d..518817e06 100644 --- a/packages/astro/src/cli/info/index.ts +++ b/packages/astro/src/cli/info/index.ts @@ -41,10 +41,22 @@ export async function printInfo({ flags }: InfoOptions) { await copyToClipboard(output.trim()); } -const SUPPORTED_SYSTEM = new Set(['darwin', 'win32']); async function copyToClipboard(text: string) { const system = platform(); - if (!SUPPORTED_SYSTEM.has(system)) return; + let command = ''; + if (system === 'darwin') { + command = 'pbcopy'; + } else if (system === 'win32') { + command = 'clip'; + } else { + // Unix: check if `xclip` is installed + const output = execSync('which xclip', { encoding: 'utf8' }); + if (output[0] !== '/') { + // Did not find a path for xclip, bail out! + return; + } + command = 'xclip -sel clipboard -l 1'; + } console.log(); const { shouldCopy } = await prompts({ @@ -54,11 +66,11 @@ async function copyToClipboard(text: string) { initial: true, }); if (!shouldCopy) return; - const command = system === 'darwin' ? 'pbcopy' : 'clip'; + try { - execSync(`echo ${JSON.stringify(text.trim())} | ${command}`, { + execSync(command, { + input: text.trim(), encoding: 'utf8', - stdio: 'ignore', }); } catch (e) { console.error(