-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathprepare-controller.ts
166 lines (134 loc) · 7.43 KB
/
prepare-controller.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
import * as child_process from "child_process";
import * as choki from "chokidar";
import { hook } from "../common/helpers";
import { performanceLog } from "../common/decorators";
import { EventEmitter } from "events";
import * as path from "path";
import { PREPARE_READY_EVENT_NAME, WEBPACK_COMPILATION_COMPLETE, PACKAGE_JSON_FILE_NAME, PLATFORMS_DIR_NAME } from "../constants";
interface IPlatformWatcherData {
webpackCompilerProcess: child_process.ChildProcess;
nativeFilesWatcher: choki.FSWatcher;
}
export class PrepareController extends EventEmitter {
private watchersData: IDictionary<IDictionary<IPlatformWatcherData>> = {};
private isInitialPrepareReady = false;
private persistedData: IFilesChangeEventData[] = [];
constructor(
private $platformController: IPlatformController,
public $hooksService: IHooksService,
private $logger: ILogger,
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder,
private $platformsDataService: IPlatformsDataService,
private $prepareNativePlatformService: IPrepareNativePlatformService,
private $projectChangesService: IProjectChangesService,
private $projectDataService: IProjectDataService,
private $webpackCompilerService: IWebpackCompilerService
) { super(); }
@performanceLog()
@hook("prepare")
public async prepare(prepareData: IPrepareData): Promise<IPrepareResultData> {
await this.$platformController.addPlatformIfNeeded(prepareData);
this.$logger.info("Preparing project...");
let result = null;
const projectData = this.$projectDataService.getProjectData(prepareData.projectDir);
const platformData = this.$platformsDataService.getPlatformData(prepareData.platform, projectData);
if (prepareData.watch) {
result = await this.startWatchersWithPrepare(platformData, projectData, prepareData);
} else {
await this.$webpackCompilerService.compileWithoutWatch(platformData, projectData, prepareData);
const hasNativeChanges = await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData);
result = { hasNativeChanges, platform: prepareData.platform.toLowerCase() };
}
await this.$projectChangesService.savePrepareInfo(platformData, projectData, prepareData);
this.$logger.info(`Project successfully prepared (${prepareData.platform.toLowerCase()})`);
return result;
}
public stopWatchers(projectDir: string, platform: string): void {
const platformLowerCase = platform.toLowerCase();
if (this.watchersData && this.watchersData[projectDir] && this.watchersData[projectDir][platformLowerCase] && this.watchersData[projectDir][platformLowerCase].nativeFilesWatcher) {
this.watchersData[projectDir][platformLowerCase].nativeFilesWatcher.close();
this.watchersData[projectDir][platformLowerCase].nativeFilesWatcher = null;
}
if (this.watchersData && this.watchersData[projectDir] && this.watchersData[projectDir][platformLowerCase] && this.watchersData[projectDir][platformLowerCase].webpackCompilerProcess) {
this.$webpackCompilerService.stopWebpackCompiler(platform);
this.watchersData[projectDir][platformLowerCase].webpackCompilerProcess = null;
}
}
@hook("watch")
private async startWatchersWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<IPrepareResultData> {
if (!this.watchersData[projectData.projectDir]) {
this.watchersData[projectData.projectDir] = {};
}
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase]) {
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase] = {
nativeFilesWatcher: null,
webpackCompilerProcess: null
};
}
await this.startJSWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial compilation
const hasNativeChanges = await this.startNativeWatcherWithPrepare(platformData, projectData, prepareData); // -> start watcher + initial prepare
const result = { platform: platformData.platformNameLowerCase, hasNativeChanges };
const hasPersistedDataWithNativeChanges = this.persistedData.find(data => data.platform === result.platform && data.hasNativeChanges);
if (hasPersistedDataWithNativeChanges) {
result.hasNativeChanges = true;
}
this.isInitialPrepareReady = true;
if (this.persistedData && this.persistedData.length) {
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hasNativeChanges: result.hasNativeChanges, hmrData: null, platform: platformData.platformNameLowerCase });
}
return result;
}
private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess) {
this.$webpackCompilerService.on(WEBPACK_COMPILATION_COMPLETE, data => {
this.emitPrepareEvent({ ...data, hasNativeChanges: false, platform: platformData.platformNameLowerCase });
});
const childProcess = await this.$webpackCompilerService.compileWithWatch(platformData, projectData, prepareData);
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].webpackCompilerProcess = childProcess;
}
}
private async startNativeWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<boolean> {
if ((prepareData.nativePrepare && prepareData.nativePrepare.skipNativePrepare) || this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].nativeFilesWatcher) {
return false;
}
const patterns = await this.getWatcherPatterns(platformData, projectData);
const watcherOptions: choki.WatchOptions = {
ignoreInitial: true,
cwd: projectData.projectDir,
awaitWriteFinish: {
pollInterval: 100,
stabilityThreshold: 500
},
ignored: ["**/.*", ".*"] // hidden files
};
const watcher = choki.watch(patterns, watcherOptions)
.on("all", async (event: string, filePath: string) => {
filePath = path.join(projectData.projectDir, filePath);
this.$logger.trace(`Chokidar raised event ${event} for ${filePath}.`);
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hmrData: null, hasNativeChanges: true, platform: platformData.platformNameLowerCase });
});
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].nativeFilesWatcher = watcher;
const hasNativeChanges = await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData);
return hasNativeChanges;
}
@hook('watchPatterns')
public async getWatcherPatterns(platformData: IPlatformData, projectData: IProjectData): Promise<string[]> {
const pluginsNativeDirectories = this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir)
.filter(dep => dep.nativescript)
.map(dep => path.join(dep.directory, PLATFORMS_DIR_NAME, platformData.platformNameLowerCase));
const patterns = [
path.join(projectData.projectDir, PACKAGE_JSON_FILE_NAME),
path.join(projectData.getAppDirectoryPath(), PACKAGE_JSON_FILE_NAME),
path.join(projectData.getAppResourcesRelativeDirectoryPath(), platformData.normalizedPlatformName),
].concat(pluginsNativeDirectories);
return patterns;
}
private emitPrepareEvent(filesChangeEventData: IFilesChangeEventData) {
if (this.isInitialPrepareReady) {
this.emit(PREPARE_READY_EVENT_NAME, filesChangeEventData);
} else {
this.persistedData.push(filesChangeEventData);
}
}
}
$injector.register("prepareController", PrepareController);