-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (141 loc) · 4.32 KB
/
index.js
File metadata and controls
157 lines (141 loc) · 4.32 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* filesync
*
* Copyright © 2015 Jan Loose (hellpf), All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var chokidar = require('chokidar'),
fs = require('fs'),
path = require('path'),
ncp = require('ncp'),
rmdir = require('rmdir'),
stdio = require('stdio');
var ops = stdio.getopt({
'source': {
key: 's',
args: 1,
description: 'Source directory',
mandatory: true
},
'target': {
key: 't',
args: 1,
description: 'Target diectory or directories (sepearated by a semicolon)',
mandatory: true
},
'syncDelete': {
key: 'd',
args: 0,
description: 'Sync delete changes?',
mandatory: false
}
});
var sourceDir = ops.source,
targetDirs = ops.target.split(';'),
syncDelete = ops.syncDelete;
var calcDir = function(dir) {
if (dir.indexOf('/') !== 0) {
dir = path.join(process.env.PWD, dir);
}
return dir;
};
sourceDir = calcDir(sourceDir);
for (var i = 0; i < targetDirs.length; i++) {
targetDirs[i] = calcDir(targetDirs[i]);
}
var eachDir = function(dir, callback) {
var dirs = dir.split('/').splice(1);
for (var i = 1; i < dirs.length; i++) {
callback('/' + path.join.apply(this, dirs.slice(0, i)), i === dirs.length - 1);
}
};
var stop = function() {
eachDir(sourceDir, function(dir, last) {
fs.unwatchFile(dir);
if (last) {
stopWatchingContent();
}
});
};
var stopWatchingContent = function() {
if (watcher) {
watcher.unwatch(sourceDir);
console.log('unwatched ' + sourceDir);
watcher = undefined;
}
};
var start = function() {
eachDir(sourceDir, function(dir, last) {
fs.watchFile(dir, function() {
fs.stat(sourceDir, function(err) {
if (err) {
if (watcher) {
stop();
setTimeout(existTest, 0);
}
}
});
});
if (last) {
startWatchingContent();
}
});
};
var watcher;
var startWatchingContent = function() {
var ready = false;
watcher = chokidar.watch(sourceDir, { ignored: /[\/\\]\./ });
var done = function(msg) {
console.log(msg);
};
watcher.on('all', function(event, path) {
if (!ready) {
return;
}
targetDirs.forEach(function(targetDir) {
var destination = targetDir + path.substring(sourceDir.length);
var displayedDestination = path.substring(sourceDir.length);
displayedDestination = displayedDestination ? displayedDestination : '.';
if (event == 'add' || event == 'change') {
console.log('copying file ' + displayedDestination);
ncp(path, destination, done.bind(this, 'copied file ' + displayedDestination));
} else if (event == 'unlink') {
if (syncDelete) {
console.log('deleting file ' + displayedDestination);
fs.unlink(destination, done.bind(this, 'deleted file ' + displayedDestination));
}
} else if (event == 'addDir') {
console.log('copying directory ' + displayedDestination);
ncp(path, destination, done.bind(this, 'copied directory ' + displayedDestination));
} else if (event == 'unlinkDir') {
if (syncDelete) {
console.log('deleting directory ' + displayedDestination);
rmdir(destination, done.bind(this, 'deleted directory ' + displayedDestination));
}
} else {
console.warn('unknown state: ' + event + ' ' + path);
}
});
});
watcher.on('ready', function() {
console.log('watching ' + sourceDir);
ready = true;
});
};
var existTest = function() {
fs.stat(sourceDir, function(err) {
if (!err) {
start();
} else {
if (err.code == 'ENOENT') {
console.error(err.path + ' doesn\'t exist.');
} else {
console.error(err);
}
setTimeout(existTest, 1000);
}
});
};
existTest();