javascript - Shell command execution -
i'm trying implement shell command execution found here: node.js shell command execution
so i'm trying second answer (by chris eineke) working. no output.
here try:
run_cmd = (cmd, args, done) -> spawn = require('child_process').spawn child = spawn(cmd, args) result = { stdout: '' }; child.stdout.on \data ! (buffer) -> result.stdout += buffer child.stdout.on \end !-> done() result dir = run_cmd( 'ls', ['-a'] , !-> console.log('done') ) console.log dir.stdout
it compiles to:
run_cmd = function(cmd, args, done){ var spawn, child, result; spawn = require('child_process').spawn; child = spawn(cmd, args); result = { stdout: '' }; child.stdout.on('data', function(buffer){ result.stdout += buffer; }); child.stdout.on('end', function(){ done(); }); return result; }; dir = run_cmd('ls', ['-a'], function(){ console.log('done'); }); console.log(dir.stdout);
but can't see ls
results. mistake? or doing wrong?
you're not seeing results because run_cmd runs async , result empty when console.log dir.stdout
ran. should rather pass results callback , logging there. here's working livescript version:
run_cmd = !(cmd, args, done) -> spawn = require 'child_process' .spawn child = spawn cmd, args result = stdout: '' child.stdout ..on \data !(buffer) -> result.stdout += buffer ..on \end !-> done result <-! run_cmd \ls <[ -a ]> console ..log \done ..log it.stdout
here see multiple ls features used paren-free chaining (spawn
line), braceless object definition (result
line), cascades (the ..on
, ..log
lines), backcalls (<-!
), word arrays (<[ array of words ]>
) , implicit argument (it
). more info on them, refer livescript documentation.
Comments
Post a Comment