Skip to content

Commit 91f0914

Browse files
author
Fatme
authored
Merge pull request #3603 from NativeScript/fatme/fix-livesync
Fix LiveSync problems
2 parents 8655173 + b2229a3 commit 91f0914

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

lib/services/android-project-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
706706
platformVersion = projectPackageJson.version;
707707
}
708708
} else {
709-
return false;
709+
return true;
710710
}
711711
}
712712

lib/services/livesync/livesync-service.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,19 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
321321
// In case liveSync is called for a second time for the same projectDir.
322322
const isAlreadyLiveSyncing = this.liveSyncProcessesInfo[projectData.projectDir] && !this.liveSyncProcessesInfo[projectData.projectDir].isStopped;
323323

324+
// 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.
325+
const currentlyRunningDeviceDescriptors = this.getLiveSyncDeviceDescriptors(projectData.projectDir);
326+
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, currentlyRunningDeviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
327+
324328
this.setLiveSyncProcessInfo(liveSyncData.projectDir, deviceDescriptors);
325329

326330
if (!liveSyncData.skipWatcher && this.liveSyncProcessesInfo[projectData.projectDir].deviceDescriptors.length) {
327331
// Should be set after prepare
328332
this.$usbLiveSyncService.isInitialized = true;
329-
await this.startWatcher(projectData, liveSyncData, deviceDescriptors, { isAlreadyLiveSyncing });
333+
await this.startWatcher(projectData, liveSyncData, deviceDescriptors);
330334
}
331335

332-
await this.initialSync(projectData, liveSyncData, deviceDescriptors, { isAlreadyLiveSyncing });
336+
await this.initialSync(projectData, liveSyncData, deviceDescriptorsForInitialSync);
333337
}
334338

335339
private setLiveSyncProcessInfo(projectDir: string, deviceDescriptors: ILiveSyncDeviceInfo[]): void {
@@ -342,13 +346,6 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
342346
this.liveSyncProcessesInfo[projectDir].deviceDescriptors = _.uniqBy(currentDeviceDescriptors.concat(deviceDescriptors), deviceDescriptorPrimaryKey);
343347
}
344348

