Skip to content

Commit 5f46293

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

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/definitions/platform.d.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ 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 {IShouldPrepareInfo} shouldPrepareInfo Options needed to decide whether to prepare.
84+
* @returns {Promise<boolean>} true indicates that the project should be prepared.
85+
*/
86+
shouldPrepare(shouldPrepareInfo: IShouldPrepareInfo): Promise<boolean>
87+
8188
/**
8289
* Installs the application on specified device.
8390
* When finishes, saves .nsbuildinfo in application root folder to indicate the prepare that was used to build the app.
@@ -314,11 +321,18 @@ interface IPreparePlatformJSInfo extends IPreparePlatformCoreInfo, ICopyAppFiles
314321
projectFilesConfig?: IProjectFilesConfig;
315322
}
316323

317-
interface IPreparePlatformCoreInfo extends IPreparePlatformInfoBase {
318-
platformSpecificData: IPlatformSpecificData
324+
interface IShouldPrepareInfo extends IOptionalProjectChangesInfoComposition {
325+
platformInfo: IPreparePlatformInfo;
326+
}
327+
328+
interface IOptionalProjectChangesInfoComposition {
319329
changesInfo?: IProjectChangesInfo;
320330
}
321331

332+
interface IPreparePlatformCoreInfo extends IPreparePlatformInfoBase, IOptionalProjectChangesInfoComposition {
333+
platformSpecificData: IPlatformSpecificData
334+
}
335+
322336
interface IPreparePlatformInfo extends IPreparePlatformInfoBase, IPlatformConfig, IPlatformTemplate, ISkipNativeCheckOptional { }
323337

324338
interface IPlatformConfig {

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(shouldPrepareInfo: IShouldPrepareInfo): Promise<boolean> {
192+
shouldPrepareInfo.changesInfo = shouldPrepareInfo.changesInfo || await this.getChangesInfo(shouldPrepareInfo.platformInfo);
193+
const requiresNativePrepare = (!shouldPrepareInfo.platformInfo.nativePrepare || !shouldPrepareInfo.platformInfo.nativePrepare.skipNativePrepare) && shouldPrepareInfo.changesInfo.nativePlatformStatus === constants.NativePlatformStatus.requiresPrepare;
194+
195+
return shouldPrepareInfo.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)