UNPKG

@ima/cli

Version:

IMA.js CLI tool to build, develop and work with IMA.js applications.

104 lines (103 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handlerFactory = handlerFactory; exports.resolveCliPluginArgs = resolveCliPluginArgs; exports.sharedArgsFactory = sharedArgsFactory; exports.runCommand = runCommand; const child_process_1 = require("child_process"); const utils_1 = require("../webpack/utils"); /** * Initializes cli script handler function, which takes cli arguments, * parses them and defines defaults. Should be used to initialize any * cli command script, since it takes care of parsing mandatory arguments. * * @param {HandlerFn} handlerFn Cli script command handler. * @returns {void} */ function handlerFactory(handlerFn) { return async (yargs) => { const [command] = yargs._ || []; // Force development env for dev process.env.NODE_ENV = command === 'dev' ? 'development' : (process.env.NODE_ENV ?? 'production'); return await handlerFn({ ...yargs, rootDir: process.cwd(), command: command.toString(), environment: process.env.NODE_ENV, }); }; } /** * Resolves additional cliArgs that can be provided with custom cli plugins * defined in the ima.config.js. * * @param {ImaCliCommand} Current command for which args are loaded. * @returns {CommandBuilder} Yargs commands object. */ function resolveCliPluginArgs(command) { const imaConfig = (0, utils_1.requireImaConfig)(); if (!imaConfig || !Array.isArray(imaConfig?.plugins)) { return {}; } return imaConfig.plugins .filter(plugin => plugin?.cliArgs && Object.keys(plugin.cliArgs).length !== 0) .reduce((acc, cur) => { if (cur?.cliArgs && cur.cliArgs[command]) { return { ...acc, ...cur.cliArgs[command], }; } return acc; }, {}); } /** * Initializes shared args with their default values based * on the current CLI command. * * @param {ImaCliCommand} command Current CLI command identifier. * @returns {CommandBuilder} Object with shared args. */ function sharedArgsFactory(command) { return { clean: { desc: 'Clean build folder before building the application', type: 'boolean', default: true, }, clearCache: { desc: 'Deletes node_modules/.cache directory to invalidate loaders cache', type: 'boolean', }, verbose: { desc: 'Use default webpack CLI output instead of custom one', type: 'boolean', }, ignoreWarnings: { desc: 'Webpack will no longer print warnings during compilation', type: 'boolean', }, }; } /** * Runs a command and waits for it to finish. */ function runCommand(command, args, env = {}) { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, args, { stdio: 'inherit', env: { ...process.env, ...env }, }); child.on('close', code => { if (code === 0) { resolve(); } else { reject(new Error(`Command failed with code ${code}`)); } }); }); }