-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
80 lines (73 loc) · 2.37 KB
/
Copy pathcli.js
File metadata and controls
80 lines (73 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
var fs = require('fs');
var util = require('util');
var path = require('path');
var walk = require('events');
var watch = require('./main.js');
var exec = require('child_process').exec;
var argv = require('optimist').usage('Watch files in a file tree.').wrap(80).option('directory', {
alias: 'd',
desc: 'Define the root directory to watch, if this is not defined the program will use the current working directory.'
}).option('created', {
alias: 'c',
desc: 'Command to handle created files'
}).option('modified', {
alias: 'm',
desc: 'Command to handle modified files'
}).option('removed', {
alias: 'r',
desc: 'Command to handle deleted files'
}).option('all', {
alias: 'a',
desc: 'Command to handle all file changes'
}).option('interval', {
alias: 'i',
desc: 'Watcher interval'
}).option('help', {
alias: 'h',
desc: 'Show this message'
}).
default('interval', 150).
default('directory', process.cwd()).check(function(argv) {
// console.log(argv);
if(!(argv.modified || argv.removed || argv.created || argv.all)){
throw '';
}
if(argv.help) {
throw '';
}
if(argv.all) {
argv.created = argv.modified = argv.removed = argv.all;
}
}).argv;
var wrapper = function(action, cmd) {
return function(file) {
var command = (cmd || '');
command = command.replace(/\$basename/g, path.basename(file, path.extname(file)));
command = command.replace(/\$filename/g, path.basename(file));
command = command.replace(/\$file/g, file);
command = command.replace(/\$action/g, action);
command = command.replace(/\$relative/g, path.relative(argv.directory, file));
exec(command, function(err, stdout, stderr) {
if(err !== null) {
console.log('command error: ' + err);
process.exit(1);
} else {
util.puts(stdout);
}
});
};
};
watch.monitor(argv.directory, {
interval: argv.interval
}, function(monitor) {
if(argv.created) {
monitor.on('created', wrapper('created', argv.created));
}
if(argv.modified) {
monitor.on('changed', wrapper('changed', argv.modified));
}
if(argv.removed) {
monitor.on('removed', wrapper('removed', argv.removed));
}
});