From 097564a851b36b63e0a6bf88144997ef65aa057a Mon Sep 17 00:00:00 2001 From: James George Date: Sat, 10 Oct 2020 20:00:52 +0530 Subject: [PATCH] fix: cli-executer supplies args further up (#1904) --- packages/webpack-cli/__tests__/cli-executer.test.js | 11 ++--------- packages/webpack-cli/lib/bootstrap.js | 12 ++++++++---- packages/webpack-cli/lib/utils/arg-parser.js | 4 ++-- packages/webpack-cli/lib/utils/cli-executer.js | 12 ++++++------ 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/webpack-cli/__tests__/cli-executer.test.js b/packages/webpack-cli/__tests__/cli-executer.test.js index 74bdde7c346..f64847f5084 100644 --- a/packages/webpack-cli/__tests__/cli-executer.test.js +++ b/packages/webpack-cli/__tests__/cli-executer.test.js @@ -1,9 +1,5 @@ -jest.mock('../lib/bootstrap'); jest.mock('enquirer'); -const runCLI = require('../lib/bootstrap'); -runCLI.mockImplementation(() => {}); - describe('CLI Executer', () => { let cliExecuter = null; let multiCalls = 0; @@ -45,11 +41,8 @@ describe('CLI Executer', () => { }); it('runs enquirer options then runs webpack', async () => { - await cliExecuter(); - - // ensure that the webpack runCLI is called - expect(runCLI.mock.calls.length).toEqual(1); - expect(runCLI.mock.calls[0][0]).toEqual(['--config', 'test1', '--entry', 'test2', '--progress']); + const args = await cliExecuter(); + expect(args.length).toBe(5); // check that webpack options are actually being displayed that // the user can select from diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 65a1b8809d9..5428c5dd74b 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -13,11 +13,13 @@ process.title = 'webpack-cli'; // Create a new instance of the CLI object const cli = new WebpackCLI(); -async function runCLI(cliArgs) { +const parseArgs = (args) => argParser(core, args, true, process.title); + +const runCLI = async (cliArgs) => { let args; const commandIsUsed = isCommandUsed(cliArgs); - const parsedArgs = argParser(core, cliArgs, true, process.title); + const parsedArgs = parseArgs(cliArgs); if (parsedArgs.unknownArgs.includes('help') || parsedArgs.opts.help) { options.enabled = !cliArgs.includes('--no-color'); helpRunner(cliArgs); @@ -56,7 +58,9 @@ async function runCLI(cliArgs) { parsedArgs.unknownArgs.forEach((unknown) => { logger.warn(`Unknown argument: ${unknown}`); }); - await cliExecuter(); + const args = await cliExecuter(); + const { opts } = parseArgs(args); + await cli.run(opts, core); return; } const parsedArgsOpts = parsedArgs.opts; @@ -102,6 +106,6 @@ async function runCLI(cliArgs) { return; } } -} +}; module.exports = runCLI; diff --git a/packages/webpack-cli/lib/utils/arg-parser.js b/packages/webpack-cli/lib/utils/arg-parser.js index ef379d735cc..c72ad1bd7d4 100644 --- a/packages/webpack-cli/lib/utils/arg-parser.js +++ b/packages/webpack-cli/lib/utils/arg-parser.js @@ -13,7 +13,7 @@ const { defaultCommands } = require('./commands'); * @param {boolean} argsOnly false if all of process.argv has been provided, true if * args is only a subset of process.argv that removes the first couple elements */ -function argParser(options, args, argsOnly = false, name = '') { +const argParser = (options, args, argsOnly = false, name = '') => { const parser = new commander.Command(); // Set parser name parser.name(name); @@ -155,6 +155,6 @@ function argParser(options, args, argsOnly = false, name = '') { unknownArgs, opts, }; -} +}; module.exports = argParser; diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index 6a8af1841c5..276981f1285 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -2,9 +2,8 @@ const { MultiSelect, Input } = require('enquirer'); const { cyan } = require('colorette'); const logger = require('./logger'); const cliArgs = require('./cli-flags').core; -const runCLI = require('../bootstrap'); -async function prompter() { +const prompter = async () => { const args = []; const typePrompt = new MultiSelect({ @@ -49,16 +48,17 @@ async function prompter() { } return [...args, ...boolArgs]; -} +}; -async function run() { +const run = async () => { try { const args = await prompter(); logger.info('\nExecuting CLI\n'); - await runCLI(args); + return args; } catch (err) { logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible options.`); + process.exit(2); } -} +}; module.exports = run;