Skip to content

Commit f867466

Browse files
committed
feat: Allow use of error object inside the notifier message/title
Related to micromata#7
1 parent 471dfc2 commit f867466

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

cli.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const cli = meow(`
2121
Use "mute" to disable default sound notification.
2222
Options: Mute, Basso, Blow, Bottle, Frog, Funk, Glass, Hero,
2323
Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink
24-
Default: Bottle
24+
Default: Bottle
25+
--error -e A JavaScript code string to customize the content of the
26+
\${error} variable e.g. "error.stderr.substr(0, 9) + '...'"
27+
Default: "error.toString()"
2528
--version -v Displays the version number.
2629
--help -h Displays the help.
2730
@@ -51,6 +54,10 @@ const cli = meow(`
5154
sound: {
5255
type: 'string',
5356
alias: 's'
57+
},
58+
error: {
59+
type: 'string',
60+
alias: 'e'
5461
}
5562
}
5663
});

lib/error-notifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ module.exports = (command, opts) => {
1111
execaPending.stdout.pipe(process.stdout);
1212
execaPending.stderr.pipe(process.stderr);
1313
return execaPending.then(result => resolve(result))
14-
.catch(error => notifier.notify(notifierOptions(opts), () => reject(error)));
14+
.catch(error => notifier.notify(notifierOptions(opts, error), () => reject(error)));
1515
});
1616
};

lib/notifier-options.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
module.exports = (opts) => {
2-
1+
module.exports = (opts, error) => {
2+
const errorRegExp = /\${error}/g;
3+
// TODO: Implement error handling
4+
const errorHandler = new Function('error', `return eval(${JSON.stringify(opts.e || opts.error || 'error.toString()')})`); // eslint-disable-line no-new-func
5+
const errorReplace = (str) => (error) ? str.replace(errorRegExp, errorHandler(error)) : str;
36
const notifierOpts = {
4-
title: opts.t || opts.title || 'An error has occured',
5-
message: opts.m || opts.message || 'Check the terminal for more information',
7+
title: errorReplace(opts.t || opts.title || 'An error has occured'),
8+
message: errorReplace(opts.m || opts.message || 'Check the terminal for more information'),
69
icon: opts.i || opts.icon || '',
7-
sound: opts.s || opts.sound || true
10+
sound: opts.s || opts.sound || true,
11+
// Defective since Windows 10 1903 Update
12+
// https://github.com/mikaelbr/node-notifier/issues/277
13+
// `true` is just for testing purpose - should be removed from production code
14+
wait: true
815
};
916
notifierOpts.sound = /mute/i.test(notifierOpts.sound) ? false : notifierOpts.sound;
1017

0 commit comments

Comments
 (0)