| 31 | // Copies a string snippet to the clipboard for different platforms |
| 32 | export const copyToClipboard = async (text: string): Promise<void> => { |
| 33 | const run = (cmd: string, args: string[]) => |
| 34 | new Promise<void>((resolve, reject) => { |
| 35 | const child = spawn(cmd, args); |
| 36 | let stderr = ''; |
| 37 | child.stderr.on('data', (chunk) => (stderr += chunk.toString())); |
| 38 | child.on('error', reject); |
| 39 | child.on('close', (code) => { |
| 40 | if (code === 0) return resolve(); |
| 41 | const errorMsg = stderr.trim(); |
| 42 | reject( |
| 43 | new Error( |
| 44 | `'${cmd}' exited with code ${code}${errorMsg ? `: ${errorMsg}` : ''}`, |
| 45 | ), |
| 46 | ); |
| 47 | }); |
| 48 | child.stdin.on('error', reject); |
| 49 | child.stdin.write(text); |
| 50 | child.stdin.end(); |
| 51 | }); |
| 52 | |
| 53 | switch (process.platform) { |
| 54 | case 'win32': |