Skip to content

Commit 5998865

Browse files
author
Maksim Elkin
committed
node-arguments simplify processing
1 parent bb312dd commit 5998865

File tree

6 files changed

+77
-61
lines changed

6 files changed

+77
-61
lines changed

lib/api.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
23
const path = require('path');
34
const fs = require('fs');
45
const os = require('os');
@@ -15,6 +16,8 @@ const makeDir = require('make-dir');
1516
const ms = require('ms');
1617
const chunkd = require('chunkd');
1718
const Emittery = require('emittery');
19+
const minimist = require('minimist');
20+
const dargs = require('dargs');
1821
const globs = require('./globs');
1922
const RunStatus = require('./run-status');
2023
const fork = require('./fork');
@@ -320,28 +323,28 @@ class Api extends Emittery {
320323
}
321324

322325
async _computeForkExecArgv() {
323-
const {nodeArguments: execArgv} = this.options;
324-
if (execArgv.length === 0) {
325-
return execArgv;
326-
}
326+
const {nodeArguments: configExecArgs} = this.options;
327327

328-
// --inspect-brk is used in addition to --inspect to break on first line and wait
329-
const inspectArgIndex = execArgv.findIndex(arg => /^--inspect(-brk)?($|=)/.test(arg));
330-
if (inspectArgIndex === -1) {
331-
return execArgv;
332-
}
328+
const mainProcessArgs = minimist(process.execArgv);
333329

334-
const port = await getPort();
335-
const forkExecArgv = execArgv.slice();
336-
let flagName = '--inspect';
337-
const oldValue = forkExecArgv[inspectArgIndex];
338-
if (oldValue.includes('brk')) {
339-
flagName += '-brk';
340-
}
330+
const args = {
331+
...mainProcessArgs,
332+
...configExecArgs
333+
};
341334

342-
forkExecArgv[inspectArgIndex] = `${flagName}=${port}`;
335+
const hasInspect = args.inspect || args['inspect-brk'];
336+
if (hasInspect) {
337+
if (args.inspect && args['inspect-brk']) {
338+
throw new Error('Flags inspect and inspect-brk provided simultaneously');
339+
}
340+
341+
const port = await getPort();
342+
343+
const flagName = args.inspect ? 'inspect' : 'inspect-brk';
344+
args[flagName] = port;
345+
}
343346

344-
return forkExecArgv;
347+
return dargs(args).join(' ');
345348
}
346349
}
347350

lib/cli.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ exports.run = async () => { // eslint-disable-line complexity
118118
},
119119
'node-arguments': {
120120
type: 'string',
121-
default: conf.nodeArguments
121+
default: ''
122122
},
123123
'--': {
124124
type: 'string'
@@ -220,6 +220,13 @@ exports.run = async () => { // eslint-disable-line complexity
220220
exit(error.message);
221221
}
222222

223+
let nodeArguments;
224+
try {
225+
nodeArguments = normalizeNodeArguments(conf.nodeArguments, cli.flags.nodeArguments);
226+
} catch (error) {
227+
exit(error.message);
228+
}
229+
223230
// Copy resultant cli.flags into conf for use with Api and elsewhere
224231
Object.assign(conf, cli.flags);
225232

@@ -271,7 +278,7 @@ exports.run = async () => { // eslint-disable-line complexity
271278
snapshotDir: conf.snapshotDir ? path.resolve(projectDir, conf.snapshotDir) : null,
272279
timeout: conf.timeout,
273280
updateSnapshots: conf.updateSnapshots,
274-
nodeArguments: normalizeNodeArguments(conf.nodeArguments || []),
281+
nodeArguments,
275282
workerArgv: cli.flags['--']
276283
});
277284

lib/node-arguments.js

+13-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
'use strict';
22

