From 74ed0c2ebc366a41d053da3f12fed9225d29d2f0 Mon Sep 17 00:00:00 2001 From: 0x04 Date: Thu, 29 Mar 2018 13:44:26 +0200 Subject: [PATCH 1/3] feat: Allow use of `error` object inside the notifier message/title Related to #7 --- lib/error-notifier.js | 55 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/error-notifier.js b/lib/error-notifier.js index 86a4c49..74efa2e 100644 --- a/lib/error-notifier.js +++ b/lib/error-notifier.js @@ -3,12 +3,65 @@ const notifier = require('node-notifier'); const notifierOptions = require('./notifier-options'); +const errorLiteral = 'error'; + +function errorLiteralPosition(str) { + let index = str.indexOf(`\${${errorLiteral}`); + let length = 0; + + if (index > -1) { + index += 2; + + let open = 1; + + while (length < str.length - index) { + const char = str.charAt(index + length); + + if (char === '{') { + open++; + } else if (char === '}') { + open--; + } + + if (open === 0) { + break; + } + + length++; + } + + if (open > 0) { + throw new TypeError('Missing brace for error literal!'); + } + } + + return {index, length}; +} + +function errorLiteralReplace(err, msg) { + let position; + + while ((position = errorLiteralPosition(msg)) && position.index > -1) { + const lc = msg.substr(position.index, position.length); + const fn = new Function(errorLiteral, `return ${lc}`); // eslint-disable-line no-new-func + msg = msg.replace(`\${${lc}}`, fn(err)); + } + + return msg; +} + module.exports = (command, opts) => { opts = opts || {}; return new Promise((resolve, reject) => { execa.shell(command, {env: {FORCE_COLOR: true}}) .then(result => resolve(result)) - .catch(err => notifier.notify(notifierOptions(opts), () => reject(err))); + .catch(err => { + const notifierOpts = notifierOptions(opts); + notifierOpts.title = errorLiteralReplace(err, notifierOpts.title); + notifierOpts.message = errorLiteralReplace(err, notifierOpts.message); + + return notifier.notify(notifierOpts, () => reject(err)); + }); }); }; From f867466117468feb56260e1f4829173dd4c7e167 Mon Sep 17 00:00:00 2001 From: 0x04 Date: Tue, 10 Sep 2019 22:30:07 +0200 Subject: [PATCH 2/3] feat: Allow use of `error` object inside the notifier message/title Related to #7 --- cli.js | 9 ++++++++- lib/error-notifier.js | 2 +- lib/notifier-options.js | 17 ++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index 1036eff..84ff120 100755 --- a/cli.js +++ b/cli.js @@ -21,7 +21,10 @@ const cli = meow(` Use "mute" to disable default sound notification. Options: Mute, Basso, Blow, Bottle, Frog, Funk, Glass, Hero, Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink - Default: Bottle + Default: Bottle + --error -e A JavaScript code string to customize the content of the + \${error} variable e.g. "error.stderr.substr(0, 9) + '...'" + Default: "error.toString()" --version -v Displays the version number. --help -h Displays the help. @@ -51,6 +54,10 @@ const cli = meow(` sound: { type: 'string', alias: 's' + }, + error: { + type: 'string', + alias: 'e' } } }); diff --git a/lib/error-notifier.js b/lib/error-notifier.js index 9b9d4f8..99377a9 100644 --- a/lib/error-notifier.js +++ b/lib/error-notifier.js @@ -11,6 +11,6 @@ module.exports = (command, opts) => { execaPending.stdout.pipe(process.stdout); execaPending.stderr.pipe(process.stderr); return execaPending.then(result => resolve(result)) - .catch(error => notifier.notify(notifierOptions(opts), () => reject(error))); + .catch(error => notifier.notify(notifierOptions(opts, error), () => reject(error))); }); }; diff --git a/lib/notifier-options.js b/lib/notifier-options.js index de1de4e..1e8368e 100644 --- a/lib/notifier-options.js +++ b/lib/notifier-options.js @@ -1,10 +1,17 @@ -module.exports = (opts) => { - +module.exports = (opts, error) => { + const errorRegExp = /\${error}/g; + // TODO: Implement error handling + const errorHandler = new Function('error', `return eval(${JSON.stringify(opts.e || opts.error || 'error.toString()')})`); // eslint-disable-line no-new-func + const errorReplace = (str) => (error) ? str.replace(errorRegExp, errorHandler(error)) : str; const notifierOpts = { - title: opts.t || opts.title || 'An error has occured', - message: opts.m || opts.message || 'Check the terminal for more information', + title: errorReplace(opts.t || opts.title || 'An error has occured'), + message: errorReplace(opts.m || opts.message || 'Check the terminal for more information'), icon: opts.i || opts.icon || '', - sound: opts.s || opts.sound || true + sound: opts.s || opts.sound || true, + // Defective since Windows 10 1903 Update + // https://github.com/mikaelbr/node-notifier/issues/277 + // `true` is just for testing purpose - should be removed from production code + wait: true }; notifierOpts.sound = /mute/i.test(notifierOpts.sound) ? false : notifierOpts.sound; From a2b5d96891561a76f3d15faa6122853c5c0f1c92 Mon Sep 17 00:00:00 2001 From: 0x04 Date: Sat, 21 Sep 2019 13:40:49 +0200 Subject: [PATCH 3/3] fix: Correct Indention of usage --- cli.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli.js b/cli.js index 84ff120..e40177e 100755 --- a/cli.js +++ b/cli.js @@ -21,10 +21,10 @@ const cli = meow(` Use "mute" to disable default sound notification. Options: Mute, Basso, Blow, Bottle, Frog, Funk, Glass, Hero, Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink - Default: Bottle + Default: Bottle --error -e A JavaScript code string to customize the content of the - \${error} variable e.g. "error.stderr.substr(0, 9) + '...'" - Default: "error.toString()" + \${error} variable e.g. "error.stderr.substr(0, 9) + '...'" + Default: "error.toString()" --version -v Displays the version number. --help -h Displays the help.