diff --git a/README.md b/README.md index cf993e54..71949cf3 100644 --- a/README.md +++ b/README.md @@ -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}'] } }); +``` diff --git a/index.js b/index.js index 65f9f8c8..c96a649f 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -19,7 +41,7 @@ WebpackNotifierPlugin.prototype.compileMessage = function(stats) { } else if (!this.lastBuildSucceeded || this.options.alwaysNotify) { this.lastBuildSucceeded = true; - return 'Build successful'; + return { body: 'Build successful' }; } else { return; @@ -27,16 +49,31 @@ WebpackNotifierPlugin.prototype.compileMessage = function(stats) { 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) { @@ -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 }); } }; diff --git a/package.json b/package.json index ec7f8934..887ac3dd 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "author": "Tobias Bieniek ", "license": "ISC", "dependencies": { + "es6-template-strings": "^2.0.0", "node-notifier": "^4.1.0" }, "devDependencies": {