Skip to content

Commit

Permalink
Improve astro info compatability (#8730)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
natemoo-re and lilnasy committed Oct 3, 2023
1 parent f277ba8 commit 357270f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-fishes-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improve `astro info` copy to clipboard compatability
22 changes: 17 additions & 5 deletions packages/astro/src/cli/info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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(
Expand Down

0 comments on commit 357270f

Please sign in to comment.