Skip to content

Commit 097564a

Browse files
fix: cli-executer supplies args further up (#1904)
1 parent 7e90f11 commit 097564a

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

packages/webpack-cli/__tests__/cli-executer.test.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
jest.mock('../lib/bootstrap');
21
jest.mock('enquirer');
32

4-
const runCLI = require('../lib/bootstrap');
5-
runCLI.mockImplementation(() => {});
6-
73
describe('CLI Executer', () => {
84
let cliExecuter = null;
95
let multiCalls = 0;
@@ -45,11 +41,8 @@ describe('CLI Executer', () => {
4541
});
4642

4743
it('runs enquirer options then runs webpack', async () => {
48-
await cliExecuter();
49-
50-
// ensure that the webpack runCLI is called
51-
expect(runCLI.mock.calls.length).toEqual(1);
52-
expect(runCLI.mock.calls[0][0]).toEqual(['--config', 'test1', '--entry', 'test2', '--progress']);
44+
const args = await cliExecuter();
45+
expect(args.length).toBe(5);
5346

5447
// check that webpack options are actually being displayed that
5548
// the user can select from

packages/webpack-cli/lib/bootstrap.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ process.title = 'webpack-cli';
1313
// Create a new instance of the CLI object
1414
const cli = new WebpackCLI();
1515

16-
async function runCLI(cliArgs) {
16+
const parseArgs = (args) => argParser(core, args, true, process.title);
17+
18+
const runCLI = async (cliArgs) => {
1719
let args;
1820

1921
const commandIsUsed = isCommandUsed(cliArgs);
20-
const parsedArgs = argParser(core, cliArgs, true, process.title);
22+
const parsedArgs = parseArgs(cliArgs);
2123
if (parsedArgs.unknownArgs.includes('help') || parsedArgs.opts.help) {
2224
options.enabled = !cliArgs.includes('--no-color');
2325
helpRunner(cliArgs);
@@ -56,7 +58,9 @@ async function runCLI(cliArgs) {
5658
parsedArgs.unknownArgs.forEach((unknown) => {
5759
logger.warn(`Unknown argument: ${unknown}`);
5860
});
59-
await cliExecuter();
61+
const args = await cliExecuter();
62+
const { opts } = parseArgs(args);
63+
await cli.run(opts, core);
6064
return;
6165
}
6266
const parsedArgsOpts = parsedArgs.opts;
@@ -102,6 +106,6 @@ async function runCLI(cliArgs) {
102106
return;
103107
}
104108
}
105-
}
109+
};
106110

107111
module.exports = runCLI;

packages/webpack-cli/lib/utils/arg-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { defaultCommands } = require('./commands');
1313
* @param {boolean} argsOnly false if all of process.argv has been provided, true if
1414
* args is only a subset of process.argv that removes the first couple elements
1515
*/
16-
function argParser(options, args, argsOnly = false, name = '') {
16+
const argParser = (options, args, argsOnly = false, name = '') => {
1717
const parser = new commander.Command();
1818
// Set parser name
1919
parser.name(name);
@@ -155,6 +155,6 @@ function argParser(options, args, argsOnly = false, name = '') {
155155
unknownArgs,
156156
opts,
157157
};
158-
}
158+
};
159159

160160
module.exports = argParser;

packages/webpack-cli/lib/utils/cli-executer.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ const { MultiSelect, Input } = require('enquirer');
22
const { cyan } = require('colorette');
33
const logger = require('./logger');
44
const cliArgs = require('./cli-flags').core;
5-
const runCLI = require('../bootstrap');
65

7-
async function prompter() {
6+
const prompter = async () => {
87
const args = [];
98

109
const typePrompt = new MultiSelect({
@@ -49,16 +48,17 @@ async function prompter() {
4948
}
5049

5150
return [...args, ...boolArgs];
52-
}
51+
};
5352

54-
async function run() {
53+
const run = async () => {
5554
try {
5655
const args = await prompter();
5756
logger.info('\nExecuting CLI\n');
58-
await runCLI(args);
57+
return args;
5958
} catch (err) {
6059
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible options.`);
60+
process.exit(2);
6161
}
62-
}
62+
};
6363

6464
module.exports = run;

0 commit comments

Comments
 (0)