diff --git a/cli.js b/cli.js index 1036eff..e40177e 100755 --- a/cli.js +++ b/cli.js @@ -22,6 +22,9 @@ const cli = meow(` Options: Mute, Basso, Blow, Bottle, Frog, Funk, Glass, Hero, Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink 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;