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

[WIP] chore: migrate to commander #1347

Closed
wants to merge 15 commits into from
Closed
29 changes: 14 additions & 15 deletions packages/webpack-cli/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WebpackCLI = require('./webpack-cli');
const { core, commands } = require('./utils/cli-flags');
const logger = require('./utils/logger');
const cliExecuter = require('./utils/cli-executer');

const argParser = require('./utils/arg-parser');
require('./utils/process-log');

process.title = 'webpack-cli';
Expand All @@ -21,7 +21,7 @@ const isCommandUsed = commands =>
});

const resolveNegatedArgs = args => {
args._unknown.forEach((arg, idx) => {
args.forEach((arg, idx) => {
if (arg.includes('--') || arg.includes('--no')) {
const argPair = arg.split('=');
const optName = arg.includes('--no') ? argPair[0].slice(5) : argPair[0].slice(2);
Expand All @@ -36,23 +36,23 @@ const resolveNegatedArgs = args => {
if (cliFlag) {
args[cliFlag.group][optName] = argValue;
args._all[optName] = argValue;
args._unknown[idx] = null;
args.args[idx] = null;
}
}
});
};

async function runCLI(cli, commandIsUsed) {
let args;
const helpFlagExists = isFlagPresent(process.argv, 'help');
const versionFlagExists = isFlagPresent(process.argv, 'version');
const parsedArgs = argParser('webpack', core, process.argv, cli.runHelp, cli.runVersion);

if (helpFlagExists) {
if (parsedArgs.args.includes('help')) {
cli.runHelp(process.argv);
return;
} else if (versionFlagExists) {
process.exit(0);
}

if (parsedArgs.args.includes('version')) {
cli.runVersion();
return;
process.exit(0);
}

if (commandIsUsed) {
Expand All @@ -61,18 +61,17 @@ async function runCLI(cli, commandIsUsed) {
return await cli.runCommand(commandIsUsed, ...args);
} else {
try {
args = cli.commandLineArgs(core, { stopAtFirstUnknown: false, partial: true });
if (args._unknown) {
resolveNegatedArgs(args);
args._unknown
if (parsedArgs.args.length > 0) {
resolveNegatedArgs(parsedArgs.args);
parsedArgs.args
.filter(e => e)
.forEach(unknown => {
logger.warn('Unknown argument:', unknown);
});
cliExecuter();
return;
}
const result = await cli.run(args, core);
const result = await cli.run(parsedArgs.opts(), core);
if (!result) {
return;
}
Expand Down
41 changes: 41 additions & 0 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const commander = require("commander");

/**
* Creates Argument parser corresponding to the supplied options
* parse the args and return the result
*
* @param {object[]} options Array of objects with details about flags
* @param {string[]} args process.argv or it's subset
*/
function argParser(name, options, args, helpFunction, versionFunction) {
const parser = new commander.Command();
// Set parser name
parser.name(name);

// Use customized version output
parser.on('option:version', () => {
versionFunction(args);
process.exit(0);
});

// Use customised help output
parser.on('option:help', () => {
helpFunction(args);
process.exit(0);
});

// Allow execution if unknown arguments are present
parser.allowUnknownOption(true);

// Register options on the parser
options.reduce((parserInstance, option) => {
const flags = option.alias ? `-${option.alias}, --${option.name}` : `--${option.name}`;
const flagsWithType = option.type !== Boolean ? flags + ' [type]' : flags;
parserInstance.option(flagsWithType, option.description, option.defaultValue);
return parserInstance;
}, parser);

return parser.parse(args);
}

module.exports = argParser;
15 changes: 12 additions & 3 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class WebpackCLI extends GroupHelper {
this.outputConfiguration = {};
}
setMappedGroups(args, inlineOptions) {
const { _all } = args;
Object.keys(_all).forEach(key => {
this.setGroupMap(key, _all[key], inlineOptions);
Object.keys(args).forEach(key => {
this.setGroupMap(this._toKebabCase(key), args[key], inlineOptions);
});
}
setGroupMap(key, val, inlineOptions) {
if (val === undefined) return;
const opt = inlineOptions.find(opt => opt.name === key);
const groupName = opt.group;
if (this.groupMap.has(groupName)) {
Expand Down Expand Up @@ -91,6 +91,15 @@ class WebpackCLI extends GroupHelper {
getCoreFlags() {
return core;
}

/**
* Convert camelcase to kebabcase
* @param {string} string
*/
_toKebabCase(string) {
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
return string.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
}

/**
* Based on the parsed keys, the function will import and create
* a group that handles respective values
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"chalk": "^3.0.0",
"command-line-args": "^5.1.1",
"command-line-usage": "^6.1.0",
"commander": "^5.0.0",
"enquirer": "^2.3.4",
"execa": "^3.2.0",
"import-local": "^3.0.2",
Expand Down