Skip to content

Instantly share code, notes, and snippets.

@muddydixon
Created July 30, 2012 18:47
Show Gist options
  • Save muddydixon/3209110 to your computer and use it in GitHub Desktop.
Save muddydixon/3209110 to your computer and use it in GitHub Desktop.
var spawn = require('child_process').spawn
, run = function(cmd, argv, cb) {
var dat, err, proc;
if ((argv != null) && !argv instanceof Array) {
argv = [argv];
}
proc = spawn(cmd, argv);
proc.stdout.setEncoding('utf8');
proc.stderr.setEncoding('utf8');
dat = '';
err = '';
proc.stdout.on('data', function(data) {
dat += data;
});
proc.stderr.on('data', function(data) {
err += data;
});
proc.on('exit', function(code) {
if (err === '') {
err = null;
}
return typeof cb === "function" ? cb(err, dat) : void 0;
});
};
// こんな風にして使う
run('ls', ['-1t'], function(err, files){
console.log(files);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment