Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy support for Wayland via wl-copy #11964

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-suns-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add wayland (wl-copy) support to `astro info`
34 changes: 22 additions & 12 deletions packages/astro/src/cli/info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,34 @@ export async function printInfo({ flags }: InfoOptions) {
}

async function copyToClipboard(text: string) {
text = text.trim()
const system = platform();
let command = '';
if (system === 'darwin') {
command = 'pbcopy';
} else if (system === 'win32') {
command = 'clip';
} else {
try {
// 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;
// Unix: check if a supported command is installed
const unixCommands = [
['xclip', '-sel clipboard -l 1'],
['wl-copy', '"$0"']
]
for (const [unixCommand, args] of unixCommands) {
try {
const output = execSync(`which ${unixCommand}`, { encoding: 'utf8', stdio: 'pipe' });
if (output[0] !== '/') {
// Did not find a path. Skip!
continue;
}
command = `${unixCommand} ${args}`;
} catch {
// Failed to execute which. Skip!
continue;
}
command = 'xclip -sel clipboard -l 1';
} catch {
// Did not find xclip, bail out!
return;
}
// Did not find supported command. Bail out!
if (!command) return;
}

console.log();
Expand All @@ -86,8 +95,9 @@ async function copyToClipboard(text: string) {
if (!shouldCopy) return;

try {
execSync(command, {
input: text.trim(),
execSync(command.replaceAll('$0', text), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit worried with doing a plain replacement this way that could expose executing arbitrary commands, but it looks like we don't accept any user input that reaches here, so probably fine for now.

stdio: 'ignore',
input: text,
encoding: 'utf8',
});
} catch {
Expand Down
Loading