-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
98 lines (81 loc) · 2.76 KB
/
index.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
const syncLocalFiles = require('./lib/local-syncfiles');
const watchLocalFiles = require('./lib/local-watch');
const matchUtil = require('./lib/match-util');
const isAbsoluteUrl = require('is-absolute');
const path = require('path');
const fs = require('fs');
const formatParams = (srcDir, targetDir, customOptions = {}) => {
// format srcDir and targetDir to absolute path
if (!srcDir) {
throw new Error(`[sync-directory] source path is missing`);
}
if (!targetDir) {
throw new Error(`[sync-directory] target path is missing`);
}
const cwd = customOptions.cwd || process.cwd(); // prefer customed cwd
if (!isAbsoluteUrl(srcDir)) {
srcDir = path.join(cwd, srcDir);
}
if (!isAbsoluteUrl(targetDir)) {
targetDir = path.join(cwd, targetDir);
}
if (!fs.existsSync(srcDir)) {
throw new Error(`[sync-directory] "srcDir" folder does not exist: "${srcDir}"`);
}
const options = {
type: 'copy',
skipInitialSync: false,
stayHardlink: true,
watch: false,
deleteOrphaned: false,
staySymlink: false,
chokidarWatchOptions: {},
include: null,
exclude: null,
forceSync: null,
nodeep: false,
afterEachSync: () => {},
onError: (err) => {
const e = new Error(err.message);
e.stack = err.stack;
throw e;
}
};
Object.assign(options, customOptions);
// priot: 1
options.include = options.include === null ? () => true : matchUtil.toFunction(options.include);
// priot: 2
options.exclude = options.exclude === null ? () => false : matchUtil.toFunction(options.exclude);
// priot: 3
options.forceSync = options.forceSync === null ? () => false : matchUtil.toFunction(options.forceSync);
options.afterSync = options.afterEachSync;
return { srcDir, targetDir, options };
};
const synced = (...args) => {
const params = formatParams(...args);
if (params) {
const { srcDir, targetDir, options } = params;
if (!options.skipInitialSync) {
syncLocalFiles.sync(srcDir, targetDir, options);
}
if (options.watch) {
return watchLocalFiles(srcDir, targetDir, options);
}
}
};
const asynced = async (...args) => {
const params = formatParams(...args);
if (params) {
const { srcDir, targetDir, options } = params;
if (!options.skipInitialSync) {
await syncLocalFiles.async(srcDir, targetDir, options);
}
if (options.watch) {
return watchLocalFiles(srcDir, targetDir, options);
}
}
};
module.exports = synced;
module.exports.sync = synced;
module.exports.async = asynced;
module.exports.formatParams = formatParams;