Skip to content

Commit

Permalink
Support passing in an array of regexes to logFilter param
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonlehman committed Aug 5, 2016
1 parent 3fcae45 commit 6fbd67b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
28 changes: 25 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ var path = require('path');
var spawn = require('child_process').spawn;
var isFunction = require('lodash.isfunction');
var isNumber = require('lodash.isnumber');
var isArray = require('lodash.isarray');
var some = require('lodash.some');
var debug = require('debug')('yexec');

module.exports = function(params, callback) {
var options = pick(params, 'cwd', 'env');
options.stdio = 'pipe';

var executableBaseName = path.basename(params.executable);

debug('spawning %s %s', params.executable, params.args.join(' '));

var process;
try {
process = spawn(params.executable, params.args, options);
Expand All @@ -18,13 +23,30 @@ module.exports = function(params, callback) {
}

var processExited;
var filter;

// If the
if (isArray(params.logFilter)) {
filter = function(level, msg) {
return !some(params.logFilter, function(pattern) {
return isFunction(pattern.test) && pattern.test(msg);
});
}
} else if (isFunction(params.logFilter)) {
filter = params.logFilter;
} else {
filter = function() {
return true;
}
}

var log = function(level, data) {
if (!params.logger) return;
var msg = data.toString().trim();
if (msg.length === 0) return;
if (isFunction(params.logFilter) && !params.logFilter(level, msg)) return;
params.logger[level](msg);
if (filter(level, msg)) {
params.logger[level](msg);
}
};

// Log stdout to the log as info
Expand All @@ -49,7 +71,7 @@ module.exports = function(params, callback) {
if (processExited) return;
processExited = true;
if (isNumber(code) && code !== 0) {
var error = new Error('Process ' + executableBaseName + ' failed with code');
var error = new Error('Process ' + executableBaseName + ' failed with code ' + code);
error.code = code;
callback(error);
} else {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yexec",
"version": "1.0.2",
"version": "1.0.3",
"description": "Yet another cmd execution wrapper that works the way I like it",
"main": "index.js",
"scripts": {
Expand All @@ -12,9 +12,12 @@
"repository": "https://github.com/dvonlehman/node-yexec",
"license": "ISC",
"dependencies": {
"debug": "^2.2.0",
"lodash.isarray": "^4.0.0",
"lodash.isfunction": "^3.0.8",
"lodash.isnumber": "^3.0.3",
"lodash.pick": "^4.2.0"
"lodash.pick": "^4.2.0",
"lodash.some": "^4.5.1"
},
"devDependencies": {
"istanbul": "^0.4.3",
Expand Down
18 changes: 17 additions & 1 deletion test/yexec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ describe('yexec', function() {
});
});

it('filters logging', function(done) {
it('filters log events passing in array of patterns', function(done) {
var log = new Log();
var params = {
executable: 'node',
args: [path.join(__dirname, './fixtures/success.js')],
logger: log,
logFilter: [/stdout/]
};
yexec(params, function(err) {
if (err) return done(err);
assert.equal(log._info.length, 0);
assert.equal(log._warn.length, 1);
done();
});
});

it('filters logging with function', function(done) {
var log = new Log();
var params = {
executable: 'node',
Expand Down

0 comments on commit 6fbd67b

Please sign in to comment.