Created
January 14, 2016 07:06
-
-
Save deepak/a3f5b7760555d74a17ca to your computer and use it in GitHub Desktop.
do not understand how to pass arguments to node's child_process.spawn
This file contains hidden or 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
"use strict"; | |
const fs = require('fs'), | |
spawn = require('child_process').spawn, | |
fileName = process.argv[2], | |
spawnCmd = process.argv[3], | |
spawnArgs = process.argv.slice(4, process.argv.length); | |
console.log([typeof spawnArgs, spawnArgs]); | |
if (!fileName) { | |
throw Error("need to specify file to watch!"); | |
} | |
// TODO: does not handle deletes | |
fs.watch(fileName, function() { | |
console.log("file '"+ fileName + "' just changed"); | |
// works | |
// const ls = spawn('ls', ['-lh', fileName]); | |
// but this does not work. here | |
// works on the bash terminal | |
// nor does it error out. frustrating | |
// const ls = spawn(spawnCmd, ['-l -h', fileName]); | |
// this works. | |
// so it is a special array then :-) | |
// const ls = spawn(spawnCmd, ['-l', '-h', fileName]); | |
// why ? bug ? | |
// or this is just POSIX | |
// and bash has added a feature to make it work ? | |
const ls = spawn(spawnCmd, spawnArgs.concat([fileName])); | |
ls.stdout.pipe(process.stdout); | |
let readStream = fs.createReadStream(fileName) | |
readStream.pipe(process.stdout); | |
// will not throw error if file is deleted | |
readStream.on('error', function(err) { | |
console.log(err); | |
}); | |
}); | |
console.log("now watching '"+ fileName + "' for changes"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment