mirror of
https://github.com/samsonjs/NorthWatcher.git
synced 2026-03-30 10:16:06 +00:00
Compare commits
10 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0452df8944 | ||
|
|
e1ea5e39b7 | ||
|
|
a9f0908e1f | ||
|
|
4f7823d11a | ||
|
|
4da17cbfcc | ||
|
|
f0bf25fa79 | ||
|
|
18215d0a79 | ||
|
|
62c3256d3e | ||
|
|
e6cb31244a | ||
|
|
72993faa34 |
3 changed files with 64 additions and 31 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules
|
||||||
|
*.tmproj
|
||||||
|
|
@ -15,11 +15,30 @@
|
||||||
// - *maybe* a minimum interval at which commands are run, e.g. at most once every 5 minutes
|
// - *maybe* a minimum interval at which commands are run, e.g. at most once every 5 minutes
|
||||||
|
|
||||||
var fs = require('fs')
|
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() {
|
function main() {
|
||||||
var path = require('path')
|
var path = require('path')
|
||||||
, eachLine = require('batteries').FileExt.eachLine
|
, 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) {
|
fs.stat(rcFile, function(err, s) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
@ -35,17 +54,17 @@ function main() {
|
||||||
|
|
||||||
// <trigger> <dir> <command>
|
// <trigger> <dir> <command>
|
||||||
var m
|
var m
|
||||||
if (m = line.match(/^([-+]{0,2})\s*(\S+)\s*(.+)$/i)) {
|
if (m = line.match(/^([-+]{0,2})\s*(?:"([^"]+)"|(\S+))\s+(.+)$/i)) {
|
||||||
var trigger = m[1] || '+-' // default watches for both
|
var trigger = m[1] || '+-' // default watches for both
|
||||||
, dir = m[2]
|
, dir = m[2] || m[3]
|
||||||
, command = m[3]
|
, command = m[4]
|
||||||
, options = { create: trigger.indexOf('+') !== -1
|
, options = { create: trigger.indexOf('+') !== -1
|
||||||
, remove: trigger.indexOf('-') !== -1
|
, remove: trigger.indexOf('-') !== -1
|
||||||
, command: command
|
, command: command
|
||||||
}
|
}
|
||||||
if (dir.charAt(0) !== '/') dir = path.resolve(process.env.HOME, dir)
|
if (dir.charAt(0) !== '/') dir = path.resolve(process.env.HOME, dir)
|
||||||
ensureDirectory(dir)
|
ensureDirectory(dir)
|
||||||
console.log('>>> watch ' + line)
|
log('>>> watch ' + line)
|
||||||
watch(dir, options)
|
watch(dir, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,7 +111,7 @@ function watcherForDir(dir, options) {
|
||||||
, c = unset(setDiff(newFiles, files[dir]))
|
, c = unset(setDiff(newFiles, files[dir]))
|
||||||
, r = unset(setDiff(files[dir], newFiles))
|
, r = unset(setDiff(files[dir], newFiles))
|
||||||
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) {
|
watchedDirs[dir].forEach(function(o) {
|
||||||
if (c.length > 0 && o.create || r.length > 0 && o.remove) {
|
if (c.length > 0 && o.create || r.length > 0 && o.remove) {
|
||||||
runCommand({ command: o.command
|
runCommand({ command: o.command
|
||||||
|
|
@ -106,7 +125,7 @@ function watcherForDir(dir, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function runCommand(options) {
|
function runCommand(options) {
|
||||||
console.log('>>> running command: ' + options.command)
|
log('>>> running command: ' + options.command)
|
||||||
var spawn = require('child_process').spawn
|
var spawn = require('child_process').spawn
|
||||||
// TODO quoting
|
// TODO quoting
|
||||||
, args = options.command.split(/\s+/)
|
, args = options.command.split(/\s+/)
|
||||||
|
|
@ -162,7 +181,9 @@ function spiel() {
|
||||||
, " - todo update-remote-todo-list.sh"
|
, " - todo update-remote-todo-list.sh"
|
||||||
, ""
|
, ""
|
||||||
, "So each line has an optional trigger followed by a directory and"
|
, "So each line has an optional trigger followed by a directory and"
|
||||||
, "then a command. The triggers are:"
|
, "then a command. The directory can be quoted if it contains spaces."
|
||||||
|
, ""
|
||||||
|
, "The triggers are:"
|
||||||
, ""
|
, ""
|
||||||
, " + files are created"
|
, " + files are created"
|
||||||
, " - files are removed"
|
, " - files are removed"
|
||||||
|
|
@ -173,7 +194,7 @@ function spiel() {
|
||||||
, "No frills."
|
, "No frills."
|
||||||
, ""
|
, ""
|
||||||
, "Create a watch file and then run NorthWatcher again."
|
, "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()
|
if (require.main === module) main()
|
||||||
52
package.json
52
package.json
|
|
@ -1,23 +1,33 @@
|
||||||
{ "name" : "northwatcher"
|
{
|
||||||
, "description" : "NorthWatcher is cron for filesystem changes."
|
"name": "northwatcher",
|
||||||
, "version" : "0.1.4"
|
"description": "NorthWatcher is cron for filesystem changes.",
|
||||||
, "homepage" : "http://samhuri.net/proj/NorthWatcher"
|
"version": "0.2.0",
|
||||||
, "author" : "Sami Samhuri <sami.samhuri@gmail.com>"
|
"homepage": "http://samhuri.net/proj/NorthWatcher",
|
||||||
, "repository" :
|
"author": "Sami Samhuri <sami@samhuri.net>",
|
||||||
{ "type" : "git"
|
"repository": {
|
||||||
, "url" : "http://github.com/samsonjs/NorthWatcher.git"
|
"type": "git",
|
||||||
}
|
"url": "git://github.com/samsonjs/NorthWatcher.git"
|
||||||
, "bugs" :
|
},
|
||||||
{ "mail" : "sami.samhuri@gmail.com"
|
"bugs": {
|
||||||
, "web" : "http://github.com/samsonjs/NorthWatcher/issues"
|
"email": "sami.samhuri@gmail.com",
|
||||||
}
|
"url": "http://github.com/samsonjs/NorthWatcher/issues"
|
||||||
, "bin" : { "northwatcher" : "./northwatcher.js" }
|
},
|
||||||
, "main" : "./northwatcher.js"
|
"bin": {
|
||||||
, "engines" : { "node" : ">=0.4.0" }
|
"northwatcher": "./northwatcher.js"
|
||||||
, "licenses" :
|
},
|
||||||
[ { "type" : "MIT"
|
"main": "./northwatcher.js",
|
||||||
, "url" : "http://github.com/samsonjs/NorthWatcher/raw/master/LICENSE"
|
"engines": {
|
||||||
|
"node": ">=0.4.x"
|
||||||
|
},
|
||||||
|
"licenses": [
|
||||||
|
{
|
||||||
|
"type": "MIT",
|
||||||
|
"url": "http://github.com/samsonjs/NorthWatcher/raw/master/LICENSE"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
, "dependencies" : { "batteries" : "0.2.x" }
|
"dependencies": {
|
||||||
|
"batteries": "0.4.x",
|
||||||
|
"optimist": "0.2.x"
|
||||||
|
},
|
||||||
|
"devDependencies": {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue