Skip to content
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ Trigger a notification every time. Call it "noisy-mode".
```js
new WebpackNotifierPlugin({alwaysNotify: true});
```

### Editor

Opens the file in your editor when the notification is clicked.

Takes a command and a list or arguments interpreted as template strings. Available options are `file`, `line` and `column`. Note that lines and columns of errors are only set when using a preprocessor like babel.

```js
new WebpackNotifierPlugin({ editor: { command: 'atom', args: ['${file}:${line}'] } });
```
61 changes: 50 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
var path = require('path');
var os = require('os');
var spawn = require('child_process').spawn;
var notifier = require('node-notifier');
var template = require('es6-template-strings');

var DEFAULT_LOGO = path.join(__dirname, 'logo.png');

var WebpackNotifierPlugin = module.exports = function(options) {
this.options = options || {};
this.lastBuildSucceeded = false;

if (this.options.editor) {
var editor = this.options.editor;
editor.args = editor.args || [];

notifier.on('click', function(notifierObject, notifierOptions) {
if (!notifierOptions.location) {
return;
}

var command = template(editor.command, notifierOptions.location);
var args = editor.args.map(function(arg) {
return template(arg, notifierOptions.location);
});

if (command) {
spawn(command, args);
}
});
}
};

WebpackNotifierPlugin.prototype.compileMessage = function(stats) {
Expand All @@ -19,24 +41,39 @@ WebpackNotifierPlugin.prototype.compileMessage = function(stats) {

} else if (!this.lastBuildSucceeded || this.options.alwaysNotify) {
this.lastBuildSucceeded = true;
return 'Build successful';
return { body: 'Build successful' };

} else {
return;
}

this.lastBuildSucceeded = false;

var message;
if (error.module && error.module.rawRequest)
message = error.module.rawRequest + '\n';
var rawRequest, resource, line, column;

if (error.module) {
rawRequest = error.module.rawRequest;
resource = error.module.resource;
}

var errorOrWarning = error.error || error.warning;
if (errorOrWarning && errorOrWarning.loc) {
line = errorOrWarning.loc.line;
column = errorOrWarning.loc.column;
}

if (error.error)
message = 'Error: ' + message + error.error.toString();
else if (error.warning)
message = 'Warning: ' + message + error.warning.toString();
var body = (error.error ? 'Error: ' : error.warning ? 'Warning: ' : '') +
(rawRequest ? rawRequest + '\n' : '') +
(errorOrWarning ? errorOrWarning.toString() : '');

return message;
return {
body: body,
location: {
file: resource,
line: line,
column: column
}
};
};

WebpackNotifierPlugin.prototype.compilationDone = function(stats) {
Expand All @@ -47,9 +84,11 @@ WebpackNotifierPlugin.prototype.compilationDone = function(stats) {

notifier.notify({
title: this.options.title || 'Webpack',
message: msg,
message: msg.body,
contentImage: contentImage,
icon: (os.platform() === 'win32') ? contentImage : undefined
icon: (os.platform() === 'win32') ? contentImage : undefined,
wait: !!this.options.editor,
location: msg.location
});
}
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "Tobias Bieniek <tobias.bieniek@gmx.de>",
"license": "ISC",
"dependencies": {
"es6-template-strings": "^2.0.0",
"node-notifier": "^4.1.0"
},
"devDependencies": {
Expand Down