Skip to content

Commit

Permalink
feat(cli): allow to pass flags to node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
smelukov committed Jan 29, 2020
1 parent aa6d82b commit 49db9de
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 22 deletions.
28 changes: 28 additions & 0 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
const execa = require('execa');
const WebpackCLI = require('./webpack-cli');
const { core, commands } = require('./utils/cli-flags');
const cmdArgs = require('command-line-args');
const logger = require('./utils/logger');

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

const cliPath = require.resolve('../cli.js');
const isFlagPresent = (args, flag) => args.find(arg => [flag, `--${flag}`].includes(arg));
const isArgCommandName = (arg, cmd) => arg === cmd.name || arg === cmd.alias;
const removeCmdFromArgs = (args, cmd) => args.filter(arg => !isArgCommandName(arg, cmd));
Expand Down Expand Up @@ -45,13 +47,39 @@ async function runCLI(cli, commandIsUsed) {
let args;
const helpFlagExists = isFlagPresent(process.argv, 'help');
const versionFlagExists = isFlagPresent(process.argv, 'version');
const nodeArgsExists = isFlagPresent(process.argv, 'node-args');

if (helpFlagExists) {
cli.runHelp(process.argv);
return;
} else if (versionFlagExists) {
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);
}
}

try {
const childProcess = execa('node', [...nodeArgs, cliPath, ...cliArgs], { stdio: 'inherit' });
await childProcess;
process.exit();
} catch (e) {
process.exit(e.exitCode);
}
}

if (commandIsUsed) {
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ module.exports = {
group: BASIC_GROUP,
description: 'Get current version',
},
{
name: 'node-args',
usage: '--node-args "--max-old-space-size=1024"',
type: String,
multiple: true,
group: BASIC_GROUP,
description: 'NodeJS flags',
},
/* {
name: "analyze",
type: Boolean,
Expand Down
28 changes: 7 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"cli-table3": "^0.5.1",
"command-line-args": "^5.1.1",
"command-line-usage": "^6.1.0",
"execa": "^3.2.0",
"import-local": "^3.0.2",
"interpret": "^2.0.0",
"terser-webpack-plugin": "^2.3.2",
Expand All @@ -144,7 +145,6 @@
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.1.1",
"execa": "^3.2.0",
"husky": "^3.0.9",
"jest": "^24.9.0",
"jest-cli": "^24.9.0",
Expand Down
1 change: 1 addition & 0 deletions test/node/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'a.js';
1 change: 1 addition & 0 deletions test/node/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('---from bootstrap.js---');
1 change: 1 addition & 0 deletions test/node/bootstrap2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('---from bootstrap2.js---');
23 changes: 23 additions & 0 deletions test/node/node.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const { stat } = require('fs');
const { resolve, sep } = require('path');
const { run, extractSummary } = require('../utils/test-utils');

describe('node flags', () => {
it('is able to options flags to node js', done => {
const { stdout, stderr } = run(__dirname, ['--node-args', `--require=${resolve(__dirname, 'bootstrap.js')}`, '--node-args', `-r ${resolve(__dirname, 'bootstrap2.js')}`, '--output', './bin/[name].bundle.js'], false);
expect(stderr).toContain('');
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();
});
});
});
9 changes: 9 additions & 0 deletions test/node/webpack.config.js
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',
},
};

0 comments on commit 49db9de

Please sign in to comment.