3-
const trim = require('lodash/trim');
4-
5-
// Parse formats:
6-
// --param=--parameter --abc
7-
// |_this____|
8-
// --param=" --arg1 --arg2 param " --abc
9-
// |_this part___________|
10-
function parseCliParameter(str) {
11-
str = str[0].startsWith('"') && str.endsWith('"') ? str.slice(1, str.length - 1) : str;
12-
return trim(str)
13-
.split(' ');
14-
}
15-
16-
function normalizeNodeArguments(nodeArguments) {
17-
const parsed = Array.isArray(nodeArguments) ? nodeArguments : parseCliParameter(nodeArguments);
18-
19-
const detectedInspect = parsed.find(arg => /^--inspect(-brk)?(=|$)/.test(arg));
20-
21-
if (detectedInspect && detectedInspect.includes('=')) {
22-
throw new Error('The \'nodeArguments\' configuration must not contain inspect with port.');
23-
}
24-
25-
const mainProcessArgs = process.execArgv.filter(arg => !detectedInspect || !/^--inspect(-brk)?(=|$)/.test(arg));
26-
27-
return parsed.concat(mainProcessArgs);
3+
const minimist = require('minimist');
4+
const omit = require('lodash/omit');
5+
6+
/**
7+
* @param {string[]} confParams
8+
* @param {string} cliParams
9+
* @return {object}
10+
*/
11+
function normalizeNodeArguments(confParams, cliParams) {
12+
return omit({
13+
...minimist(confParams),
14+
...minimist(cliParams.split(' '))
15+
}, '_');
2816
}
2917

3018
module.exports = normalizeNodeArguments;

package-lock.json

+23-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"concordance": "^4.0.0",
8181
"convert-source-map": "^1.6.0",
8282
"currently-unhandled": "^0.4.1",
83+
"dargs": "^7.0.0",
8384
"debug": "^4.1.1",
8485
"del": "^5.1.0",
8586
"dot-prop": "^5.2.0",
@@ -106,6 +107,7 @@
106107
"md5-hex": "^3.0.1",
107108
"meow": "^5.0.0",
108109
"micromatch": "^4.0.2",
110+
"minimist": "^1.2.0",
109111
"ms": "^2.1.2",
110112
"observable-to-promise": "^1.0.0",
111113
"ora": "^4.0.2",

test/node-arguments.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@ const {test} = require('tap');
44
const normalizeNodeArguments = require('../lib/node-arguments');
55

66
test('normalizes multiple node arguments from cli', t => {
7-
t.deepEqual(normalizeNodeArguments('"--a --b --c"'), ['--a', '--b', '--c']);
7+
t.deepEqual(normalizeNodeArguments([], '--a --b --c'), {a: true, b: true, c: true});
88
t.end();
99
});
1010

1111
test('normalizes multiple node arguments from config', t => {
12-
t.deepEqual(normalizeNodeArguments(['--arg1', '--b']), ['--arg1', '--b']);
12+
t.deepEqual(normalizeNodeArguments(['--arg1', '--b'], ''), {arg1: true, b: true});
1313
t.end();
1414
});
1515

16-
test('normalizes single node arguments from cli', t => {
17-
t.deepEqual(normalizeNodeArguments('--test-flag'), ['--test-flag']);
18-
t.end();
19-
});
20-
21-
test('removes extra inspect', t => {
22-
process.execArgv = ['--inspect-brk=123'];
23-
t.deepEqual(normalizeNodeArguments('--inspect'), ['--inspect']);
16+
test('normalizes node arguments from config and cli', t => {
17+
t.deepEqual(
18+
normalizeNodeArguments(['--arg1', '--b=2'], '--arg2 --b=12'),
19+
{arg1: true, arg2: true, b: 12}
20+
);
2421
t.end();
2522
});
2623

27-
test('fails on inspect with port', t => {
28-
t.throws(() => normalizeNodeArguments('--inspect=9230'), 'The \'nodeArguments\' configuration must not contain inspect with port.');
24+
test('normalizes single node arguments from cli', t => {
25+
t.deepEqual(normalizeNodeArguments([], '--test-flag'), {'test-flag': true});
2926
t.end();
3027
});
31-

0 commit comments

Comments
 (0)