-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.js
More file actions
110 lines (92 loc) · 3.17 KB
/
watch.js
File metadata and controls
110 lines (92 loc) · 3.17 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
const chokidar = require('chokidar');
const path = require('path');
const fs = require('fs-extra');
const { renderFile } = require('ejs');
const minimatch = require('minimatch');
class Watcher {
constructor() {
const sourcePath = process.cwd();
const bootPath = path.join(sourcePath, 'bootstrap.js');
this.templatePath = path.join(sourcePath, 'template');
const mockPath = path.join(sourcePath, 'mock');
this.destPath = path.join(mockPath, 'output');
const inputMockPath = path.join(mockPath, 'prompt.mock.json');
if (!fs.existsSync(bootPath) || !fs.existsSync(this.templatePath)) {
console.log('Please run this CMD under the root directory of esboot template! (with bootstrap.js)');
process.exit(-1);
}
const bootConfig = require(bootPath);
this.templateExt = '.ejs';
this.answer = this.initAnswer(inputMockPath, bootConfig, mockPath);
this.copyIgnores = this.initCopyIgnores(bootConfig, this.answer);
}
initCopyIgnores(bootConfig, answer) {
const l = bootConfig.ignore ? bootConfig.ignore.reduce((prev, next) => {
prev[next] = '';
return prev;
}, {}) : {};
const obj = { ...bootConfig.filter, ...l };
for (let key in obj) {
const v = obj[key];
if (answer.hasOwnProperty(v) && answer[v]) delete obj[key];
}
return Object.keys(obj)
}
initAnswer(inputMockPath, bootConfig, mockPath) {
let answer = {};
if (fs.existsSync(inputMockPath)) {
answer = fs.readJSONSync(inputMockPath);
} else {
if (bootConfig.hasOwnProperty('prompt')) {
answer = bootConfig.prompt.reduce((prev, curr) => {
prev[curr.name] = curr.default;
return prev
}, {});
fs.ensureDir(mockPath);
fs.writeJSONSync(inputMockPath, answer);
}
}
console.log('load prompt config', answer);
return answer;
}
isCopyExclude(p) {
for (let i of this.copyIgnores) {
if (minimatch(p, i, { dot: true })) return true
}
return false;
}
copy(from, to) {
fs.copySync(from, to);
}
copyTpl(from, to) {
renderFile(from, this.answer, (err, rs) => {
if (err) throw err;
fs.writeFileSync(to.replace(this.templateExt, ''), rs, 'utf8');
})
}
copyFile(from, to) {
from.endsWith(this.templateExt) ? this.copyTpl(from, to) : this.copy(from, to);
}
start() {
fs.emptyDirSync(this.destPath);
const watcher = chokidar.watch(this.templatePath);
console.log('start watch', this.templatePath, '...');
watcher.on('all', (event, fullPath) => {
const relativeTemplatePath = path.relative(this.templatePath, fullPath);
const destPath = path.join(this.destPath, relativeTemplatePath);
// console.log(event, relativeTemplatePath);
if (event === 'add' || event === 'change') {
if (this.isCopyExclude(relativeTemplatePath)) return;
fs.ensureDirSync(path.dirname(destPath));
this.copyFile(fullPath, destPath);
console.log(event, relativeTemplatePath);
return;
}
if (event === 'unlink' || event === 'unlinkDir') {
console.log(event, relativeTemplatePath);
fs.removeSync(destPath);
}
});
}
}
module.exports = Watcher;