Skip to content

Commit

Permalink
Merge pull request #44 from RoccoC/feature/callbacks
Browse files Browse the repository at this point in the history
Feature/callbacks
  • Loading branch information
RoccoC committed Aug 20, 2019
2 parents b861e3e + 8f65131 commit b39b4a1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
coverage
tests/assets
.idea
9 changes: 7 additions & 2 deletions CHANGELOG.md
@@ -1,12 +1,17 @@
# Changelog

#### 1.1.0
###### _August 10, 2019_

- Added two new config options: onCompileStart, onComplete.

#### 1.0.3
###### _June_ 4, 2019_
###### _June 4, 2019_

- Updated dependencies to latest versions to fix high-severity js-yaml vulnerability.

#### 1.0.2
###### _May_ 3, 2019_
###### _May 3, 2019_

- Corrected *sound TS types.

Expand Down
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -55,6 +55,16 @@ The sound to play for failure notifications. Defaults to the value of the *sound
#### compilationSound
The sound to play for compilation notifications. Defaults to the value of the *sound* configuration option. Set to false to play no sound for compilation notifications. Takes precedence over the *sound* configuration option.

#### onCompileStart
A function which is invoked when compilation starts. Optional. The function is passed one parameter:
* {webpack.compilation.Compilation} compilation - The webpack Compilation instance.
Note that `suppressCompileStart` must be `false`.

#### onComplete
A function which is invoked when compilation completes. Optional. The function is passed two parameters:
* {webpack.compilation.Compilation} compilation - The webpack Compilation instance.
* {CompilationStatus} status - one of 'success', 'warning', or 'error'

#### suppressSuccess
Defines when success notifications are shown. Can be one of the following values:
* false - Show success notification for each successful compilation (default).
Expand Down
22 changes: 21 additions & 1 deletion index.d.ts
@@ -1,5 +1,6 @@
import { Plugin } from 'webpack';
import NotificationCenter from 'node-notifier/notifiers/notificationcenter';
import webpack = require("webpack");

export = WebpackBuildNotifierPlugin;

Expand All @@ -15,6 +16,14 @@ declare namespace WebpackBuildNotifierPlugin {
resource?: string;
};
};
/**
* String to represent valid compilation results.
*/
enum CompilationStatus {
SUCCESS = 'success',
WARNING = 'warning',
ERROR = 'error',
}

