Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Modernizing ProcessExitCode plugin (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Earl authored and Blackbaud-PaulCrowder committed Jun 7, 2017
1 parent e7e9223 commit 018127d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
4 changes: 2 additions & 2 deletions config/webpack/common.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
const processExitCode = require('../../plugin/process-exit-code');
const ProcessExitCode = require('../../plugin/process-exit-code');
const skyPagesConfigUtil = require('../sky-pages/sky-pages.config');
const aliasBuilder = require('./alias-builder');

Expand Down Expand Up @@ -142,7 +142,7 @@ function getWebpackConfig(skyPagesConfig) {
),

// Webpack 2 behavior does not correctly return non-zero exit code.
processExitCode
new ProcessExitCode()
]
};
}
Expand Down
4 changes: 2 additions & 2 deletions config/webpack/test.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getWebpackConfig(skyPagesConfig, argv) {
const DefinePlugin = require('webpack/lib/DefinePlugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const processExitCode = require('../../plugin/process-exit-code');
const ProcessExitCode = require('../../plugin/process-exit-code');
const skyPagesConfigUtil = require('../sky-pages/sky-pages.config');
const aliasBuilder = require('./alias-builder');

Expand Down Expand Up @@ -160,7 +160,7 @@ function getWebpackConfig(skyPagesConfig, argv) {
),

// Webpack 2 behavior does not correctly return non-zero exit code.
processExitCode
new ProcessExitCode()
]
};

Expand Down
18 changes: 7 additions & 11 deletions plugin/process-exit-code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
* This function is inspired from webpack-fail-plugin, which has been deprecated.
* Unfortunately, webpack does not correctly return a non-zero exit code unless using their CLI.
*/
module.exports = function processExitCode() {
let isWatch = true;
function ProcessExitCode() { }

this.plugin('run', (compiler, callback) => {
isWatch = false;
callback.call(compiler);
});

this.plugin('done', stats => {
if (stats.compilation.errors && stats.compilation.errors.length && !isWatch) {
process.on('beforeExit', () => process.exit(1));
ProcessExitCode.prototype.apply = function (compiler) {
compiler.plugin('done', stats => {
if (stats.compilation.errors && stats.compilation.errors.length) {
process.on('exit', () => process.exitCode = 1);
}
});

};

module.exports = ProcessExitCode;
49 changes: 40 additions & 9 deletions test/config-webpack-common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const path = require('path');
const fs = require('fs');
const ProcessExitCode = require('../plugin/process-exit-code');
const runtimeUtils = require('../utils/runtime-test-utils');

describe('config webpack common', () => {
Expand Down Expand Up @@ -123,12 +124,11 @@ describe('config webpack common', () => {
});

expect(processOnSpy).not.toHaveBeenCalled();
})
});

it('should pass a non-zero exit code to process.exit on errors', () => {
const processExitSpy = spyOn(process, 'exit');
it('should set process.exitCode to 1 if compilation errors', () => {
const processOnSpy = spyOn(process, 'on').and.callFake((evt, cb) => {
if (evt === 'beforeExit') {
if (evt === 'exit') {
cb();
}
});
Expand All @@ -139,13 +139,10 @@ describe('config webpack common', () => {
});

config.plugins.forEach(plugin => {
if (plugin.name === 'processExitCode') {
if (plugin instanceof ProcessExitCode) {
plugin.apply({
plugin: (evt, cb) => {
switch (evt) {
case 'run':
cb(() => {}, () => {});
break;
case 'done':
cb({
compilation: {
Expand All @@ -162,7 +159,41 @@ describe('config webpack common', () => {
});

expect(processOnSpy).toHaveBeenCalled();
expect(processExitSpy).toHaveBeenCalledWith(1);
expect(process.exitCode).toEqual(1);
});

it('should not set process.exit listener if no compilation errors', () => {

// Reset process.exitCode from any previous tests
process.exitCode = 0;

const processOnSpy = spyOn(process, 'on');
const lib = require('../config/webpack/common.webpack.config');
const config = lib.getWebpackConfig({
runtime: runtimeUtils.getDefaultRuntime(),
skyux: {}
});

config.plugins.forEach(plugin => {
if (plugin instanceof ProcessExitCode) {
plugin.apply({
plugin: (evt, cb) => {
switch (evt) {
case 'done':
cb({
compilation: {
errors: []
}
});
break;
}
}
});
}
});

expect(processOnSpy).not.toHaveBeenCalled();
expect(process.exitCode).not.toEqual(1);
});

});

0 comments on commit 018127d

Please sign in to comment.