Skip to content

Commit e74cc8f

Browse files
Fix execution of livesync hooks when there are no node_modules
In case `node_modules` are deleted from the project and `tns run <platform>` or `tns debug <platform>` is executed, the livesync hook fails as the package that's required in it is not installed. In order to fix this, extract the logic of the liveSync method in a separate method and decorate it with the hook. In the public method make sure all dependencies are installed and call the private method. This way the hook will be executed correctly.
1 parent 7e8c9e2 commit e74cc8f

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

lib/services/livesync/debug-livesync-service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class DebugLiveSyncService extends LiveSyncService implements IDebugLiveS
1212
protected $logger: ILogger,
1313
$processService: IProcessService,
1414
$hooksService: IHooksService,
15+
$pluginsService: IPluginsService,
1516
protected $injector: IInjector,
1617
private $options: IOptions,
1718
private $debugDataService: IDebugDataService,
@@ -28,6 +29,7 @@ export class DebugLiveSyncService extends LiveSyncService implements IDebugLiveS
2829
$logger,
2930
$processService,
3031
$hooksService,
32+
$pluginsService,
3133
$injector);
3234
}
3335

lib/services/livesync/livesync-service.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,16 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
2828
protected $logger: ILogger,
2929
private $processService: IProcessService,
3030
private $hooksService: IHooksService,
31+
private $pluginsService: IPluginsService,
3132
protected $injector: IInjector) {
3233
super();
3334
}
3435

35-
@hook("liveSync")
3636
public async liveSync(deviceDescriptors: ILiveSyncDeviceInfo[],
3737
liveSyncData: ILiveSyncInfo): Promise<void> {
3838
const projectData = this.$projectDataService.getProjectData(liveSyncData.projectDir);
39-
// In case liveSync is called for a second time for the same projectDir.
40-
const isAlreadyLiveSyncing = this.liveSyncProcessesInfo[projectData.projectDir] && !this.liveSyncProcessesInfo[projectData.projectDir].isStopped;
41-
this.setLiveSyncProcessInfo(liveSyncData.projectDir, deviceDescriptors);
42-
43-
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, this.liveSyncProcessesInfo[projectData.projectDir].deviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
44-
45-
await this.initialSync(projectData, deviceDescriptorsForInitialSync, liveSyncData);
46-
47-
if (!liveSyncData.skipWatcher && deviceDescriptors && deviceDescriptors.length) {
48-
// Should be set after prepare
49-
this.$injector.resolve<DeprecatedUsbLiveSyncService>("usbLiveSyncService").isInitialized = true;
50-
51-
await this.startWatcher(projectData, liveSyncData);
52-
}
39+
await this.$pluginsService.ensureAllDependenciesAreInstalled(projectData);
40+
await this.liveSyncOperation(deviceDescriptors, liveSyncData, projectData);
5341
}
5442

5543
public async stopLiveSync(projectDir: string, deviceIdentifiers?: string[]): Promise<void> {
@@ -125,6 +113,25 @@ export class LiveSyncService extends EventEmitter implements ILiveSyncService {
125113
this.$logger.info(`Successfully synced application ${liveSyncResultInfo.deviceAppData.appIdentifier} on device ${liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier}.`);
126114
}
127115

116+
@hook("liveSync")
117+
private async liveSyncOperation(deviceDescriptors: ILiveSyncDeviceInfo[],
118+
liveSyncData: ILiveSyncInfo, projectData: IProjectData): Promise<void> {
119+
// In case liveSync is called for a second time for the same projectDir.
120+
const isAlreadyLiveSyncing = this.liveSyncProcessesInfo[projectData.projectDir] && !this.liveSyncProcessesInfo[projectData.projectDir].isStopped;
121+
this.setLiveSyncProcessInfo(liveSyncData.projectDir, deviceDescriptors);
122+
123+
const deviceDescriptorsForInitialSync = isAlreadyLiveSyncing ? _.differenceBy(deviceDescriptors, this.liveSyncProcessesInfo[projectData.projectDir].deviceDescriptors, deviceDescriptorPrimaryKey) : deviceDescriptors;
124+
125+
await this.initialSync(projectData, deviceDescriptorsForInitialSync, liveSyncData);
126+
127+
if (!liveSyncData.skipWatcher && deviceDescriptors && deviceDescriptors.length) {
128+
// Should be set after prepare
129+
this.$injector.resolve<DeprecatedUsbLiveSyncService>("usbLiveSyncService").isInitialized = true;
130+
131+
await this.startWatcher(projectData, liveSyncData);
132+
}
133+
}
134+
128135
private setLiveSyncProcessInfo(projectDir: string, deviceDescriptors: ILiveSyncDeviceInfo[]): void {
129136
this.liveSyncProcessesInfo[projectDir] = this.liveSyncProcessesInfo[projectDir] || Object.create(null);
130137
this.liveSyncProcessesInfo[projectDir].actionsChain = this.liveSyncProcessesInfo[projectDir].actionsChain || Promise.resolve();

0 commit comments

Comments
 (0)