From 4bfff9cac57b141ba29e4138d31b32f2cbec4374 Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Wed, 29 Jan 2020 21:07:40 +0300 Subject: [PATCH] feat(cli): add parseArgs helper --- lib/bootstrap.js | 17 ++--------------- lib/utils/parse-args.js | 26 ++++++++++++++++++++++++++ test/node/node.test.js | 27 ++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 lib/utils/parse-args.js diff --git a/lib/bootstrap.js b/lib/bootstrap.js index 72da794ea81..87234a5ac81 100644 --- a/lib/bootstrap.js +++ b/lib/bootstrap.js @@ -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'); @@ -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' }); diff --git a/lib/utils/parse-args.js b/lib/utils/parse-args.js new file mode 100644 index 00000000000..d504c14e614 --- /dev/null +++ b/lib/utils/parse-args.js @@ -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 }; +}; diff --git a/test/node/node.test.js b/test/node/node.test.js index 073d79d1134..469268affcd 100644 --- a/test/node/node.test.js +++ b/test/node/node.test.js @@ -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---');