UNPKG

@ima/cli

Version:

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

92 lines (91 loc) 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.closeCompiler = closeCompiler; exports.runCompiler = runCompiler; exports.watchCompiler = watchCompiler; exports.handleError = handleError; const logger_1 = require("@ima/dev-utils/logger"); const formatStats_1 = require("./formatStats"); const ProgressPlugin_1 = require("../webpack/plugins/ProgressPlugin"); const utils_1 = require("../webpack/utils/utils"); /** * Cli Error handler. * * @param {Error | unknown} err * @returns {void} */ function handleError(error) { logger_1.logger.error(error ?? 'Unexpected error occurred, closing the compiler...'); } /** * Promise based helper to close running webpack compiler. * * @param {MultiCompiler} compiler Compiler instance to close * @returns {Promise<Error | void>} Any unexpected rejection error or nothing. */ async function closeCompiler(compiler) { return new Promise((resolve, reject) => compiler.close(closeError => (closeError ? reject(closeError) : resolve()))); } /** * Runs webpack compiler with given configuration. * * @param {MultiCompiler} compiler Webpack compiler instance * @param {ImaCliArgs} args Cli and build args. * @param {ImaConfig} imaConfig loaded ima.config.js. * @returns {Promise<Error | MultiStats | undefined>} Stats or error. */ async function runCompiler(compiler, args, imaConfig) { return new Promise((resolve, reject) => { compiler.run((error, stats) => closeCompiler(compiler).then(async () => { // Stop CLI progress bar (0, ProgressPlugin_1.getProgress)().stop(); // Reject when there are any errors if (error || stats?.hasErrors()) { if (stats) { await (0, formatStats_1.formatWebpackErrors)(stats, args); } else if (error) { logger_1.logger.error(error); } return reject(error); } // Format stats after plugin done callback (0, formatStats_1.formatStats)(stats, args); // Print warnings (0, formatStats_1.formatWebpackWarnings)(stats, args); // Run postProcess hook await (0, utils_1.runImaPluginsHook)(args, imaConfig, 'postProcess'); return resolve(compiler); })); }); } /** * Runs webpack watch compiler with given configuration. * * @param {MultiCompiler} compiler Webpack compiler instance * @param {ImaCliArgs} args Cli and build args. * @param {ImaConfig} imaConfig loaded ima.config.js. * @param {Configuration['watchOptions']={}} watchOptions * Additional watch options. * @returns {Promise<MultiCompiler>} compiler instance. */ async function watchCompiler(compiler, args, imaConfig) { let firstRun = true; return new Promise(resolve => { compiler.watch(imaConfig.watchOptions, async (error, stats) => { // Stop CLI progress bar (0, ProgressPlugin_1.getProgress)().stop(); // Format stats after plugin done callback if (firstRun) { (0, formatStats_1.formatStats)(stats, args); } // Print warnings (0, formatStats_1.formatWebpackWarnings)(stats, args); // Print errors await (0, formatStats_1.formatWebpackErrors)(stats, args); // Update first run flag firstRun = false; return resolve(compiler); }); }); }