-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (124 loc) · 3.83 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const CREATE_DEBUG_FILES = false;
const fs = require('fs');
const path = require('path');
const execSync = require('child_process').execSync;
const glob = require('glob');
const _ = require('underscore');
var config = require(path.join(process.cwd(), process.argv[2]));
if (!config.sets) {
config = {
before : config.before,
sets : [ config ]
};
delete config.sets.before;
}
// Commands to run once before the watch
config.before = config.before || [];
if (typeof config.before === 'string') {
config.before = [ config.before ];
}
_.each(config.sets, (set) => {
if (typeof set.include === 'string') {
set.include = [ set.include ];
}
if (typeof set.exclude === 'string') {
set.exclude = [ set.exclude ];
}
if (typeof set.commands === 'string') {
set.commands = [ set.commands ];
}
set.commands = set.commands || [];
if (set.command) {
set.commands.push(set.command);
}
set.root = set.root || process.cwd();
});
if (config.before.length) {
console.log('Running initial commands...');
_.each(config.before, (cmd) => {
try {
console.log(' ' + cmd);
let parts = cmd.split
let child = require('child_process').exec(cmd);
child.stdout.on('data', (data) => {
console.log(`${data}`.replace(/\n$/, ''));
});
child.stderr.on('data', (data) => {
console.log(`${data}`.replace(/\n$/, ''));
});
} catch (_ignored) { /* ignored */ }
});
}
var commands = {};
var scanList = [];
var doneList = [];
var horizon = Date.now();
rescanFiles();
console.log('Starting file watch...');
poll();
function rescanFiles() {
console.log('Scanning files...');
_.each(config.sets, (set) => {
_.each(set.include, (pattern) => {
var list = glob.sync(pattern, {
nodir : true,
ignore : set.exclude,
});
_.each(list, (file) => {
commands[file] = commands[file] || [];
commands[file].push([ set.commands, set.root ]);
});
});
});
scanList = _.keys(commands);
doneList = [];
console.log('Found '+scanList.length+' files...');
var byExt = _.groupBy(scanList, (file) => {
return path.extname(file).substr(1);
});
_.each(byExt, function(list, ext) {
console.log(' ' + list.length + ' ' + ext + ' files');
});
if (CREATE_DEBUG_FILES) {
fs.writeFileSync("watch-trigger.status.json", JSON.stringify({
commands : commands,
}, null, 4));
}
}
function updateTimestamp(filename) {
var now = fs.statSync(filename).mtime.valueOf();
return (now > horizon);
}
function poll() {
if (scanList.length === 0) {
scanList = doneList;
doneList = [];
}
var filename = scanList.shift();
doneList.push(filename);
if (!fs.existsSync(filename)) {
rescanFiles();
setTimeout(poll, 200);
} else if (updateTimestamp(filename)) {
console.log();
console.log('[WATCH] File modification detected: ' + filename);
_.each(commands[filename], function (group) {
_.each(group[0], function (cmd) {
// Replace $1 with the filename
var base = filename.replace(/^\.\//, '');
var rel = path.relative(group[1], base).replace(/\\/g, "/");
cmd = cmd.replace(/\$1/g, base).replace(/\$2/g, rel);
console.log('[WATCH] ' + cmd);
console.log();
try {
execSync(cmd, { stdio: 'inherit' });
} catch (_ignored) { /* ignored */ }
});
});
horizon = Date.now();
setTimeout(poll, 200);
} else {
setTimeout(poll, 20);
}
}