-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-scripts.js
66 lines (61 loc) · 1.97 KB
/
update-scripts.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
fs = require('fs');
path = require('path');
var walk = function(dir, filter, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (filter(file, stat)) {
results.push(file);
}
if (stat && stat.isDirectory()) {
walk(file, filter, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
if (!--pending) done(null, results);
}
});
});
});
};
walk('.', function(file, stat){
if (file=='./scripts') return false;
return stat.isDirectory() && path.basename(file).toLowerCase()==='scripts';
}, function(error, scriptFolders){
if (error){
console.log(error);
} else {
var count = 0;
console.log('\nscript sources:');
console.log(scriptFolders);
console.log();
fs.readdir('./scripts', function(err, scripts){
if (!err) {
scripts.forEach(function(script) {
var scriptPath = path.join('./scripts', script);
scriptFolders.forEach(function(folder){
var source = path.join(folder, path.basename(script));
if (fs.existsSync(source)){
var sourceStat = fs.statSync(source);
var scriptStat = fs.existsSync(scriptPath) && fs.statSync(scriptPath);
if (sourceStat.isFile() && (!scriptStat || sourceStat.mtime > scriptStat.mtime || process.argv.indexOf('--force') > -1)) {
console.log('updating '+path.basename(script)+' from '+folder);
fs.writeFileSync(scriptPath, fs.readFileSync(source));
count++;
}
}
});
});
if (count === 0) {
console.log('Everything up-to-date');
}
}
});
}
});