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

feat: Allow use of error object inside the notifier message/title (WIP) #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -51,6 +54,10 @@ const cli = meow(`
sound: {
type: 'string',
alias: 's'
},
error: {
type: 'string',
alias: 'e'
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/error-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
});
};
17 changes: 12 additions & 5 deletions lib/notifier-options.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down