Created
May 28, 2013 16:29
-
-
Save popomore/5664045 to your computer and use it in GitHub Desktop.
shell redirect in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var spawn = require('child_process').spawn; | |
function run(cmd) { | |
var output, out, index = cmd.indexOf('>'); | |
if (index > -1) { | |
output = cmd.substring(index + 1).replace(/(^\s+|\s+$)/g, ''); | |
cmd = cmd.substring(0, index).split(/\s+/); | |
} else { | |
cmd = cmd.split(/\s+/); | |
} | |
if (output) { | |
out = fs.openSync(path.resolve(output), 'a'); | |
} | |
spawn(cmd[0], cmd.slice(1), {stdio: [ | |
process.stdin, | |
out ? out : process.stdout, | |
process.stderr | |
]}); | |
} | |
run('echo hello > a.html'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment