Skip to content

Commit

Permalink
feat: Allow use of error object inside the notifier message/title
Browse files Browse the repository at this point in the history
Related to micromata#7
  • Loading branch information
0x04 committed Mar 29, 2018
1 parent a33fd60 commit 74ed0c2
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion lib/error-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
};

0 comments on commit 74ed0c2

Please sign in to comment.