Skip to content

Commit

Permalink
fix: cli-executer supplies args further up (#1904)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Oct 10, 2020
1 parent 7e90f11 commit 097564a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
11 changes: 2 additions & 9 deletions 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;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions packages/webpack-cli/lib/bootstrap.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -102,6 +106,6 @@ async function runCLI(cliArgs) {
return;
}
}
}
};

module.exports = runCLI;
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/utils/arg-parser.js
Expand Up @@ -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);
Expand Down Expand Up @@ -155,6 +155,6 @@ function argParser(options, args, argsOnly = false, name = '') {
unknownArgs,
opts,
};
}
};

module.exports = argParser;
12 changes: 6 additions & 6 deletions packages/webpack-cli/lib/utils/cli-executer.js
Expand Up @@ -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({
Expand Down Expand Up @@ -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;

0 comments on commit 097564a

Please sign in to comment.