Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open editor when clicking on notification [2] #63

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = {
],
rules: {
'vars-on-top': 0,
'consistent-return': 0
'consistent-return': 0,
'no-nested-ternary': 0,
'one-var': 0,
'one-var-declaration-per-line': 0,
'object-curly-newline': 0
}
};
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ Do not notify on the first build. This allows you to receive notifications on s
```js
new WebpackNotifierPlugin({skipFirstNotification: 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}'] } });
```
11 changes: 10 additions & 1 deletion example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ module.exports = {
filename: "bundle.js"
},
plugins: [
new WebpackNotifierPlugin(),
new WebpackNotifierPlugin({
editor: {
command: 'C:\\Program Files\\JetBrains\\WebStorm 2019.2.4\\bin\\webstorm.bat',
args: [
'--line', '${line}',
'--column', '${column}',
'${file}',
]
}
}),
]
};
57 changes: 44 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var stripANSI = require('strip-ansi');
var path = require('path');
var os = require('os');
var spawn = require('cross-spawn');
var notifier = require('node-notifier');
var template = require('es6-template-strings');

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

Expand Down Expand Up @@ -80,23 +82,32 @@ WebpackNotifierPlugin.prototype.compileEndOptions = 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;
}

if (error.error) {
message = (hasEmoji ? '❌ ' : '') + 'Error: ' + message + error.error.toString();
} else if (error.warning) {
message = (hasEmoji ? '⚠️ ' : '') + 'Warning: ' + message + error.warning.toString();
} else if (error.message) {
message = (hasEmoji ? '⚠️ ' : '') + 'Warning: ' + message + error.message.toString();
var errorOrWarning = error.error || error.warning || error.message;
if (errorOrWarning && errorOrWarning.loc) {
line = errorOrWarning.loc.line;
column = errorOrWarning.loc.column;
}

var message = (error.error ? (hasEmoji ? '❌ ' : '') + 'Error: ' : (error.warning || error.message) ? (hasEmoji ? '⚠️ ' : '') + 'Warning: ' : '')
+ (rawRequest ? rawRequest + '\n' : '')
+ (errorOrWarning ? errorOrWarning.toString() : '');

return {
message: stripANSI(message),
contentImage: contentImage,
status: status
status: status,
location: {
file: resource,
line: line,
column: column
}
};
};

Expand All @@ -111,7 +122,8 @@ WebpackNotifierPlugin.prototype.hasWarnings = function (stats) {
};

WebpackNotifierPlugin.prototype.compilationDone = function (stats) {
var { message, contentImage, status } = this.compileEndOptions(stats);
var { editor } = this.options;
var { message, contentImage, status, location } = this.compileEndOptions(stats);
if (message) {
var title = this.options.title ? this.options.title : 'Webpack';
if (typeof title === 'function') {
Expand All @@ -133,9 +145,28 @@ WebpackNotifierPlugin.prototype.compilationDone = function (stats) {
title,
message,
contentImage,
icon
icon,
wait: !!this.options.editor,
location: location
}
), function (error, response) {
console.log('webpack-notifier debug info', { // TODO - WIP debug info
notifier: notifier.Notification.name,
error,
response,
metadata: Array.from(arguments).slice(2)
});

if (editor && response === 'activate' && location) {
var command = template(editor.command, location);
var args = (editor.args || []).map(function (arg) {
return template(arg, location);
});
if (command) {
spawn(command, args);
}
}
));
});
}
};

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"author": "Tobias Bieniek <tobias.bieniek@gmx.de>",
"license": "ISC",
"dependencies": {
"node-notifier": "^8.0.0",
"cross-spawn": "^7.0.3",
"es6-template-strings": "^2.0.0",
"node-notifier": "mikaelbr/node-notifier#e55bd8fa64",
"strip-ansi": "^6.0.0"
},
"devDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion test/VerbosityLevelAllVariants.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import {contentImageSerializer, reduceArraySerializer, testChangesFlow} from './helpers/utils';
import {
changedOptionsSerializer,
contentImageSerializer,
reduceArraySerializer,
skipNotifierCallback,
testChangesFlow
} from './helpers/utils';

expect.addSnapshotSerializer(reduceArraySerializer);
expect.addSnapshotSerializer(contentImageSerializer);
expect.addSnapshotSerializer(skipNotifierCallback);
expect.addSnapshotSerializer(changedOptionsSerializer);

describe('VerbosityLevelAllVariants', () => {
describe.each([...generateOptions()])('%j', (opts) => {
Expand Down
11 changes: 10 additions & 1 deletion test/WebpackNotifierPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import {join} from 'path';
import {contentImageSerializer, reduceArraySerializer, testChangesFlow, TestArguments} from './helpers/utils';
import {
contentImageSerializer,
reduceArraySerializer,
changedOptionsSerializer,
testChangesFlow,
TestArguments,
skipNotifierCallback
} from './helpers/utils';
import CustomWarningPlugin from './helpers/CustomWarningPlugin';
import ChildCompilationPlugin from './helpers/ChildCompilationPlugin';

expect.addSnapshotSerializer(reduceArraySerializer);
expect.addSnapshotSerializer(contentImageSerializer);
expect.addSnapshotSerializer(skipNotifierCallback);
expect.addSnapshotSerializer(changedOptionsSerializer);

describe('WebpackNotifierPlugin', () => {
describe('one compilation', () => {
Expand Down