coryd.dev/cli/lib/runScript.js

44 lines
1.1 KiB
JavaScript

import fs from 'fs-extra';
import path from 'path';
import inquirer from 'inquirer';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.resolve(__dirname, '..', '..');
const packageJsonPath = path.join(rootDir, 'package.json');
export const runRootScript = async (scriptArg) => {
const pkg = await fs.readJson(packageJsonPath);
const scripts = pkg.scripts || {};
let script = scriptArg;
if (!script) {
const { selected } = await inquirer.prompt([
{
type: 'list',
name: 'selected',
message: 'Select a script to run:',
choices: Object.keys(scripts)
}
]);
script = selected;
}
if (!scripts[script]) {
console.error(`❌ Script "${script}" not found in package.json`);
process.exit(1);
}
try {
execSync(`npm run ${script}`, {
stdio: 'inherit',
cwd: rootDir
});
} catch (err) {
console.error(`❌ Failed to run script "${script}"`);
process.exit(1);
}
};