|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { SpawnOptions } from 'node:child_process'; |
|
|
import { spawn } from 'node:child_process'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isAtCommand = (query: string): boolean => |
|
|
|
|
|
query.startsWith('@') || /\s@/.test(query); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isSlashCommand = (query: string): boolean => { |
|
|
if (!query.startsWith('/')) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
if (query.startsWith('//')) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
if (query.startsWith('/*')) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
return true; |
|
|
}; |
|
|
|
|
|
|
|
|
export const copyToClipboard = async (text: string): Promise<void> => { |
|
|
const run = (cmd: string, args: string[], options?: SpawnOptions) => |
|
|
new Promise<void>((resolve, reject) => { |
|
|
const child = options ? spawn(cmd, args, options) : spawn(cmd, args); |
|
|
let stderr = ''; |
|
|
if (child.stderr) { |
|
|
child.stderr.on('data', (chunk) => (stderr += chunk.toString())); |
|
|
} |
|
|
child.on('error', reject); |
|
|
child.on('close', (code) => { |
|
|
if (code === 0) return resolve(); |
|
|
const errorMsg = stderr.trim(); |
|
|
reject( |
|
|
new Error( |
|
|
`'${cmd}' exited with code ${code}${errorMsg ? `: ${errorMsg}` : ''}`, |
|
|
), |
|
|
); |
|
|
}); |
|
|
if (child.stdin) { |
|
|
child.stdin.on('error', reject); |
|
|
child.stdin.write(text); |
|
|
child.stdin.end(); |
|
|
} else { |
|
|
reject(new Error('Child process has no stdin stream to write to.')); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const linuxOptions: SpawnOptions = { stdio: ['pipe', 'inherit', 'pipe'] }; |
|
|
|
|
|
switch (process.platform) { |
|
|
case 'win32': |
|
|
return run('clip', []); |
|
|
case 'darwin': |
|
|
return run('pbcopy', []); |
|
|
case 'linux': |
|
|
try { |
|
|
await run('xclip', ['-selection', 'clipboard'], linuxOptions); |
|
|
} catch (primaryError) { |
|
|
try { |
|
|
|
|
|
await run('xsel', ['--clipboard', '--input'], linuxOptions); |
|
|
} catch (fallbackError) { |
|
|
const xclipNotFound = |
|
|
primaryError instanceof Error && |
|
|
(primaryError as NodeJS.ErrnoException).code === 'ENOENT'; |
|
|
const xselNotFound = |
|
|
fallbackError instanceof Error && |
|
|
(fallbackError as NodeJS.ErrnoException).code === 'ENOENT'; |
|
|
if (xclipNotFound && xselNotFound) { |
|
|
throw new Error( |
|
|
'Please ensure xclip or xsel is installed and configured.', |
|
|
); |
|
|
} |
|
|
|
|
|
let primaryMsg = |
|
|
primaryError instanceof Error |
|
|
? primaryError.message |
|
|
: String(primaryError); |
|
|
if (xclipNotFound) { |
|
|
primaryMsg = `xclip not found`; |
|
|
} |
|
|
let fallbackMsg = |
|
|
fallbackError instanceof Error |
|
|
? fallbackError.message |
|
|
: String(fallbackError); |
|
|
if (xselNotFound) { |
|
|
fallbackMsg = `xsel not found`; |
|
|
} |
|
|
|
|
|
throw new Error( |
|
|
`All copy commands failed. "${primaryMsg}", "${fallbackMsg}". `, |
|
|
); |
|
|
} |
|
|
} |
|
|
return; |
|
|
default: |
|
|
throw new Error(`Unsupported platform: ${process.platform}`); |
|
|
} |
|
|
}; |
|
|
|
|
|
export const getUrlOpenCommand = (): string => { |
|
|
|
|
|
let openCmd: string; |
|
|
switch (process.platform) { |
|
|
case 'darwin': |
|
|
openCmd = 'open'; |
|
|
break; |
|
|
case 'win32': |
|
|
openCmd = 'start'; |
|
|
break; |
|
|
case 'linux': |
|
|
openCmd = 'xdg-open'; |
|
|
break; |
|
|
default: |
|
|
|
|
|
openCmd = 'xdg-open'; |
|
|
console.warn( |
|
|
`Unknown platform: ${process.platform}. Attempting to open URLs with: ${openCmd}.`, |
|
|
); |
|
|
break; |
|
|
} |
|
|
return openCmd; |
|
|
}; |
|
|
|