Skip to content

Commit

Permalink
Updated node-notifier to fix #43; added test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
RoccoC committed Oct 15, 2019
1 parent e153dd8 commit 1a5987d
Show file tree
Hide file tree
Showing 14 changed files with 3,346 additions and 5,719 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

#### 1.2.2
###### _October 15, 2019_

- Updated node-notifier to fix [#43](/../../issues/43); added test coverage.

#### 1.2.1
###### _October 15, 2019_

Expand Down
81 changes: 44 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,7 @@ var process = require('process');
var os = require('os');
var notifier = require('node-notifier');
var stripAnsi = require('strip-ansi');
var exec = require('child_process').exec;

// ensure the SnoreToast appId is registered, which is needed for Windows Toast notifications
// this is necessary in Windows 8 and above, (Windows 10 post build 1709), where all notifications must be generated
// by a valid application.
// see: https://github.com/KDE/snoretoast, https://github.com/RoccoC/webpack-build-notifier/issues/20
var appName;
if (process.platform === 'win32') {
var os = require('os');
var versionParts = os.release().split('.');
var winVer = +(`${versionParts[0]}.${versionParts[1]}`)
if (winVer >= 6.2) {
// Windows version >= 8
var cp = require('child_process');
var snoreToast = path.join(require.resolve('node-notifier'), '../vendor/snoreToast/SnoreToast.exe');
try {
cp.execFileSync(
snoreToast,
[
'-appID',
'Snore.DesktopToasts',
'-install',
'SnoreToast.lnk',
snoreToast,
'Snore.DesktopToasts'
]
);
appName = 'Snore.DesktopToasts';
} catch (e) {
console.error("An error occurred while attempting to install the SnoreToast AppID!", e);
}
}
}
var { exec, execFileSync } = require('child_process');

var WebpackBuildNotifierPlugin = function (cfg) {
cfg = cfg || {};
Expand Down Expand Up @@ -194,6 +162,8 @@ var WebpackBuildNotifierPlugin = function (cfg) {
*/
this.notifyOptions = cfg.notifyOptions || {};

this.registerSnoreToast();

// add notification click handler to activate terminal window
notifier.on('click', this.onClick.bind(this));
if (this.onTimeout) {
Expand All @@ -209,21 +179,21 @@ WebpackBuildNotifierPlugin.prototype.defaultMessageFormatter = function (error,
};

WebpackBuildNotifierPlugin.prototype.activateTerminalWindow = function () {
if (os.platform() === 'darwin') {
if (process.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"');
} else if (os.platform() === 'win32') {
} else if (process.platform === 'win32') {
// TODO: Windows platform
}
};

WebpackBuildNotifierPlugin.prototype.onCompilationWatchRun = function (compilation, callback) {
notifier.notify({
appName: appName,
appName: this.appName,
title: this.title,
message: 'Compilation started...',
contentImage: this.logo,
Expand Down Expand Up @@ -276,7 +246,7 @@ WebpackBuildNotifierPlugin.prototype.onCompilationDone = function (results) {
if (notify) {
notifier.notify(
Object.assign(this.notifyOptions, {
appName: appName,
appName: this.appName,
title: title,
message: stripAnsi(msg),
sound: sound,
Expand Down Expand Up @@ -313,4 +283,41 @@ WebpackBuildNotifierPlugin.prototype.apply = function (compiler) {
}
};

WebpackBuildNotifierPlugin.prototype.registerSnoreToast = function () {
// ensure the SnoreToast appId is registered, which is needed for Windows Toast notifications
// this is necessary in Windows 8 and above, (Windows 10 post build 1709), where all notifications must be generated
// by a valid application.
// see: https://github.com/KDE/snoretoast, https://github.com/RoccoC/webpack-build-notifier/issues/20
/* istanbul ignore else */
if (process.platform === 'win32') {
const versionParts = os.release().split('.');
const winVer = +(`${versionParts[0]}.${versionParts[1]}`);
/* istanbul ignore else */
if (winVer >= 6.2) {
// Windows version >= 8
const snoreToast = path.join(
require.resolve('node-notifier'),
'../vendor/snoreToast',
process.arch === 'x64' ? 'snoretoast-x64.exe' : 'snoretoast-x86.exe'
);
try {
execFileSync(
snoreToast,
[
'-appID',
'Snore.DesktopToasts',
'-install',
'SnoreToast.lnk',
snoreToast,
'Snore.DesktopToasts'
]
);
this.appName = 'Snore.DesktopToasts';
} catch (e) {
console.error('An error occurred while attempting to install the SnoreToast AppID!', e);
}
}
}
};

module.exports = WebpackBuildNotifierPlugin;
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ["index.js"],
coverageDirectory: "coverage",
moduleFileExtensions: ["js"],
testEnvironment: "node"
};

0 comments on commit 1a5987d

Please sign in to comment.