345-
private async initialSync(projectData: IProjectData, liveSyncData: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceInfo[], options: { isAlreadyLiveSyncing: boolean }): Promise<void> {
346-
// 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.
347-
const currentlyRunningDeviceDescriptors = this.getLiveSyncDeviceDescriptors(projectData.projectDir);
348-
const deviceDescriptorsForInitialSync = options.isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, currentlyRunningDeviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
349-
await this.initialSyncCore(projectData, deviceDescriptorsForInitialSync, liveSyncData);
350-
}
351-
352349
private getLiveSyncService(platform: string): IPlatformLiveSyncService {
353350
if (this.$mobileHelper.isiOSPlatform(platform)) {
354351
return this.$injector.resolve("iOSLiveSyncService");
@@ -450,7 +447,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
450447
return null;
451448
}
452449

453-
private async initialSyncCore(projectData: IProjectData, deviceDescriptors: ILiveSyncDeviceInfo[], liveSyncData: ILiveSyncInfo): Promise<void> {
450+
private async initialSync(projectData: IProjectData, liveSyncData: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceInfo[]): Promise<void> {
454451
const preparedPlatforms: string[] = [];
455452
const rebuiltInformation: ILiveSyncBuildInfo[] = [];
456453

@@ -524,7 +521,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
524521
};
525522
}
526523

527-
private async startWatcher(projectData: IProjectData, liveSyncData: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceInfo[], options: { isAlreadyLiveSyncing: boolean }): Promise<void> {
524+
private async startWatcher(projectData: IProjectData, liveSyncData: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceInfo[]): Promise<void> {
528525
const devicesIds = deviceDescriptors.map(dd => dd.identifier);
529526
const devices = _.filter(this.$devicesService.getDeviceInstances(), device => _.includes(devicesIds, device.deviceInfo.identifier));
530527
const platforms = _(devices).map(device => device.deviceInfo.platform).uniq().value();

lib/services/platform-service.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
263263
this.$errors.failWithoutHelp(`Unable to install dependencies. Make sure your package.json is valid and all dependencies are correct. Error is: ${err.message}`);
264264
}
265265

266-
await this.ensurePlatformInstalled(platform, platformTemplate, projectData, config, nativePrepare);
266+
await this.ensurePlatformInstalled(platform, platformTemplate, projectData, config, appFilesUpdaterOptions, nativePrepare);
267267

268268
const bundle = appFilesUpdaterOptions.bundle;
269269
const nativePlatformStatus = (nativePrepare && nativePrepare.skipNativePrepare) ? constants.NativePlatformStatus.requiresPlatformAdd : constants.NativePlatformStatus.requiresPrepare;
@@ -603,7 +603,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
603603

604604
@helpers.hook('cleanApp')
605605
public async cleanDestinationApp(platformInfo: IPreparePlatformInfo): Promise<void> {
606-
await this.ensurePlatformInstalled(platformInfo.platform, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config, platformInfo.nativePrepare);
606+
await this.ensurePlatformInstalled(platformInfo.platform, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config, platformInfo.appFilesUpdaterOptions, platformInfo.nativePrepare);
607607

608608
const platformData = this.$platformsData.getPlatformData(platformInfo.platform, platformInfo.projectData);
609609
const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
@@ -711,14 +711,27 @@ export class PlatformService extends EventEmitter implements IPlatformService {
711711
}
712712
}
713713

714-
public async ensurePlatformInstalled(platform: string, platformTemplate: string, projectData: IProjectData, config: IPlatformOptions, nativePrepare?: INativePrepare): Promise<void> {
714+
public async ensurePlatformInstalled(platform: string, platformTemplate: string, projectData: IProjectData, config: IPlatformOptions, appFilesUpdaterOptions: IAppFilesUpdaterOptions, nativePrepare?: INativePrepare): Promise<void> {
715715
let requiresNativePlatformAdd = false;
716716

717+
const platformData = this.$platformsData.getPlatformData(platform, projectData);
718+
const prepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
719+
// In case when no platform is added and webpack plugin is started it produces files in platforms folder. In this case {N} CLI needs to add platform and keeps the already produced files from webpack
720+
if (appFilesUpdaterOptions.bundle && this.isPlatformInstalled(platform, projectData) && !this.$fs.exists(platformData.configurationFilePath) && (!prepareInfo || !prepareInfo.nativePlatformStatus || prepareInfo.nativePlatformStatus !== constants.NativePlatformStatus.alreadyPrepared)) {
721+
const tmpDirectoryPath = path.join(projectData.projectDir, "platforms", "tmp");
722+
this.$fs.deleteDirectory(tmpDirectoryPath);
723+
this.$fs.ensureDirectoryExists(tmpDirectoryPath);
724+
this.$fs.copyFile(path.join(platformData.appDestinationDirectoryPath, "*"), tmpDirectoryPath);
725+
await this.addPlatform(platform, platformTemplate, projectData, config, "", nativePrepare);
726+
this.$fs.copyFile(path.join(tmpDirectoryPath, "*"), platformData.appDestinationDirectoryPath);
727+
this.$fs.deleteDirectory(tmpDirectoryPath);
728+
return;
729+
}
730+
717731
if (!this.isPlatformInstalled(platform, projectData)) {
718732
await this.addPlatform(platform, platformTemplate, projectData, config, "", nativePrepare);
719733
} else {
720734
const shouldAddNativePlatform = !nativePrepare || !nativePrepare.skipNativePrepare;
721-
const prepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
722735
// In case there's no prepare info, it means only platform add had been executed. So we've come from CLI and we do not need to prepare natively.
723736
requiresNativePlatformAdd = prepareInfo && prepareInfo.nativePlatformStatus === constants.NativePlatformStatus.requiresPlatformAdd;
724737
if (requiresNativePlatformAdd && shouldAddNativePlatform) {

test/npm-support.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function createTestInjector(): IInjector {
4949
testInjector.register("platformsData", PlatformsDataLib.PlatformsData);
5050
testInjector.register("platformService", PlatformServiceLib.PlatformService);
5151
testInjector.register("logger", stubs.LoggerStub);
52-
testInjector.register("npmInstallationManager", {});
52+
testInjector.register("npmInstallationManager", {
53+
install: () => Promise.resolve()
54+
});
5355
testInjector.register("prompter", {});
5456
testInjector.register("sysInfo", {});
5557
testInjector.register("androidProjectService", {});

0 commit comments

Comments
 (0)