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:
parent
f277ba8b70
commit
357270f2a3
2 changed files with 22 additions and 5 deletions
5
.changeset/witty-fishes-heal.md
Normal file
5
.changeset/witty-fishes-heal.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve `astro info` copy to clipboard compatability
|
|
@ -41,10 +41,22 @@ export async function printInfo({ flags }: InfoOptions) {
|
||||||
await copyToClipboard(output.trim());
|
await copyToClipboard(output.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
const SUPPORTED_SYSTEM = new Set(['darwin', 'win32']);
|
|
||||||
async function copyToClipboard(text: string) {
|
async function copyToClipboard(text: string) {
|
||||||
const system = platform();
|
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();
|
console.log();
|
||||||
const { shouldCopy } = await prompts({
|
const { shouldCopy } = await prompts({
|
||||||
|
@ -54,11 +66,11 @@ async function copyToClipboard(text: string) {
|
||||||
initial: true,
|
initial: true,
|
||||||
});
|
});
|
||||||
if (!shouldCopy) return;
|
if (!shouldCopy) return;
|
||||||
const command = system === 'darwin' ? 'pbcopy' : 'clip';
|
|
||||||
try {
|
try {
|
||||||
execSync(`echo ${JSON.stringify(text.trim())} | ${command}`, {
|
execSync(command, {
|
||||||
|
input: text.trim(),
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
stdio: 'ignore',
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(
|
console.error(
|
||||||
|
|
Loading…
Reference in a new issue