mirror of
https://github.com/samsonjs/NorthWatcher.git
synced 2026-04-02 10:45:54 +00:00
Compare commits
No commits in common. "master" and "v0.1.5" have entirely different histories.
3 changed files with 13 additions and 37 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,2 +0,0 @@
|
||||||
node_modules
|
|
||||||
*.tmproj
|
|
||||||
|
|
@ -15,30 +15,11 @@
|
||||||
// - *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').fs.eachLine
|
, eachLine = require('batteries').fs.eachLine
|
||||||
, rcFile = argv._.shift() || require('path').join(process.env.HOME, '.northwatcher')
|
, rcFile = require('path').join(process.env.HOME, '.northwatcher')
|
||||||
|
|
||||||
fs.stat(rcFile, function(err, s) {
|
fs.stat(rcFile, function(err, s) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
@ -54,17 +35,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] || m[3]
|
, dir = m[2]
|
||||||
, command = m[4]
|
, command = m[3]
|
||||||
, 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)
|
||||||
log('>>> watch ' + line)
|
console.log('>>> watch ' + line)
|
||||||
watch(dir, options)
|
watch(dir, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,7 +92,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
|
||||||
log('>>> ' + dir + ' changed! c: ' + JSON.stringify(c) + ' r: ' + JSON.stringify(r))
|
console.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
|
||||||
|
|
@ -125,7 +106,7 @@ function watcherForDir(dir, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function runCommand(options) {
|
function runCommand(options) {
|
||||||
log('>>> running command: ' + options.command)
|
console.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+/)
|
||||||
|
|
@ -181,9 +162,7 @@ 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 directory can be quoted if it contains spaces."
|
, "then a command. The triggers are:"
|
||||||
, ""
|
|
||||||
, "The triggers are:"
|
|
||||||
, ""
|
, ""
|
||||||
, " + files are created"
|
, " + files are created"
|
||||||
, " - files are removed"
|
, " - files are removed"
|
||||||
|
|
@ -194,7 +173,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) { log(s) })
|
].forEach(function(s) { console.log(s) })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (require.main === module) main()
|
if (require.main === module) main()
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "northwatcher",
|
"name": "northwatcher",
|
||||||
"description": "NorthWatcher is cron for filesystem changes.",
|
"description": "NorthWatcher is cron for filesystem changes.",
|
||||||
"version": "0.2.0",
|
"version": "0.1.5",
|
||||||
"homepage": "http://samhuri.net/proj/NorthWatcher",
|
"homepage": "http://samhuri.net/proj/NorthWatcher",
|
||||||
"author": "Sami Samhuri <sami@samhuri.net>",
|
"author": "Sami Samhuri <sami@samhuri.net>",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
},
|
},
|
||||||
"main": "./northwatcher.js",
|
"main": "./northwatcher.js",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.4.x"
|
"node": "0.4.x"
|
||||||
},
|
},
|
||||||
"licenses": [
|
"licenses": [
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +26,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"batteries": "0.4.x",
|
"batteries": "0.4.x"
|
||||||
"optimist": "0.2.x"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {}
|
"devDependencies": {}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue