-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncExternals.js
40 lines (34 loc) · 1.22 KB
/
syncExternals.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
const cp = require('child_process');
const chokidar = require('chokidar');
const syncExternalsList = require('./syncExternalsList.js').syncExternalsList;
let bounceTimer = null;
chokidar.watch('.', {ignored: /node_modules|(^|[\/\\])\../}).on('all', (event, path) => {
console.log('change', event, path);
if (bounceTimer) clearTimeout(bounceTimer);
bounceTimer = setTimeout(() => {
bounceTimer = null;
syncFolders();
}, 100);
});
const createSyncScript = folder => `rsync -av --progress ./* ${folder} --exclude node_modules`;
function syncFolders() {
syncExternalsList
.map(path => {
if (path.indexOf('*tus*') === 0) {
path = '/mnt/' + path[5].toLowerCase() + path.substr(7).replace(/\\/g, '/')
}
return path;
})
.forEach(syncFolder);
}
function syncFolder(folder) {
// help: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
cp.exec(createSyncScript(folder), {}, (error, stdout, stderr) => {
if (stdout.toString()) console.log('info', stdout.toString());
if (stderr.toString()) console.log('errors', stderr.toString());
if (error)
console.log('Sync error', error);
else
console.log('success');
});
}