Skip to content

Commit 86a7e55

Browse files
feat: Add hook for checkForChanges
In case a plugin needs to apply some modifications over its own source, based on the project files, currently it cannot do it. The earliest possible hooks are before-shouldPrepare and beforePrepare, but they are both run after CLI had check the project for changes. In case the plugin uses any of these hooks and apply changes to its source code, CLI will not move the changes to platforms dir of the project as it will not recheck the project state. In order to resolve this issue, add a hook to checkForChanges method. This way a plugin can apply the changes in before-checkForChanges hook and CLI will detect them correctly. Change the method parameters to be a single object, which will allow easier modifications in the future without breaking the plugins that are already using the hook.
1 parent c6c374a commit 86a7e55

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

lib/definitions/project-changes.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ interface IProjectChangesOptions extends IAppFilesUpdaterOptions, IProvision, IT
4141
nativePlatformStatus?: "1" | "2" | "3";
4242
}
4343

44+
interface ICheckForChangesOptions extends IPlatform, IProjectDataComposition {
45+
projectChangesOptions: IProjectChangesOptions
46+
}
47+
4448
interface IProjectChangesService {
45-
checkForChanges(platform: string, projectData: IProjectData, buildOptions: IProjectChangesOptions): Promise<IProjectChangesInfo>;
49+
checkForChanges(checkForChangesOpts: ICheckForChangesOptions): Promise<IProjectChangesInfo>;
4650
getPrepareInfo(platform: string, projectData: IProjectData): IPrepareInfo;
4751
savePrepareInfo(platform: string, projectData: IProjectData): void;
4852
getPrepareInfoFilePath(platform: string, projectData: IProjectData): string;

lib/services/platform-service.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,17 @@ export class PlatformService extends EventEmitter implements IPlatformService {
269269

270270
const bundle = appFilesUpdaterOptions.bundle;
271271
const nativePlatformStatus = (nativePrepare && nativePrepare.skipNativePrepare) ? constants.NativePlatformStatus.requiresPlatformAdd : constants.NativePlatformStatus.requiresPrepare;
272-
const changesInfo = await this.$projectChangesService.checkForChanges(platform, projectData, {
273-
bundle,
274-
release: appFilesUpdaterOptions.release,
275-
provision: config.provision,
276-
teamId: config.teamId,
277-
nativePlatformStatus,
278-
skipModulesNativeCheck: skipNativeCheckOptions.skipModulesNativeCheck
272+
const changesInfo = await this.$projectChangesService.checkForChanges({
273+
platform,
274+
projectData,
275+
projectChangesOptions: {
276+
bundle,
277+
release: appFilesUpdaterOptions.release,
278+
provision: config.provision,
279+
teamId: config.teamId,
280+
nativePlatformStatus,
281+
skipModulesNativeCheck: skipNativeCheckOptions.skipModulesNativeCheck
282+
}
279283
});
280284

281285
this.$logger.trace("Changes info in prepare platform:", changesInfo);

lib/services/project-changes-service.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import { NODE_MODULES_FOLDER_NAME, NativePlatformStatus, PACKAGE_JSON_FILE_NAME, APP_GRADLE_FILE_NAME, BUILD_XCCONFIG_FILE_NAME } from "../constants";
3-
import { getHash } from "../common/helpers";
3+
import { getHash, hook } from "../common/helpers";
44

55
const prepareInfoFileName = ".nsprepareinfo";
66

@@ -45,18 +45,25 @@ export class ProjectChangesService implements IProjectChangesService {
4545
private _outputProjectMtime: number;
4646
private _outputProjectCTime: number;
4747

48+
private get $hooksService(): IHooksService {
49+
return this.$injector.resolve<IHooksService>("hooksService");
50+
}
51+
4852
constructor(
4953
private $platformsData: IPlatformsData,
5054
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
5155
private $fs: IFileSystem,
52-
private $filesHashService: IFilesHashService) {
56+
private $filesHashService: IFilesHashService,
57+
private $injector: IInjector) {
5358
}
5459

5560
public get currentChanges(): IProjectChangesInfo {
5661
return this._changesInfo;
5762
}
5863

59-
public async checkForChanges(platform: string, projectData: IProjectData, projectChangesOptions: IProjectChangesOptions): Promise<IProjectChangesInfo> {
64+
@hook("checkForChanges")
65+
public async checkForChanges(checkForChangesOpts: ICheckForChangesOptions): Promise<IProjectChangesInfo> {
66+
const { platform, projectData, projectChangesOptions } = checkForChangesOpts;
6067
const platformData = this.$platformsData.getPlatformData(platform, projectData);
6168
this._changesInfo = new ProjectChangesInfo();
6269
const isNewPrepareInfo = await this.ensurePrepareInfo(platform, projectData, projectChangesOptions);

test/project-changes-service.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PlatformsData } from "../lib/platforms-data";
66
import { ProjectChangesService } from "../lib/services/project-changes-service";
77
import * as Constants from "../lib/constants";
88
import { FileSystem } from "../lib/common/file-system";
9+
import { HooksServiceStub } from "./stubs";
910

1011
// start tracking temporary folders/files
1112
temp.track();
@@ -36,6 +37,7 @@ class ProjectChangesServiceTest extends BaseServiceTest {
3637
this.injector.register("logger", {
3738
warn: () => ({})
3839
});
40+
this.injector.register("hooksService", HooksServiceStub);
3941

4042
const fs = this.injector.resolve<IFileSystem>("fs");
4143
fs.writeJson(path.join(this.projectDir, Constants.PACKAGE_JSON_FILE_NAME), {
@@ -149,9 +151,28 @@ describe("Project Changes Service Tests", () => {
149151

150152
describe("Accumulates Changes From Project Services", () => {
151153
it("accumulates changes from the project service", async () => {
152-
const iOSChanges = await serviceTest.projectChangesService.checkForChanges("ios", serviceTest.projectData, { bundle: false, release: false, provision: undefined, teamId: undefined });
154+
const iOSChanges = await serviceTest.projectChangesService.checkForChanges({
155+
platform: "ios",
156+
projectData: serviceTest.projectData,
157+
projectChangesOptions: {
158+
bundle: false,
159+
release: false,
160+
provision: undefined,
161+
teamId: undefined
162+
}
163+
});
153164
assert.isTrue(!!iOSChanges.signingChanged, "iOS signingChanged expected to be true");
154-
const androidChanges = await serviceTest.projectChangesService.checkForChanges("android", serviceTest.projectData, { bundle: false, release: false, provision: undefined, teamId: undefined });
165+
166+
const androidChanges = await serviceTest.projectChangesService.checkForChanges({
167+
platform: "android",
168+
projectData: serviceTest.projectData,
169+
projectChangesOptions: {
170+
bundle: false,
171+
release: false,
172+
provision: undefined,
173+
teamId: undefined
174+
}
175+
});
155176
assert.isFalse(!!androidChanges.signingChanged, "Android signingChanged expected to be false");
156177
});
157178
});

test/stubs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ export class ChildProcessStub {
651651
}
652652

653653
export class ProjectChangesService implements IProjectChangesService {
654-
public async checkForChanges(platform: string): Promise<IProjectChangesInfo> {
654+
public async checkForChanges(checkForChangesOpts: ICheckForChangesOptions): Promise<IProjectChangesInfo> {
655655
return <IProjectChangesInfo>{};
656656
}
657657

0 commit comments

Comments
 (0)