From 62c3256d3eb8c9d8409363525d4281772d09b4ef Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 12 Jun 2011 19:01:40 -0700 Subject: [PATCH] add command line options for rcfile and logfile --- .gitignore | 2 ++ northwatcher.js | 29 ++++++++++++++++++++++++----- package.json | 1 + 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf117f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +*.tmproj diff --git a/northwatcher.js b/northwatcher.js index 0b4ec96..2078dbc 100755 --- a/northwatcher.js +++ b/northwatcher.js @@ -15,11 +15,30 @@ // - *maybe* a minimum interval at which commands are run, e.g. at most once every 5 minutes var fs = require('fs') + , argv = require('optimist') + .alias('l', 'log') + .default('l', process.env.NORTHWATCHER_LOG) + .argv + , log + +if (argv.log) { + var logStream = fs.createWriteStream(argv.log, { + flags: 'a' + , mode: 0644 + }) + log = function (s) { + logStream.write(s + '\n') + } + process.on('exit', logStream.end.bind(logStream)) +} +else { + log = console.log +} function main() { var path = require('path') , eachLine = require('batteries').fs.eachLine - , rcFile = require('path').join(process.env.HOME, '.northwatcher') + , rcFile = argv._.shift() || require('path').join(process.env.HOME, '.northwatcher') fs.stat(rcFile, function(err, s) { if (err) { @@ -45,7 +64,7 @@ function main() { } if (dir.charAt(0) !== '/') dir = path.resolve(process.env.HOME, dir) ensureDirectory(dir) - console.log('>>> watch ' + line) + log('>>> watch ' + line) watch(dir, options) } @@ -92,7 +111,7 @@ function watcherForDir(dir, options) { , c = unset(setDiff(newFiles, files[dir])) , r = unset(setDiff(files[dir], newFiles)) files[dir] = newFiles - console.log('>>> ' + dir + ' changed! c: ' + JSON.stringify(c) + ' r: ' + JSON.stringify(r)) + log('>>> ' + dir + ' changed! c: ' + JSON.stringify(c) + ' r: ' + JSON.stringify(r)) watchedDirs[dir].forEach(function(o) { if (c.length > 0 && o.create || r.length > 0 && o.remove) { runCommand({ command: o.command @@ -106,7 +125,7 @@ function watcherForDir(dir, options) { } function runCommand(options) { - console.log('>>> running command: ' + options.command) + log('>>> running command: ' + options.command) var spawn = require('child_process').spawn // TODO quoting , args = options.command.split(/\s+/) @@ -173,7 +192,7 @@ function spiel() { , "No frills." , "" , "Create a watch file and then run NorthWatcher again." - ].forEach(function(s) { console.log(s) }) + ].forEach(function(s) { log(s) }) } if (require.main === module) main() \ No newline at end of file diff --git a/package.json b/package.json index 229f6a2..924176f 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ ], "dependencies": { "batteries": "0.4.x" + , "optimist": "0.2.x" }, "devDependencies": {} } \ No newline at end of file