Skip to content

Commit 37c3f4c

Browse files
author
Dimitar Kerezov
committed
refactor: introduce shouldprepare hook
1 parent 6a25723 commit 37c3f4c

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/definitions/platform.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ interface IPlatformService extends NodeJS.EventEmitter {
7878
*/
7979
shouldInstall(device: Mobile.IDevice, projectData: IProjectData, outputPath?: string): Promise<boolean>;
8080

81+
/**
82+
* Determines whether the project should undergo the prepare process.
83+
* @param {IPreparePlatformInfo} platformInfo Options to control the preparation.
84+
* @param {IProjectChangesInfo} @optional changesInfo Info about the current changes in the project.
85+
* @returns {Promise<boolean>} true indicates that the project should be prepared.
86+
*/
87+
shouldPrepare(platformInfo: IPreparePlatformInfo, changesInfo?: IProjectChangesInfo): Promise<boolean>
88+
8189
/**
8290
* Installs the application on specified device.
8391
* When finishes, saves .nsbuildinfo in application root folder to indicate the prepare that was used to build the app.

lib/services/platform-service.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,25 @@ export class PlatformService extends EventEmitter implements IPlatformService {
187187
return _.filter(this.$platformsData.platformsNames, p => { return this.isPlatformPrepared(p, projectData); });
188188
}
189189

190-
public async preparePlatform(platformInfo: IPreparePlatformInfo): Promise<boolean> {
190+
@helpers.hook('shouldPrepare')
191+
public async shouldPrepare(platformInfo: IPreparePlatformInfo, changesInfo?: IProjectChangesInfo): Promise<boolean> {
192+
changesInfo = changesInfo || await this.getChangesInfo(platformInfo);
193+
const requiresNativePrepare = (!platformInfo.nativePrepare || !platformInfo.nativePrepare.skipNativePrepare) && changesInfo.nativePlatformStatus === constants.NativePlatformStatus.requiresPrepare;
194+
195+
return changesInfo.hasChanges || requiresNativePrepare;
196+
}
197+
198+
private async getChangesInfo(platformInfo: IPreparePlatformInfo): Promise<IProjectChangesInfo> {
191199
const platformData = this.$platformsData.getPlatformData(platformInfo.platform, platformInfo.projectData);
192200

193-
const changesInfo = await this.initialPrepare(platformInfo.platform, platformData, platformInfo.appFilesUpdaterOptions, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config, platformInfo.nativePrepare, platformInfo);
194-
const requiresNativePrepare = (!platformInfo.nativePrepare || !platformInfo.nativePrepare.skipNativePrepare) && changesInfo.nativePlatformStatus === constants.NativePlatformStatus.requiresPrepare;
201+
return this.initialPrepare(platformInfo.platform, platformData, platformInfo.appFilesUpdaterOptions, platformInfo.platformTemplate, platformInfo.projectData, platformInfo.config, platformInfo.nativePrepare, platformInfo);
202+
}
203+
204+
public async preparePlatform(platformInfo: IPreparePlatformInfo): Promise<boolean> {
205+
const changesInfo = await this.getChangesInfo(platformInfo);
206+
const shouldPrepare = await this.shouldPrepare(platformInfo, changesInfo);
195207

196-
if (changesInfo.hasChanges || requiresNativePrepare) {
208+
if (shouldPrepare) {
197209
// Always clear up the app directory in platforms if `--bundle` value has changed in between builds or is passed in general
198210
// this is done as user has full control over what goes in platforms when `--bundle` is passed
199211
// and we may end up with duplicate symbols which would fail the build

test/stubs.ts

+3
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ export class CommandsService implements ICommandsService {
613613
}
614614

615615
export class PlatformServiceStub extends EventEmitter implements IPlatformService {
616+
public shouldPrepare(): Promise<boolean> {
617+
return Promise.resolve(true);
618+
}
616619

617620
public validateOptions(): Promise<boolean> {
618621
return Promise.resolve(true);

0 commit comments

Comments
 (0)