-
-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to pass flags to node.js (#1195)
* feat(cli): allow to pass flags to node.js closes #1084 closes #289 * chore(cli): fix snapshot * chore(cli): drop nodejs 8 from ci * chore(cli): fix test * feat(cli): add parseArgs helper * feat(cli): parseArgs now supports --node-args="..." syntax
- Loading branch information
Showing
13 changed files
with
122 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* 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 (value.startsWith('--node-args=')) { | ||
const [, argValue] = value.match(/^--node-args="?(.+?)"?$/); | ||
nodeArgs.push(argValue); | ||
} else if (isNodeArg) { | ||
isNodeArg = false; | ||
nodeArgs.push(...value.split(' ')); | ||
} else { | ||
cliArgs.push(value); | ||
} | ||
} | ||
|
||
return { cliArgs, nodeArgs }; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'a.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('---from bootstrap.js---'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('---from bootstrap2.js---'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
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('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', '--name3 value3', '--node-args', '-k v'], | ||
expectedCliArgs: [], | ||
expectedNodeArgs: ['--name1=value1', '--name2="value2"', '--name3', 'value3', '-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---'); | ||
const summary = extractSummary(stdout); | ||
const outputDir = 'node/bin'; | ||
const outDirectoryFromCompiler = summary['Output Directory'].split(sep); | ||
const outDirToMatch = outDirectoryFromCompiler.slice(outDirectoryFromCompiler.length - 2, outDirectoryFromCompiler.length).join('/'); | ||
expect(outDirToMatch).toContain(outputDir); | ||
stat(resolve(__dirname, './bin/main.bundle.js'), (err, stats) => { | ||
expect(err).toBe(null); | ||
expect(stats.isFile()).toBe(true); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const { resolve } = require('path'); | ||
|
||
module.exports = { | ||
entry: './a.js', | ||
output: { | ||
path: resolve(__dirname, 'binary'), | ||
filename: 'a.bundle.js', | ||
}, | ||
}; |