@expressots/cli
Version:
Expressots CLI - modern, fast, lightweight nodejs web framework (@cli)
108 lines (107 loc) • 3.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scriptsForm = void 0;
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const inquirer_1 = __importDefault(require("inquirer"));
const path_1 = __importDefault(require("path"));
const cli_ui_1 = require("../utils/cli-ui");
const cwd = process.cwd();
const packageJsonPath = path_1.default.join(cwd, "package.json");
function readPackageJson() {
try {
return JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf8"));
}
catch (e) {
(0, cli_ui_1.printError)(`Error reading package.json`, "scripts-command");
process.exit(1);
}
}
function listScripts(packageJson) {
const scripts = packageJson.scripts || {};
if (Object.keys(scripts).length === 0) {
(0, cli_ui_1.printWarning)("No scripts found in package.json", "scripts-command");
process.exit(0);
}
return scripts;
}
async function promptUserToSelectScripts(scripts) {
const scriptChoices = Object.keys(scripts).map((key) => ({
name: `${key}`,
value: key,
}));
let selectionOrder = [];
const answers = await inquirer_1.default.prompt([
{
type: "checkbox",
name: "selectedScripts",
message: "Select scripts to run:",
choices: scriptChoices,
filter: (selected) => {
selectionOrder = selected;
return selected;
},
loop: false,
},
]);
return answers;
}
function executeScripts(scripts, selectedScripts, runner) {
selectedScripts.forEach((script) => {
console.log(`Running ${script}...`);
try {
const command = `${runner} run ${script}`;
const options = {
stdio: "inherit",
env: { ...process.env },
};
(0, child_process_1.execSync)(command, options);
}
catch (e) {
(0, cli_ui_1.printWarning)(`Command ${script} cancelled or failed - ${e}`, "scripts-command");
}
});
}
process.stdin.on("keypress", (ch, key) => {
if (key && key.name === "escape") {
console.log("Exiting...");
process.exit(0);
}
});
const scriptsForm = async (scriptArgs = []) => {
const packageJson = readPackageJson();
const scripts = listScripts(packageJson);
const runner = fs_1.default.existsSync("package-lock.json")
? "npm"
: fs_1.default.existsSync("yarn.lock")
? "yarn"
: fs_1.default.existsSync("pnpm-lock.yaml")
? "pnpm"
: null;
if (!runner) {
(0, cli_ui_1.printError)("No package manager found! Please ensure you have npm, yarn, or pnpm installed.", "scripts-command");
process.exit(1);
}
if (scriptArgs.length > 0) {
const validScripts = scriptArgs.filter((script) => scripts[script]);
const invalidScripts = scriptArgs.filter((script) => !scripts[script]);
if (invalidScripts.length > 0) {
console.error(`Scripts not found in package.json: ${invalidScripts.join(", ")}`);
}
if (validScripts.length > 0) {
executeScripts(scripts, validScripts, runner);
}
}
else {
const { selectedScripts } = await promptUserToSelectScripts(scripts);
if (selectedScripts.length === 0) {
console.log("No scripts selected.");
process.exit(0);
}
executeScripts(scripts, selectedScripts, runner);
}
};
exports.scriptsForm = scriptsForm;