foalts2-cli
Version:
CLI tool for FoalTS
45 lines (44 loc) • 1.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runScript = void 0;
// std
const fs_1 = require("fs");
const path_1 = require("path");
// 3p
const Ajv = require("ajv");
// FoalTS
const get_command_line_arguments_util_1 = require("./get-command-line-arguments.util");
async function runScript({ name }, argv, log = console.log) {
if (!fs_1.existsSync(`build/scripts/${name}.js`)) {
if (fs_1.existsSync(`src/scripts/${name}.ts`)) {
log(`The script "${name}" does not exist in build/scripts/. But it exists in src/scripts/.`
+ ' Please build your script by running the command "npm run build" or using "npm run develop".');
}
else {
log(`The script "${name}" does not exist. You can create it by running the command "foal g script ${name}".`);
}
return;
}
const { main, schema } = require(path_1.join(process.cwd(), `./build/scripts/${name}`));
if (!main) {
log(`Error: No "main" function was found in build/scripts/${name}.js.`);
return;
}
const args = get_command_line_arguments_util_1.getCommandLineArguments(argv);
if (schema) {
const ajv = new Ajv({ useDefaults: true });
if (!ajv.validate(schema, args)) {
ajv.errors.forEach(err => {
log(`Error: The command line arguments ${err.message}.`);
});
return;
}
}
try {
await main(args);
}
catch (error) {
log(error);
}
}
exports.runScript = runScript;