/
watch-build.ts
238 lines (205 loc) · 7.25 KB
/
watch-build.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type * as d from '../../declarations';
import { build } from './build';
import { BuildContext } from './build-ctx';
import { compilerRequest } from '../bundle/dev-module';
import { createTsWatchProgram } from '../transpile/create-watch-program';
import { dirname, resolve } from 'path';
import {
filesChanged,
hasHtmlChanges,
hasScriptChanges,
hasStyleChanges,
scriptsAdded,
scriptsDeleted,
} from '../fs-watch/fs-watch-rebuild';
import { hasServiceWorkerChanges } from '../service-worker/generate-sw';
import { isString } from '@utils';
import type ts from 'typescript';
export const createWatchBuild = async (config: d.Config, compilerCtx: d.CompilerCtx): Promise<d.CompilerWatcher> => {
let isRebuild = false;
let tsWatchProgram: {
program: ts.WatchOfConfigFile<ts.EmitAndSemanticDiagnosticsBuilderProgram>;
rebuild: () => void;
};
let closeResolver: Function;
const watchWaiter = new Promise<d.WatcherCloseResults>(resolve => (closeResolver = resolve));
const dirsAdded = new Set<string>();
const dirsDeleted = new Set<string>();
const filesAdded = new Set<string>();
const filesUpdated = new Set<string>();
const filesDeleted = new Set<string>();
const onBuild = async (tsBuilder: ts.BuilderProgram) => {
const buildCtx = new BuildContext(config, compilerCtx);
buildCtx.isRebuild = isRebuild;
buildCtx.requiresFullBuild = !isRebuild;
buildCtx.dirsAdded = Array.from(dirsAdded.keys()).sort();
buildCtx.dirsDeleted = Array.from(dirsDeleted.keys()).sort();
buildCtx.filesAdded = Array.from(filesAdded.keys()).sort();
buildCtx.filesUpdated = Array.from(filesUpdated.keys()).sort();
buildCtx.filesDeleted = Array.from(filesDeleted.keys()).sort();
buildCtx.filesChanged = filesChanged(buildCtx);
buildCtx.scriptsAdded = scriptsAdded(buildCtx);
buildCtx.scriptsDeleted = scriptsDeleted(buildCtx);
buildCtx.hasScriptChanges = hasScriptChanges(buildCtx);
buildCtx.hasStyleChanges = hasStyleChanges(buildCtx);
buildCtx.hasHtmlChanges = hasHtmlChanges(config, buildCtx);
buildCtx.hasServiceWorkerChanges = hasServiceWorkerChanges(config, buildCtx);
dirsAdded.clear();
dirsDeleted.clear();
filesAdded.clear();
filesUpdated.clear();
filesDeleted.clear();
emitFsChange(compilerCtx, buildCtx);
buildCtx.start();
const result = await build(config, compilerCtx, buildCtx, tsBuilder);
if (result && !result.hasError) {
isRebuild = true;
}
};
const start = async () => {
const srcRead = watchSrcDirectory(config, compilerCtx);
const otherRead = watchRootFiles(config, compilerCtx);
await srcRead;
await otherRead;
tsWatchProgram = await createTsWatchProgram(config, onBuild);
return watchWaiter;
};
const watchingDirs = new Map<string, d.CompilerFileWatcher>();
const watchingFiles = new Map<string, d.CompilerFileWatcher>();
const onFsChange: d.CompilerFileWatcherCallback = (p, eventKind) => {
if (tsWatchProgram) {
updateCompilerCtxCache(config, compilerCtx, p, eventKind);
switch (eventKind) {
case 'dirAdd':
dirsAdded.add(p);
break;
case 'dirDelete':
dirsDeleted.add(p);
break;
case 'fileAdd':
filesAdded.add(p);
break;
case 'fileUpdate':
filesUpdated.add(p);
break;
case 'fileDelete':
filesDeleted.add(p);
break;
}
config.logger.debug(`onFsChange ${eventKind}: ${p}`);
tsWatchProgram.rebuild();
}
};
const onDirChange: d.CompilerFileWatcherCallback = (p, eventKind) => {
if (eventKind != null) {
onFsChange(p, eventKind);
}
};
const close = async () => {
watchingDirs.forEach(w => w.close());
watchingFiles.forEach(w => w.close());
watchingDirs.clear();
watchingFiles.clear();
if (tsWatchProgram) {
tsWatchProgram.program.close();
tsWatchProgram = null;
}
const watcherCloseResults: d.WatcherCloseResults = {
exitCode: 0,
};
closeResolver(watcherCloseResults);
return watcherCloseResults;
};
const request = async (data: d.CompilerRequest) => compilerRequest(config, compilerCtx, data);
compilerCtx.addWatchFile = filePath => {
if (isString(filePath) && !watchingFiles.has(filePath)) {
watchingFiles.set(filePath, config.sys.watchFile(filePath, onFsChange));
}
};
compilerCtx.addWatchDir = (dirPath, recursive) => {
if (isString(dirPath) && !watchingDirs.has(dirPath)) {
watchingDirs.set(dirPath, config.sys.watchDirectory(dirPath, onDirChange, recursive));
}
};
config.sys.addDestory(close);
return {
start,
close,
on: compilerCtx.events.on,
request,
};
};
const watchSrcDirectory = async (config: d.Config, compilerCtx: d.CompilerCtx) => {
const srcFiles = await compilerCtx.fs.readdir(config.srcDir, {
recursive: true,
excludeDirNames: ['.cache', '.git', '.github', '.stencil', '.vscode', 'node_modules'],
excludeExtensions: [
'.md',
'.markdown',
'.txt',
'.spec.ts',
'.spec.tsx',
'.e2e.ts',
'.e2e.tsx',
'.gitignore',
'.editorconfig',
],
});
srcFiles.filter(({ isFile }) => isFile).forEach(({ absPath }) => compilerCtx.addWatchFile(absPath));
compilerCtx.addWatchDir(config.srcDir, true);
};
const watchRootFiles = async (config: d.Config, compilerCtx: d.CompilerCtx) => {
// non-src files that cause a rebuild
// mainly for root level config files, and getting an event when they change
const rootFiles = await compilerCtx.fs.readdir(config.rootDir, {
recursive: false,
excludeDirNames: ['.cache', '.git', '.github', '.stencil', '.vscode', 'node_modules'],
});
rootFiles.filter(({ isFile }) => isFile).forEach(({ absPath }) => compilerCtx.addWatchFile(absPath));
};
const emitFsChange = (compilerCtx: d.CompilerCtx, buildCtx: BuildContext) => {
if (
buildCtx.dirsAdded.length > 0 ||
buildCtx.dirsDeleted.length > 0 ||
buildCtx.filesUpdated.length > 0 ||
buildCtx.filesAdded.length > 0 ||
buildCtx.filesDeleted.length > 0
) {
compilerCtx.events.emit('fsChange', {
dirsAdded: buildCtx.dirsAdded.slice(),
dirsDeleted: buildCtx.dirsDeleted.slice(),
filesUpdated: buildCtx.filesUpdated.slice(),
filesAdded: buildCtx.filesAdded.slice(),
filesDeleted: buildCtx.filesDeleted.slice(),
});
}
};
const updateCompilerCtxCache = (
config: d.Config,
compilerCtx: d.CompilerCtx,
path: string,
kind: d.CompilerFileWatcherEvent,
) => {
compilerCtx.fs.clearFileCache(path);
compilerCtx.changedFiles.add(path);
if (kind === 'fileDelete') {
compilerCtx.moduleMap.delete(path);
} else if (kind === 'dirDelete') {
const fsRootDir = resolve('/');
compilerCtx.moduleMap.forEach((_, moduleFilePath) => {
let moduleAncestorDir = dirname(moduleFilePath);
for (let i = 0; i < 50; i++) {
if (moduleAncestorDir === config.rootDir || moduleAncestorDir === fsRootDir) {
break;
}
if (moduleAncestorDir === path) {
compilerCtx.fs.clearFileCache(moduleFilePath);
compilerCtx.moduleMap.delete(moduleFilePath);
compilerCtx.changedFiles.add(moduleFilePath);
break;
}
moduleAncestorDir = dirname(moduleAncestorDir);
}
});
}
};