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(