-
Notifications
You must be signed in to change notification settings - Fork 3
/
watch.js
executable file
·100 lines (87 loc) · 2.38 KB
/
watch.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
var async = require('async');
var chokidar = require('chokidar');
var osenv = require('osenv');
var cp = require('child_process');
var nconf = require('nconf');
var debounce = require('async-debounce');
var docker_ip;
nconf.env().argv().file('bdsync.json');
nconf.defaults({
'targetPath': process.cwd(),
'ignoreFile' : '.gitignore'
});
function getdockerip (cb) {
cp.exec('boot2docker ip 2>/dev/null', function (err, stdout) {
if (err) {
return cb(err);
}
docker_ip = stdout.trim();
cb();
});
}
function install_rsync (cb) {
console.log("Installing rsync");
cp.exec('boot2docker ssh "tce-load -wi rsync"', function (err, stdout) {
cb();
});
}
function rsync (cb) {
console.log('Syncing files...');
var child = cp.spawn('rsync', [
'-av',
'--rsh=ssh -i ' + osenv.home() + '/.ssh/id_boot2docker -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no',
'--delete',
process.cwd() + '/',
'--exclude-from',
nconf.get('ignoreFile'),
'docker@' + docker_ip + ':' + nconf.get('targetPath')
]);
child.stderr.on('data', function (data) {
console.error(data.toString());
});
child.stdout.on('data', function (data) {
console.log(data.toString());
});
child.on('exit', function () {
console.log('Syncing complete.');
cb();
});
}
function umount (cb) {
console.log("Unmounting /Users");
cp.exec('boot2docker ssh "sudo umount /Users || /bin/true"', function() {
cb();
});
}
function ntpsync (cb) {
console.log("Syncing NTP");
cp.exec('boot2docker ssh "sudo timeout -t 3 ntpclient -s -h us.pool.ntp.org"', function() {
cb();
});
}
function mkdirp (cb) {
console.log("Making target path.");
cp.exec('boot2docker ssh "sudo mkdir -p ' + nconf.get('targetPath') + ' && sudo chown -R docker:staff ' + nconf.get('targetPath') + '"', function() {
cb();
});
}
console.log('Sync with ' + nconf.get('targetPath') + ", exclude from " + nconf.get('ignoreFile'));
// Settles down a function, until there's a 100ms pause.
function settle(fn) {
var lastTimeout;
return function(e, d) {
clearTimeout(lastTimeout);
lastTimeout = setTimeout(fn, 100);
};
}
async.series([
umount,
ntpsync,
mkdirp,
getdockerip,
install_rsync,
rsync
], function (err) {
var watcher = chokidar.watch(process.cwd(), { persistent: true, ignoreInitial: true });
watcher.on('all', settle(debounce(rsync, 500)));
});