type Config = {
/**
Expand Down Expand Up @@ -50,6 +59,18 @@ declare namespace WebpackBuildNotifierPlugin {
* Set to false to play no sound for compilation notifications. Takes precedence over the *sound* configuration option.
*/
compilationSound?: string | false;
/**
* A function which is invoked when compilation starts. Optional. The function is passed one parameter:
* 1. {webpack.compilation.Compilation} compilation - The webpack Compilation instance.
* Note that `suppressCompileStart` must be `false`.
*/
onCompileStart?: (compilation: webpack.compilation.Compilation) => void;
/**
* A function which is invoked when compilation completes. Optional. The function is passed two parameters:
* 1. {webpack.compilation.Compilation} compilation - The webpack Compilation instance.
* 2. {CompilationStatus} status - one of 'success', 'warning', or 'error'
*/
onComplete?: (compilation: webpack.compilation.Compilation, status: CompilationStatus) => void;
/**
* Defines when success notifications are shown. Can be one of the following values:
*
Expand Down Expand Up @@ -93,7 +114,6 @@ declare namespace WebpackBuildNotifierPlugin {
*/
compileIcon?: string;
/**
* @cfg {Function} onClick
* A function called when clicking on a warning or error notification. By default, it activates the Terminal application.
* The function is passed two parameters:
*
Expand Down
54 changes: 39 additions & 15 deletions index.js
Expand Up @@ -36,13 +36,13 @@ if (process.platform === 'win32') {
]
);
appName = 'Snore.DesktopToasts';
} catch(e) {
} catch (e) {
console.error("An error occurred while attempting to install the SnoreToast AppID!", e);
}
}
}

var WebpackBuildNotifierPlugin = function(cfg) {
var WebpackBuildNotifierPlugin = function (cfg) {
cfg = cfg || {};

var defaultIconPath = path.resolve(__dirname, 'icons');
Expand Down Expand Up @@ -101,10 +101,10 @@ var WebpackBuildNotifierPlugin = function(cfg) {
* True to suppress the warning notifications, otherwise false (default).
*/
this.suppressWarning = cfg.suppressWarning || false;
/**
* @cfg {Boolean} [suppressWarning=true]
* True to suppress the compilation started notifications (default), otherwise false.
*/
/**
* @cfg {Boolean} [suppressCompileStart=true]
* True to suppress the compilation started notifications (default), otherwise false.
*/
this.suppressCompileStart = cfg.suppressCompileStart !== false;
/**
* @cfg {Boolean} [activateTerminalOnError=false]
Expand All @@ -127,6 +127,20 @@ var WebpackBuildNotifierPlugin = function(cfg) {
* The absolute path to the icon to be displayed for failure notifications.
*/
this.failureIcon = cfg.failureIcon || path.join(defaultIconPath, 'failure.png');
/**
* @cfg {Function} [onCompileStart=undefined]
* A function which is invoked when compilation starts. Optional. The function is passed one parameter:
* 1) {webpack.compilation.Compilation} compilation - The webpack Compilation instance.
* Note that `suppressCompileStart` must be `false`.
*/
this.onCompileStart = cfg.onCompileStart;
/**
* @cfg {Function} [onComplete=undefined]
* A function which is invoked when compilation completes. Optional. The function is passed two parameters:
* 1) {webpack.compilation.Compilation} compilation - The webpack Compilation instance.
* 2) {CompilationStatus} status - one of 'success', 'warning', or 'error'
*/
this.onComplete = cfg.onComplete;
/**
* @cfg {String} [compileIcon='./icons/compile.png']
* The absolute path to the icon to be displayed for compilation started notifications.
Expand All @@ -136,7 +150,7 @@ var WebpackBuildNotifierPlugin = function(cfg) {
* @cfg {Function} onClick
* A function called when clicking on a warning or error notification. By default, it activates the Terminal application.
*/
this.onClick = cfg.onClick || function(notifierObject, options) { this.activateTerminalWindow(); };
this.onClick = cfg.onClick || function (notifierObject, options) { this.activateTerminalWindow(); };
/**
* @cfg {Function} onTimeout
* A function called when the notification times out (closes). Undefined by default. The function is passed
Expand All @@ -155,7 +169,7 @@ var WebpackBuildNotifierPlugin = function(cfg) {
* error/warning message.
* Note that the message will always be limited to 256 characters.
*/
this.messageFormatter = function(error, filepath) {
this.messageFormatter = function (error, filepath) {
var message = (cfg.messageFormatter || this.defaultMessageFormatter)(error, filepath);
if (typeof message === "string") {
return message.substr(0, 256); // limit message length to 256 characters, fixes #20
Expand Down Expand Up @@ -185,24 +199,24 @@ var WebpackBuildNotifierPlugin = function(cfg) {
var buildSuccessful = false;
var hasRun = false;

WebpackBuildNotifierPlugin.prototype.defaultMessageFormatter = function(error, filepath) {
WebpackBuildNotifierPlugin.prototype.defaultMessageFormatter = function (error, filepath) {
return filepath + os.EOL + (error.message ? error.message.replace(error.module ? error.module.resource : '', '') : '');
};

WebpackBuildNotifierPlugin.prototype.activateTerminalWindow = function() {
WebpackBuildNotifierPlugin.prototype.activateTerminalWindow = function () {
if (os.platform() === 'darwin') {
// TODO: is there a way to translate $TERM_PROGRAM into the application name
// to make this more flexible?
exec('TERM="$TERM_PROGRAM"; ' +
'[[ "$TERM" == "Apple_Terminal" ]] && TERM="Terminal"; ' +
'[[ "$TERM" == "vscode" ]] && TERM="Visual Studio Code"; ' +
'osascript -e "tell application \\"$TERM\\" to activate"');
'osascript -e "tell application \\"$TERM\\" to activate"');
} else if (os.platform() === 'win32') {
// TODO: Windows platform
}
};

WebpackBuildNotifierPlugin.prototype.onCompilationWatchRun = function(compilation, callback) {
WebpackBuildNotifierPlugin.prototype.onCompilationWatchRun = function (compilation, callback) {
notifier.notify({
appName: appName,
title: this.title,
Expand All @@ -211,19 +225,25 @@ WebpackBuildNotifierPlugin.prototype.onCompilationWatchRun = function(compilatio
icon: this.compileIcon,
sound: this.compilationSound
});
if (this.onCompileStart) {
this.onCompileStart(compilation);
}
callback();
};

WebpackBuildNotifierPlugin.prototype.onCompilationDone = function(results) {
WebpackBuildNotifierPlugin.prototype.onCompilationDone = function (results) {
var notify,
title = this.title + ' - ',
msg = 'Build successful!',
icon = this.successIcon,
sound = this.successSound;
sound = this.successSound,
onComplete = this.onComplete,
compilationStatus = 'success';

if (results.hasErrors()) {
var error = results.compilation.errors[0];
notify = true;
compilationStatus = 'error';
title += 'Error';
msg = error ? this.messageFormatter(error, error.module && error.module.rawRequest ? error.module.rawRequest : '') : 'Unknown';
icon = this.failureIcon;
Expand All @@ -232,6 +252,7 @@ WebpackBuildNotifierPlugin.prototype.onCompilationDone = function(results) {
} else if (!this.suppressWarning && results.hasWarnings()) {
var warning = results.compilation.warnings[0];
notify = true;
compilationStatus = 'warning';
title += 'Warning';
msg = warning ? this.messageFormatter(warning, warning.module && warning.module.rawRequest ? warning.module.rawRequest : '') : 'Unknown';
icon = this.warningIcon;
Expand Down Expand Up @@ -259,6 +280,9 @@ WebpackBuildNotifierPlugin.prototype.onCompilationDone = function(results) {
wait: !buildSuccessful
})
);
if (onComplete) {
onComplete(results.compilation, compilationStatus);
}
}

if (this.activateTerminalOnError && !buildSuccessful) {
Expand All @@ -268,7 +292,7 @@ WebpackBuildNotifierPlugin.prototype.onCompilationDone = function(results) {
hasRun = true;
};

WebpackBuildNotifierPlugin.prototype.apply = function(compiler) {
WebpackBuildNotifierPlugin.prototype.apply = function (compiler) {
if (compiler.hooks && compiler.hooks.watchRun && compiler.hooks.done) {
// for webpack >= 4
if (!this.suppressCompileStart) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "webpack-build-notifier",
"version": "1.0.3",
"version": "1.1.0",
"description": "A Webpack plugin that generates OS notifications for build steps using node-notifier.",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -42,4 +42,4 @@
"jest": "^24.8.0",
"webpack": "^4.33.0"
}
}
}
2 changes: 2 additions & 0 deletions tests/test.js
Expand Up @@ -35,6 +35,8 @@ describe('WebpackBuildNotifierPlugin instance test', () => {
expect(instanceWithTitle.successSound).toBe("Submarine");
expect(instanceWithTitle.failureSound).toBe("Submarine");
expect(instanceWithTitle.compilationSound).toBe("Submarine");
expect(instanceWithTitle.onCompileStart).toEqual(undefined);
expect(instanceWithTitle.onComplete).toEqual(undefined);
expect(instanceWithTitle.suppressSuccess).toBe(false);
expect(instanceWithTitle.suppressWarning).toBe(false);
expect(instanceWithTitle.suppressCompileStart).toBe(true);
Expand Down

0 comments on commit b39b4a1

Please sign in to comment.