Skip to content

Commit

Permalink
feat(cli): add parseArgs helper
Browse files Browse the repository at this point in the history
  • Loading branch information
smelukov committed Jan 29, 2020
1 parent c7fd491 commit 4bfff9c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
17 changes: 2 additions & 15 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const WebpackCLI = require('./webpack-cli');
const { core, commands } = require('./utils/cli-flags');
const cmdArgs = require('command-line-args');
const logger = require('./utils/logger');
const parseArgs = require('./utils/parse-args');

require('./utils/process-log');

Expand Down Expand Up @@ -56,22 +57,8 @@ async function runCLI(cli, commandIsUsed) {
cli.runVersion();
return;
} else if (nodeArgsExists) {
args = cmdArgs(core, { stopAtFirstUnknown: false, partial: true });
const [, , ...rawArgs] = process.argv;
const cliArgs = [];
const nodeArgs = [];
let isNodsArg = false;

for (const value of rawArgs) {
if (value === '--node-args') {
isNodsArg = true;
} else if (isNodsArg) {
isNodsArg = false;
nodeArgs.push(...value.split(' '));
} else {
cliArgs.push(value);
}
}
const { cliArgs, nodeArgs } = parseArgs(rawArgs);

try {
const childProcess = execa('node', [...nodeArgs, cliPath, ...cliArgs], { stdio: 'inherit' });
Expand Down
26 changes: 26 additions & 0 deletions lib/utils/parse-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */

/**
* Parse cli args and split these to args for node js and the rest
*
* @param {string[]} rawArgs raw cli args
* @returns {{cliArgs: string[], nodeArgs: string[]}} cli and nodejs args
*/
module.exports = rawArgs => {
const cliArgs = [];
const nodeArgs = [];
let isNodeArg = false;

for (const value of rawArgs) {
if (value === '--node-args') {
isNodeArg = true;
} else if (isNodeArg) {
isNodeArg = false;
nodeArgs.push(...value.split(' '));
} else {
cliArgs.push(value);
}
}

return { cliArgs, nodeArgs };
};
27 changes: 26 additions & 1 deletion test/node/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,34 @@
const { stat } = require('fs');
const { resolve, sep } = require('path');
const { run, extractSummary } = require('../utils/test-utils');
const parseArgs = require('../../lib/utils/parse-args');

describe('node flags', () => {
it('is able to options flags to node js', done => {
it('parseArgs helper must work correctly', () => {
[
{
rawArgs: ['--foo', '--bar', '--baz=quux'],
expectedCliArgs: ['--foo', '--bar', '--baz=quux'],
expectedNodeArgs: [],
},
{
rawArgs: ['--foo', '--bar', '--baz=quux', '--node-args', '--name1=value1', '--node-args', '--name2 value2'],
expectedCliArgs: ['--foo', '--bar', '--baz=quux'],
expectedNodeArgs: ['--name1=value1', '--name2', 'value2'],
},
{
rawArgs: ['--node-args', '--name1=value1', '--node-args', '--name2 value2', '--node-args', '-n=v', '--node-args', '-k v'],
expectedCliArgs: [],
expectedNodeArgs: ['--name1=value1', '--name2', 'value2', '-n=v', '-k', 'v'],
},
].map(({ rawArgs, expectedNodeArgs, expectedCliArgs }) => {
const { nodeArgs, cliArgs } = parseArgs(rawArgs);
expect(nodeArgs).toEqual(expectedNodeArgs);
expect(cliArgs).toEqual(expectedCliArgs);
});
});

it('is able to pass the options flags to node js', done => {
const { stdout } = run(__dirname, ['--node-args', `--require=${resolve(__dirname, 'bootstrap.js')}`, '--node-args', `-r ${resolve(__dirname, 'bootstrap2.js')}`, '--output', './bin/[name].bundle.js'], false);
expect(stdout).toContain('---from bootstrap.js---');
expect(stdout).toContain('---from bootstrap2.js---');
Expand Down

0 comments on commit 4bfff9c

Please sign in to comment.