command.ts 459 B

123456789101112131415161718
  1. import cp from 'node:child_process';
  2. import path from 'node:path';
  3. import url from 'node:url';
  4. const projectRoot = url.fileURLToPath(new URL('../', import.meta.url));
  5. export function command(cmd: string, dir = '') {
  6. return cp.execSync(cmd, { cwd: path.resolve(projectRoot, dir), encoding: 'utf-8' }).trim();
  7. }
  8. export function tryCommand(cmd: string, dir = '', fallback = '') {
  9. try {
  10. return command(cmd, dir);
  11. } catch {
  12. return fallback;
  13. }
  14. }