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>
This commit is contained in:
Nate Moore 2023-10-03 15:57:31 -05:00 committed by Martin Trapp
parent 49cbbf8d23
commit 9c7cd7c9f6
2 changed files with 22 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Improve `astro info` copy to clipboard compatability

View file

@ -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(