UNPKG

@ima/cli

Version:

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

83 lines (82 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handlerFactory = handlerFactory; exports.resolveCliPluginArgs = resolveCliPluginArgs; exports.sharedArgsFactory = sharedArgsFactory; 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', }, }; }