Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always spawn a process for cli #1198

Merged
merged 3 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

smelukov marked this conversation as resolved.
Show resolved Hide resolved
require('v8-compile-cache');

const execa = require('execa');
const importLocal = require('import-local');
// eslint-disable-next-line node/no-unpublished-require
const logger = require('../lib/utils/logger');

// eslint-disable-next-line node/no-unpublished-require
const parseArgs = require('../lib/utils/parse-args');

// Prefer the local installation of webpack-cli
if (importLocal(__filename)) {
// return;
Expand Down Expand Up @@ -36,5 +40,11 @@ if (!semver.satisfies(process.version, version)) {
process.exit(1);
}

const [, , ...rawArgs] = process.argv;
const { cliArgs, nodeArgs } = parseArgs(rawArgs);
// eslint-disable-next-line node/no-unpublished-require
require('../lib/bootstrap');
const bootstrapPath = require.resolve('../lib/bootstrap');

execa('node', [...nodeArgs, bootstrapPath, ...cliArgs], { stdio: 'inherit' }).catch(e => {
process.exit(e.exitCode);
});
17 changes: 2 additions & 15 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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');
const parseArgs = require('./utils/parse-args');

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

const cliPath = require.resolve('../bin/cli.js');
process.title = 'webpack-cli';

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 @@ -47,25 +46,13 @@ async function runCLI(cli, commandIsUsed) {
let args;
const helpFlagExists = isFlagPresent(process.argv, 'help');
const versionFlagExists = isFlagPresent(process.argv, 'version');
const nodeArgsExists = process.argv.find(arg => arg.includes('--node-args'));

if (helpFlagExists) {
cli.runHelp(process.argv);
return;
} else if (versionFlagExists) {
cli.runVersion();
return;
} else if (nodeArgsExists) {
const [, , ...rawArgs] = process.argv;
const { cliArgs, nodeArgs } = parseArgs(rawArgs);

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

if (commandIsUsed) {
Expand Down
3 changes: 2 additions & 1 deletion packages/generators/__tests__/init-generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { join } from 'path';
import { run } from 'yeoman-test';

describe('init generator', () => {
// fixme: unstable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a flaky test? Seems to pass locally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally but on Travis it times out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can increase the timeout for these tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a PR and let's see

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing 👍

describe.skip('init generator', () => {
it('generates a webpack project config', async () => {
const outputDir = await run(join(__dirname, '../init-generator')).withPrompts({
multiEntries: false,
Expand Down