forked from maksrom/javascript-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir-sync.js
More file actions
98 lines (88 loc) · 2.52 KB
/
dir-sync.js
File metadata and controls
98 lines (88 loc) · 2.52 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
(function() {
var RED, RESET, chokidar, compareMtime, fs, gutil, path, traceError;
chokidar = require('chokidar');
fs = require('fs-extra');
path = require('path');
gutil = require('gulp-util');
RED = '\u001b[31m';
RESET = '\u001b[0m';
traceError = function() {
var args;
args = Array.prototype.slice.apply(arguments);
if (typeof args[0] === 'string') {
args[0] = RED + args[0];
} else {
args.unshift(RED);
}
if (typeof args[args.length - 1] === 'string') {
args[args.length - 1] = args[args.length - 1] + RESET;
} else {
args.push(RESET);
}
return console.error.apply(console, args);
};
compareMtime = function(src, target) {
var isNewer;
isNewer = true;
if (fs.existsSync(target)) {
isNewer = fs.statSync(src).mtime > fs.statSync(target).mtime;
}
return isNewer;
};
module.exports = function(fromDir, toDir, opts) {
var filter, watcher;
if (opts == null) {
opts = {};
}
filter = opts.filter;
this._keepalive = setInterval((function() {}), 500);
watcher = chokidar.watch(fromDir, {
persistent: true
});
return watcher.on('all', function(event, srcFile) {
var destFile;
if (filter && !srcFile.match(filter)) {
return;
}
if (path.basename(srcFile) === '.DS_Store') {
return;
}
if (fromDir === srcFile) {
return;
}
destFile = path.join(toDir, path.relative(fromDir, srcFile));
if (event === 'addDir') {
if (!fs.existsSync(destFile)) {
gutil.log(gutil.colors.green("mkdir: " + destFile));
fs.mkdirSync(destFile);
}
}
if (event === 'add') {
if (!fs.existsSync(destFile)) {
gutil.log(gutil.colors.green("add: " + srcFile + " > " + destFile));
fs.copySync(srcFile, destFile);
}
}
if (event === 'change') {
if (compareMtime(srcFile, destFile)) {
gutil.log(gutil.colors.green("change: " + srcFile + " > " + destFile));
fs.copySync(srcFile, destFile);
}
}
if (event === 'unlink') {
if (fs.existsSync(destFile)) {
gutil.log(gutil.colors.green("delete: " + destFile));
fs.removeSync(destFile);
}
}
if (event === 'unlinkDir') {
if (fs.existsSync(destFile)) {
gutil.log(gutil.colors.green("rmdir: " + destFile));
return fs.removeSync(destFile, {
'force': true
});
}
}
});
};
}).call(this);