-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlivesync-service.ts
456 lines (379 loc) · 19.3 KB
/
livesync-service.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import * as path from "path";
import * as choki from "chokidar";
import { EventEmitter } from "events";
import { hook } from "../../common/helpers";
import { APP_FOLDER_NAME, PACKAGE_JSON_FILE_NAME, LiveSyncTrackActionNames } from "../../constants";
import { FileExtensions, DeviceTypes } from "../../common/constants";
const deviceDescriptorPrimaryKey = "identifier";
const LiveSyncEvents = {
liveSyncStopped: "liveSyncStopped",
// In case we name it error, EventEmitter expects instance of Error to be raised and will also raise uncaught exception in case there's no handler
liveSyncError: "liveSyncError",
liveSyncExecuted: "liveSyncExecuted",
liveSyncStarted: "liveSyncStarted",
liveSyncNotification: "notify"
};
export class LiveSyncService extends EventEmitter implements ILiveSyncService {
// key is projectDir
private liveSyncProcessesInfo: IDictionary<ILiveSyncProcessInfo> = {};
constructor(protected $platformService: IPlatformService,
private $projectDataService: IProjectDataService,
protected $devicesService: Mobile.IDevicesService,
private $mobileHelper: Mobile.IMobileHelper,
protected $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder,
protected $logger: ILogger,
private $processService: IProcessService,
private $hooksService: IHooksService,
private $pluginsService: IPluginsService,
protected $injector: IInjector) {
super();
}
public async liveSync(deviceDescriptors: ILiveSyncDeviceInfo[],
liveSyncData: ILiveSyncInfo): Promise<void> {
const projectData = this.$projectDataService.getProjectData(liveSyncData.projectDir);
await this.$pluginsService.ensureAllDependenciesAreInstalled(projectData);
await this.liveSyncOperation(deviceDescriptors, liveSyncData, projectData);
}
public async stopLiveSync(projectDir: string, deviceIdentifiers?: string[]): Promise<void> {
const liveSyncProcessInfo = this.liveSyncProcessesInfo[projectDir];
if (liveSyncProcessInfo) {
_.each(deviceIdentifiers, deviceId => {
_.remove(liveSyncProcessInfo.deviceDescriptors, descriptor => {
const shouldRemove = descriptor.identifier === deviceId;
if (shouldRemove) {
this.emit(LiveSyncEvents.liveSyncStopped, { projectDir, deviceIdentifier: descriptor.identifier });
}
return shouldRemove;
});
});
// In case deviceIdentifiers are not passed, we should stop the whole LiveSync.
if (!deviceIdentifiers || !deviceIdentifiers.length || !liveSyncProcessInfo.deviceDescriptors || !liveSyncProcessInfo.deviceDescriptors.length) {
if (liveSyncProcessInfo.timer) {
clearTimeout(liveSyncProcessInfo.timer);
}
if (liveSyncProcessInfo.watcherInfo && liveSyncProcessInfo.watcherInfo.watcher) {
liveSyncProcessInfo.watcherInfo.watcher.close();
}
liveSyncProcessInfo.watcherInfo = null;
if (liveSyncProcessInfo.actionsChain) {
await liveSyncProcessInfo.actionsChain;
}
_.each(liveSyncProcessInfo.deviceDescriptors, descriptor => {
this.emit(LiveSyncEvents.liveSyncStopped, { projectDir, deviceIdentifier: descriptor.identifier });
});
liveSyncProcessInfo.isStopped = true;
liveSyncProcessInfo.deviceDescriptors = [];
// Kill typescript watcher
const projectData = this.$projectDataService.getProjectData(projectDir);
await this.$hooksService.executeAfterHooks('watch', {
hookArgs: {
projectData
}
});
}
}
}
protected async refreshApplication(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo): Promise<void> {
const platformLiveSyncService = this.getLiveSyncService(liveSyncResultInfo.deviceAppData.platform);
try {
await platformLiveSyncService.refreshApplication(projectData, liveSyncResultInfo);
} catch (err) {
this.$logger.info(`Error while trying to start application ${projectData.projectId} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}. Error is: ${err.message || err}`);
const msg = `Unable to start application ${projectData.projectId} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}. Try starting it manually.`;
this.$logger.warn(msg);
this.emit(LiveSyncEvents.liveSyncNotification, {
projectDir: projectData.projectDir,
applicationIdentifier: projectData.projectId,
deviceIdentifier: liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier,
notification: msg
});
}
this.emit(LiveSyncEvents.liveSyncExecuted, {
projectDir: projectData.projectDir,
applicationIdentifier: projectData.projectId,
syncedFiles: liveSyncResultInfo.modifiedFilesData.map(m => m.getLocalPath()),
deviceIdentifier: liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier
});
this.$logger.info(`Successfully synced application ${liveSyncResultInfo.deviceAppData.appIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}.`);
}
@hook("liveSync")
private async liveSyncOperation(deviceDescriptors: ILiveSyncDeviceInfo[],
liveSyncData: ILiveSyncInfo, projectData: IProjectData): Promise<void> {
// In case liveSync is called for a second time for the same projectDir.
const isAlreadyLiveSyncing = this.liveSyncProcessesInfo[projectData.projectDir] && !this.liveSyncProcessesInfo[projectData.projectDir].isStopped;
// Prevent cases where liveSync is called consecutive times with the same device, for example [ A, B, C ] and then [ A, B, D ] - we want to execute initialSync only for D.
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, this.liveSyncProcessesInfo[projectData.projectDir].deviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
this.setLiveSyncProcessInfo(liveSyncData.projectDir, deviceDescriptors);
await this.initialSync(projectData, deviceDescriptorsForInitialSync, liveSyncData);
if (!liveSyncData.skipWatcher && deviceDescriptors && deviceDescriptors.length) {
// Should be set after prepare
this.$injector.resolve<DeprecatedUsbLiveSyncService>("usbLiveSyncService").isInitialized = true;
await this.startWatcher(projectData, liveSyncData);
}
}
private setLiveSyncProcessInfo(projectDir: string, deviceDescriptors: ILiveSyncDeviceInfo[]): void {
this.liveSyncProcessesInfo[projectDir] = this.liveSyncProcessesInfo[projectDir] || Object.create(null);
this.liveSyncProcessesInfo[projectDir].actionsChain = this.liveSyncProcessesInfo[projectDir].actionsChain || Promise.resolve();
this.liveSyncProcessesInfo[projectDir].isStopped = false;
const currentDeviceDescriptors = this.liveSyncProcessesInfo[projectDir].deviceDescriptors || [];
this.liveSyncProcessesInfo[projectDir].deviceDescriptors = _.uniqBy(currentDeviceDescriptors.concat(deviceDescriptors), deviceDescriptorPrimaryKey);
}
private getLiveSyncService(platform: string): IPlatformLiveSyncService {
if (this.$mobileHelper.isiOSPlatform(platform)) {
return this.$injector.resolve("iOSLiveSyncService");
} else if (this.$mobileHelper.isAndroidPlatform(platform)) {
return this.$injector.resolve("androidLiveSyncService");
}
throw new Error(`Invalid platform ${platform}. Supported platforms are: ${this.$mobileHelper.platformNames.join(", ")}`);
}
private async ensureLatestAppPackageIsInstalledOnDevice(options: IEnsureLatestAppPackageIsInstalledOnDeviceOptions): Promise<void> {
const platform = options.device.deviceInfo.platform;
if (options.preparedPlatforms.indexOf(platform) === -1) {
options.preparedPlatforms.push(platform);
// TODO: Pass provision and sdk as a fifth argument here
await this.$platformService.preparePlatform(platform, {
bundle: false,
release: false,
}, null, options.projectData, <any>{}, options.modifiedFiles);
}
const rebuildInfo = _.find(options.rebuiltInformation, info => info.isEmulator === options.device.isEmulator && info.platform === platform);
if (rebuildInfo) {
// Case where we have three devices attached, a change that requires build is found,
// we'll rebuild the app only for the first device, but we should install new package on all three devices.
await this.$platformService.installApplication(options.device, { release: false }, options.projectData, rebuildInfo.pathToBuildItem, options.deviceBuildInfoDescriptor.outputPath);
return;
}
// TODO: Pass provision and sdk as a fifth argument here
const shouldBuild = await this.$platformService.shouldBuild(platform, options.projectData, <any>{ buildForDevice: !options.device.isEmulator, clean: options.liveSyncData && options.liveSyncData.clean }, options.deviceBuildInfoDescriptor.outputPath);
let pathToBuildItem = null;
let action = LiveSyncTrackActionNames.LIVESYNC_OPERATION;
if (shouldBuild) {
pathToBuildItem = await options.deviceBuildInfoDescriptor.buildAction();
// Is it possible to return shouldBuild for two devices? What about android device and android emulator?
options.rebuiltInformation.push({ isEmulator: options.device.isEmulator, platform, pathToBuildItem });
action = LiveSyncTrackActionNames.LIVESYNC_OPERATION_BUILD;
}
if (!options.settings[platform][options.device.deviceInfo.type]) {
let isForDevice = !options.device.isEmulator;
options.settings[platform][options.device.deviceInfo.type] = true;
if (this.$mobileHelper.isAndroidPlatform(platform)) {
options.settings[platform][DeviceTypes.Emulator] = true;
options.settings[platform][DeviceTypes.Device] = true;
isForDevice = null;
}
await this.$platformService.trackActionForPlatform({ action, platform, isForDevice });
}
await this.$platformService.trackActionForPlatform({ action: LiveSyncTrackActionNames.DEVICE_INFO, platform, isForDevice: !options.device.isEmulator, deviceOsVersion: options.device.deviceInfo.version });
const shouldInstall = await this.$platformService.shouldInstall(options.device, options.projectData, options.deviceBuildInfoDescriptor.outputPath);
if (shouldInstall) {
await this.$platformService.installApplication(options.device, { release: false }, options.projectData, pathToBuildItem, options.deviceBuildInfoDescriptor.outputPath);
}
}
private async initialSync(projectData: IProjectData, deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise<void> {
const preparedPlatforms: string[] = [];
const rebuiltInformation: ILiveSyncBuildInfo[] = [];
const settings = this.getDefaultLatestAppPackageInstalledSettings();
// Now fullSync
const deviceAction = async (device: Mobile.IDevice): Promise<void> => {
try {
this.emit(LiveSyncEvents.liveSyncStarted, {
projectDir: projectData.projectDir,
deviceIdentifier: device.deviceInfo.identifier,
applicationIdentifier: projectData.projectId
});
const platform = device.deviceInfo.platform;
const deviceBuildInfoDescriptor = _.find(deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
await this.ensureLatestAppPackageIsInstalledOnDevice({
device,
preparedPlatforms,
rebuiltInformation,
projectData,
deviceBuildInfoDescriptor,
liveSyncData,
settings
});
const liveSyncResultInfo = await this.getLiveSyncService(platform).fullSync({
projectData, device,
syncAllFiles: liveSyncData.watchAllFiles,
useLiveEdit: liveSyncData.useLiveEdit,
watch: !liveSyncData.skipWatcher
});
await this.$platformService.trackActionForPlatform({ action: "LiveSync", platform: device.deviceInfo.platform, isForDevice: !device.isEmulator, deviceOsVersion: device.deviceInfo.version });
await this.refreshApplication(projectData, liveSyncResultInfo);
} catch (err) {
this.$logger.warn(`Unable to apply changes on device: ${device.deviceInfo.identifier}. Error is: ${err.message}.`);
this.emit(LiveSyncEvents.liveSyncError, {
error: err,
deviceIdentifier: device.deviceInfo.identifier,
projectDir: projectData.projectDir,
applicationIdentifier: projectData.projectId
});
await this.stopLiveSync(projectData.projectDir, [device.deviceInfo.identifier]);
}
};
// Execute the action only on the deviceDescriptors passed to initialSync.
// In case where we add deviceDescriptors to already running application, we've already executed initialSync for them.
await this.addActionToChain(projectData.projectDir, () => this.$devicesService.execute(deviceAction, (device: Mobile.IDevice) => _.some(deviceDescriptors, deviceDescriptor => deviceDescriptor.identifier === device.deviceInfo.identifier)));
}
private getDefaultLatestAppPackageInstalledSettings(): ILatestAppPackageInstalledSettings {
return {
[this.$devicePlatformsConstants.Android]: {
[DeviceTypes.Device]: false,
[DeviceTypes.Emulator]: false
},
[this.$devicePlatformsConstants.iOS]: {
[DeviceTypes.Device]: false,
[DeviceTypes.Emulator]: false
}
};
}
private async startWatcher(projectData: IProjectData, liveSyncData: ILiveSyncInfo): Promise<void> {
let pattern = [APP_FOLDER_NAME];
if (liveSyncData.watchAllFiles) {
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir);
pattern.push(PACKAGE_JSON_FILE_NAME);
// watch only production node_module/packages same one prepare uses
for (let index in productionDependencies) {
pattern.push(productionDependencies[index].directory);
}
}
const currentWatcherInfo = this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo;
if (!currentWatcherInfo || currentWatcherInfo.pattern !== pattern) {
if (currentWatcherInfo) {
currentWatcherInfo.watcher.close();
}
let filesToSync: string[] = [],
filesToRemove: string[] = [];
let timeoutTimer: NodeJS.Timer;
const startTimeout = () => {
timeoutTimer = setTimeout(async () => {
// Push actions to the queue, do not start them simultaneously
await this.addActionToChain(projectData.projectDir, async () => {
if (filesToSync.length || filesToRemove.length) {
try {
let currentFilesToSync = _.cloneDeep(filesToSync);
filesToSync = [];
let currentFilesToRemove = _.cloneDeep(filesToRemove);
filesToRemove = [];
const allModifiedFiles = [].concat(currentFilesToSync).concat(currentFilesToRemove);
const preparedPlatforms: string[] = [];
const rebuiltInformation: ILiveSyncBuildInfo[] = [];
const latestAppPackageInstalledSettings = this.getDefaultLatestAppPackageInstalledSettings();
await this.$devicesService.execute(async (device: Mobile.IDevice) => {
const liveSyncProcessInfo = this.liveSyncProcessesInfo[projectData.projectDir];
const deviceBuildInfoDescriptor = _.find(liveSyncProcessInfo.deviceDescriptors, dd => dd.identifier === device.deviceInfo.identifier);
await this.ensureLatestAppPackageIsInstalledOnDevice({
device,
preparedPlatforms,
rebuiltInformation,
projectData,
deviceBuildInfoDescriptor,
settings: latestAppPackageInstalledSettings,
modifiedFiles: allModifiedFiles
});
const service = this.getLiveSyncService(device.deviceInfo.platform);
const settings: ILiveSyncWatchInfo = {
projectData,
filesToRemove: currentFilesToRemove,
filesToSync: currentFilesToSync,
isRebuilt: !!_.find(rebuiltInformation, info => info.isEmulator === device.isEmulator && info.platform === device.deviceInfo.platform),
syncAllFiles: liveSyncData.watchAllFiles,
useLiveEdit: liveSyncData.useLiveEdit
};
const liveSyncResultInfo = await service.liveSyncWatchAction(device, settings);
await this.refreshApplication(projectData, liveSyncResultInfo);
},
(device: Mobile.IDevice) => {
const liveSyncProcessInfo = this.liveSyncProcessesInfo[projectData.projectDir];
return liveSyncProcessInfo && _.some(liveSyncProcessInfo.deviceDescriptors, deviceDescriptor => deviceDescriptor.identifier === device.deviceInfo.identifier);
}
);
} catch (err) {
const allErrors = (<Mobile.IDevicesOperationError>err).allErrors;
if (allErrors && _.isArray(allErrors)) {
for (let deviceError of allErrors) {
this.$logger.warn(`Unable to apply changes for device: ${deviceError.deviceIdentifier}. Error is: ${deviceError.message}.`);
this.emit(LiveSyncEvents.liveSyncError, {
error: deviceError,
deviceIdentifier: deviceError.deviceIdentifier,
projectDir: projectData.projectDir,
applicationIdentifier: projectData.projectId
});
await this.stopLiveSync(projectData.projectDir, [deviceError.deviceIdentifier]);
}
}
}
}
});
}, 250);
this.liveSyncProcessesInfo[liveSyncData.projectDir].timer = timeoutTimer;
};
await this.$hooksService.executeBeforeHooks('watch', {
hookArgs: {
projectData
}
});
const watcherOptions: choki.WatchOptions = {
ignoreInitial: true,
cwd: liveSyncData.projectDir,
awaitWriteFinish: {
pollInterval: 100,
stabilityThreshold: 500
},
ignored: ["**/.*", ".*"] // hidden files
};
const watcher = choki.watch(pattern, watcherOptions)
.on("all", async (event: string, filePath: string) => {
clearTimeout(timeoutTimer);
filePath = path.join(liveSyncData.projectDir, filePath);
this.$logger.trace(`Chokidar raised event ${event} for ${filePath}.`);
if (event === "add" || event === "addDir" || event === "change" /* <--- what to do when change event is raised ? */) {
filesToSync.push(filePath);
} else if (event === "unlink" || event === "unlinkDir") {
filesToRemove.push(filePath);
}
// Do not sync typescript files directly - wait for javascript changes to occur in order to restart the app only once
if (path.extname(filePath) !== FileExtensions.TYPESCRIPT_FILE) {
startTimeout();
}
});
this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo = { watcher, pattern };
this.liveSyncProcessesInfo[liveSyncData.projectDir].timer = timeoutTimer;
this.$processService.attachToProcessExitSignals(this, () => {
_.keys(this.liveSyncProcessesInfo).forEach(projectDir => {
// Do not await here, we are in process exit's handler.
this.stopLiveSync(projectDir);
});
});
this.$devicesService.on("deviceLost", async (device: Mobile.IDevice) => {
await this.stopLiveSync(projectData.projectDir, [device.deviceInfo.identifier]);
});
}
}
private async addActionToChain<T>(projectDir: string, action: () => Promise<T>): Promise<T> {
const liveSyncInfo = this.liveSyncProcessesInfo[projectDir];
if (liveSyncInfo) {
liveSyncInfo.actionsChain = liveSyncInfo.actionsChain.then(async () => {
if (!liveSyncInfo.isStopped) {
const res = await action();
return res;
}
});
const result = await liveSyncInfo.actionsChain;
return result;
}
}
}
$injector.register("liveSyncService", LiveSyncService);
/**
* This class is used only for old versions of nativescript-dev-typescript plugin.
* It should be replaced with liveSyncService.isInitalized.
* Consider adding get and set methods for isInitialized,
* so whenever someone tries to access the value of isInitialized,
* they'll get a warning to update the plugins (like nativescript-dev-typescript).
*/
export class DeprecatedUsbLiveSyncService {
public isInitialized = false;
}
$injector.register("usbLiveSyncService", DeprecatedUsbLiveSyncService);