Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cli-executer supplies args further up #1904

Merged
merged 3 commits into from Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webpack/cli-team we need tests this, let's do it in separate PR, because we need do release today

}
}
};

module.exports = run;