-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (50 loc) · 1.78 KB
/
index.js
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
#!/usr/bin/env node
var program = require('commander'),
fs = require('graceful-fs'),
path = require('path'),
packageJson = require('./package.json'),
gvc = require('./gvc'),
watchPath = path.join(process.cwd(), 'pages'),
outputPath = path.join(process.cwd(), 'pages'),
throttle = 300,
throttleTimeout;
program._name = 'gvc';
program
.version(packageJson.version)
.option('-v, --verbose', 'Verbose output')
.option('-w, --watch [path]', 'Watch Path [default' + watchPath + ']', watchPath)
.option('-o, --output [path]', 'Output Path [default' + outputPath + ']', outputPath)
.option('-T, --throttle [milliseconds]', 'Minimum time between processing (milliseconds) [default ' + throttle +']', Number, throttle)
.parse(process.argv);
function log (message) {
if (program.verbose) {
console.log(message);
}
}
function tryToProcess(filename){
var now = new Date();
clearTimeout(throttleTimeout);
throttleTimeout = setTimeout(function(){
processFile(filename);
}, program.throttle);
}
function processFile(filename) {
log(filename + ' has changed. Recompiling...');
try{
gvc.parse(path.resolve(path.join(watchPath, filename)), outputPath, function (error, result) {
if(error){
return console.log(error.stack || error);
}
log(filename + ' -> ' + path.basename(filename, path.extname(filename)) + '.json');
});
} catch(exception) {
console.log(exception.stack || exception);
}
}
log('Watching ' + watchPath + ' for changes.');
fs.watch(watchPath, function (eventType, filename) {
if (eventType !== 'change' || path.extname(filename).toLowerCase() !== '.js') {
return;
}
tryToProcess(filename);
});