From 83f73b056e224301b871bee5e9b7254e64e84e95 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 19 Aug 2020 15:50:55 +0530 Subject: [PATCH] fix: use appropriate exit codes (#1755) --- README.md | 9 +++++++++ packages/package-utils/__tests__/packageUtils.test.ts | 2 +- packages/package-utils/src/packageUtils.ts | 2 +- packages/utils/src/modify-config-helper.ts | 6 +++--- packages/utils/src/npm-packages-exists.ts | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 4 ++-- packages/webpack-cli/lib/utils/process-log.js | 2 +- 7 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b65ccaf496a..7d69c4b4bf9 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ - [Utilities](#utilities) - [Getting started](#getting-started) - [webpack CLI Scaffolds](#webpack-cli-scaffolds) +- [Exit codes and their meanings](#exit-codes-and-their-meanings) - [Contributing and Internal Documentation](#contributing-and-internal-documentation) - [Open Collective](#open-collective) @@ -96,6 +97,14 @@ With v3 of webpack CLI, we introduced scaffolding as an integral part of the CLI You can read more about [Scaffolding](https://webpack.js.org/guides/scaffolding), learn [How to compose a webpack-scaffold?](https://webpack.js.org/contribute/writing-a-scaffold) or generate one with [webpack-scaffold-starter](https://github.com/rishabh3112/webpack-scaffold-starter). +## Exit codes and their meanings + +| Exit Code | Description | +| --------- | -------------------------------------------------- | +| `0` | Success | +| `1` | Warnings/Errors from webpack | +| `2` | Configuration/options problem or an internal error | + ## Contributing and Internal Documentation The webpack family welcomes any contributor, small or big. We are happy to elaborate, guide you through the source code and find issues you might want to work on! To get started have a look at our [documentation on contributing](./.github/CONTRIBUTING.md). diff --git a/packages/package-utils/__tests__/packageUtils.test.ts b/packages/package-utils/__tests__/packageUtils.test.ts index 64bba4600b1..e853fa2ac0a 100644 --- a/packages/package-utils/__tests__/packageUtils.test.ts +++ b/packages/package-utils/__tests__/packageUtils.test.ts @@ -177,7 +177,7 @@ describe('packageUtils', () => { // runCommand should not be called, because the installation is not confirmed expect((runCommand as jest.Mock).mock.calls.length).toEqual(0); expect((prompt as jest.Mock).mock.calls[0][0][0].message).toMatch(/Would you like to install test-package\?/); - expect(process.exitCode).toEqual(-1); + expect(process.exitCode).toEqual(2); }); }); }); diff --git a/packages/package-utils/src/packageUtils.ts b/packages/package-utils/src/packageUtils.ts index 09b9f0330f5..167d7bee322 100644 --- a/packages/package-utils/src/packageUtils.ts +++ b/packages/package-utils/src/packageUtils.ts @@ -98,5 +98,5 @@ export async function promptInstallation(packageName: string, preMessage?: Funct return exports.packageExists(packageName); } // eslint-disable-next-line require-atomic-updates - process.exitCode = -1; + process.exitCode = 2; } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index eb3681b0d3f..913d0d21e84 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -86,7 +86,7 @@ export function modifyHelperUtil( } catch (err) { console.error(red('\nYour package.json was incorrectly formatted.\n')); Error.stackTraceLimit = 0; - process.exitCode = -1; + process.exitCode = 2; } env.registerStub(generator, generatorName); @@ -108,7 +108,7 @@ export function modifyHelperUtil( red("\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n"), ); Error.stackTraceLimit = 0; - process.exitCode = -1; + process.exitCode = 2; } try { // the configuration stored in .yo-rc.json should already be in the correct @@ -126,7 +126,7 @@ export function modifyHelperUtil( red('\nYour yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'), ); Error.stackTraceLimit = 0; - process.exitCode = -1; + process.exitCode = 2; } const transformConfig = Object.assign( diff --git a/packages/utils/src/npm-packages-exists.ts b/packages/utils/src/npm-packages-exists.ts index a5b3d4b7d11..3233a8027bb 100644 --- a/packages/utils/src/npm-packages-exists.ts +++ b/packages/utils/src/npm-packages-exists.ts @@ -61,7 +61,7 @@ export function npmPackagesExists(pkg: string[]): void { .catch((err: Error): void => { console.error(err.stack || err); // eslint-disable-next-line no-process-exit - process.exit(0); + process.exit(2); }) .then(resolvePackagesIfReady); }); diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index c78612e8b54..b72ab8ea325 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -58,13 +58,13 @@ class HelpGroup { } } catch (e) { logger.error('Error: External package not found.'); - process.exitCode = 1; + process.exit(2); } } if (commandsUsed.length > 1) { logger.error('You provided multiple commands. Please use only one command at a time.\n'); - process.exit(1); + process.exit(2); } if (invalidArgs.length > 0) { diff --git a/packages/webpack-cli/lib/utils/process-log.js b/packages/webpack-cli/lib/utils/process-log.js index 17dfa0a263f..45933841849 100644 --- a/packages/webpack-cli/lib/utils/process-log.js +++ b/packages/webpack-cli/lib/utils/process-log.js @@ -2,7 +2,7 @@ const logger = require('./logger'); function logErrorAndExit(error) { if (error && error.stack) logger.error(error.stack); - process.exit(1); + process.exit(error.exitCode); } process.on('uncaughtException', (error) => {