UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

148 lines (147 loc) 6.43 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionheroCLIRunner = void 0; const path = require("path"); const fs = require("fs"); const glob = require("glob"); const commander_1 = require("commander"); const typescript_1 = require("../classes/process/typescript"); const projectRoot_1 = require("../classes/process/projectRoot"); const ensureNoTsHeaderFiles_1 = require("../modules/utils/ensureNoTsHeaderFiles"); var ActionheroCLIRunner; (function (ActionheroCLIRunner) { async function run() { commander_1.program.storeOptionsAsProperties(false); commander_1.program.version(getVersion()); let pathsLoaded = []; try { const { config } = await Promise.resolve().then(() => require("../index")); // this project for (const i in config.general.paths.cli) { await loadDirectory(path.join(config.general.paths.cli[i]), pathsLoaded); } // plugins for (const pluginName in config.plugins) { if (config.plugins[pluginName].cli !== false) { // old plugins await loadDirectory(path.join(config.plugins[pluginName].path, "bin"), pathsLoaded); // new plugins await loadDirectory(path.join(config.plugins[pluginName].path, "dist", "bin"), pathsLoaded); } } // core if (config.general.cliIncludeInternal !== false) { await loadDirectory(__dirname, pathsLoaded); } } catch (e) { // we are trying to build a new project, only load the generate command await loadDirectory(path.join(__dirname), pathsLoaded, "generate"); } commander_1.program.parse(process.argv); } ActionheroCLIRunner.run = run; // --- Utils --- // async function loadDirectory(dir, pathsLoaded, match = "*") { if (!fs.existsSync(dir)) return; const realpath = fs.realpathSync(dir); if (pathsLoaded.includes(realpath)) return; pathsLoaded.push(realpath); const matcher = `${realpath}/**/+(${typescript_1.typescript ? `${match}.js|*.ts` : `${match}.js`})`; const files = ensureNoTsHeaderFiles_1.ensureNoTsHeaderFiles(glob.sync(matcher)); for (const i in files) { const collection = await Promise.resolve().then(() => require(files[i])); for (const j in collection) { const command = collection[j]; convertCLIToCommanderAction(command); } } } ActionheroCLIRunner.loadDirectory = loadDirectory; async function convertCLIToCommanderAction(cli) { var _a; if (Object.getPrototypeOf(((_a = cli === null || cli === void 0 ? void 0 : cli.prototype) === null || _a === void 0 ? void 0 : _a.constructor) || {}).name !== "CLI") { return; } const instance = new cli(); const command = commander_1.program .command(instance.name) .description(instance.description) .action(async (_arg1, _arg2, _arg3, _arg4, _arg5) => { await runCommand(instance, _arg1, _arg2, _arg3, _arg4, _arg5); }) .on("--help", () => { if (instance.example) { console.log(""); console.log("Example: \r\n" + " " + instance.example); } if (typeof instance.help === "function") instance.help(); }); for (const key in instance.inputs) { const input = instance.inputs[key]; const separators = input.required ? ["<", ">"] : ["[", "]"]; const methodName = input.required ? "requiredOption" : "option"; command[methodName](`${input.letter ? `-${input.letter}, ` : ""}--${key} ${input.flag ? "" : `${separators[0]}${input.placeholder || key}${input.variadic ? "..." : ""}${separators[1]}`}`, input.description, input.default); } } ActionheroCLIRunner.convertCLIToCommanderAction = convertCLIToCommanderAction; async function runCommand(instance, _arg1, _arg2, _arg3, _arg4, _arg5) { let toStop = false; let _arguments = []; let params = {}; [_arg1, _arg2, _arg3, _arg4, _arg5].forEach((arg) => { if (typeof (arg === null || arg === void 0 ? void 0 : arg.opts) === "function") { params = arg.opts(); } else if (arg !== null && arg !== undefined && typeof arg !== "object") { _arguments.push(arg); } }); params["_arguments"] = _arguments; if (instance.initialize === false && instance.start === false) { toStop = await instance.run({ params }); } else { try { const { Process } = await Promise.resolve().then(() => require("../index")); const actionHeroProcess = new Process(); if (instance.initialize) await actionHeroProcess.initialize(); if (instance.start) await actionHeroProcess.start(); toStop = await instance.run({ params }); } catch (error) { console.error(error.toString()); process.exit(1); } } if (toStop || toStop === null || toStop === undefined) { setTimeout(process.exit, 500, 0); } } ActionheroCLIRunner.runCommand = runCommand; function readPackageJSON(file) { return JSON.parse(fs.readFileSync(file).toString()); } ActionheroCLIRunner.readPackageJSON = readPackageJSON; function getVersion() { const parentPackageJSON = path.join(projectRoot_1.projectRoot, "package.json"); if (fs.existsSync(parentPackageJSON)) { const pkg = readPackageJSON(parentPackageJSON); return pkg.version; } else { const pkg = readPackageJSON(path.join(__dirname, "..", "..", "package.json")); return pkg.version; } } ActionheroCLIRunner.getVersion = getVersion; })(ActionheroCLIRunner = exports.ActionheroCLIRunner || (exports.ActionheroCLIRunner = {})); ActionheroCLIRunner.run();