Skip to content

Commit 5f50f67

Browse files
committed
fix: fix plugin development workflow
Chokidar raises changed event when `.js` file is changed from within symlinked plugin and this led to a native build on every .js change for plugin's developer. As a workaround, we checked if there are native changes and rebuild the project only in this case.
1 parent dbd37f3 commit 5f50f67

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

lib/controllers/prepare-controller.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { hook } from "../common/helpers";
44
import { performanceLog } from "../common/decorators";
55
import { EventEmitter } from "events";
66
import * as path from "path";
7-
import { PREPARE_READY_EVENT_NAME, WEBPACK_COMPILATION_COMPLETE } from "../constants";
7+
import { PREPARE_READY_EVENT_NAME, WEBPACK_COMPILATION_COMPLETE, PACKAGE_JSON_FILE_NAME } from "../constants";
88

99
interface IPlatformWatcherData {
1010
webpackCompilerProcess: child_process.ChildProcess;
@@ -115,6 +115,8 @@ export class PrepareController extends EventEmitter {
115115
}
116116

117117
const patterns = [
118+
path.join(projectData.projectDir, PACKAGE_JSON_FILE_NAME),
119+
path.join(projectData.getAppDirectoryPath(), PACKAGE_JSON_FILE_NAME),
118120
path.join(projectData.getAppResourcesRelativeDirectoryPath(), platformData.normalizedPlatformName),
119121
`node_modules/**/platforms/${platformData.platformNameLowerCase}/`
120122
];

lib/controllers/run-controller.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ export class RunController extends EventEmitter implements IRunController {
302302

303303
try {
304304
if (data.hasNativeChanges) {
305-
await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData);
306-
await deviceDescriptor.buildAction();
305+
if (await this.$prepareNativePlatformService.prepareNativePlatform(platformData, projectData, prepareData)) {
306+
await deviceDescriptor.buildAction();
307+
}
307308
}
308309

309310
const isInHMRMode = liveSyncInfo.useHotModuleReload && data.hmrData && data.hmrData.hash;

lib/definitions/project-changes.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ interface IPrepareInfo extends IAddedNativePlatform, IAppFilesHashes {
1515
interface IProjectChangesInfo extends IAddedNativePlatform {
1616
appResourcesChanged: boolean;
1717
configChanged: boolean;
18-
packageChanged: boolean;
1918
nativeChanged: boolean;
2019
signingChanged: boolean;
2120

lib/services/platform/prepare-native-platform-service.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ export class PrepareNativePlatformService implements IPrepareNativePlatformServi
2222
const changesInfo = await this.$projectChangesService.checkForChanges(platformData, projectData, prepareData);
2323

2424
const hasNativeModulesChange = !changesInfo || changesInfo.nativeChanged;
25-
const hasPackageChange = !changesInfo || changesInfo.packageChanged;
2625
const hasConfigChange = !changesInfo || changesInfo.configChanged;
2726
const hasChangesRequirePrepare = !changesInfo || changesInfo.changesRequirePrepare;
2827

29-
const hasChanges = hasNativeModulesChange || hasPackageChange || hasConfigChange || hasChangesRequirePrepare;
28+
const hasChanges = hasNativeModulesChange || hasConfigChange || hasChangesRequirePrepare;
3029

3130
if (changesInfo.hasChanges) {
3231
await this.cleanProject(platformData, { release });
@@ -38,11 +37,11 @@ export class PrepareNativePlatformService implements IPrepareNativePlatformServi
3837
await platformData.platformProjectService.prepareProject(projectData, prepareData);
3938
}
4039

41-
if (hasNativeModulesChange || hasPackageChange) {
40+
if (hasNativeModulesChange) {
4241
await this.$nodeModulesBuilder.prepareNodeModules(platformData, projectData);
4342
}
4443

45-
if (hasNativeModulesChange || hasPackageChange || hasConfigChange) {
44+
if (hasNativeModulesChange || hasConfigChange) {
4645
await platformData.platformProjectService.processConfigurationFilesFromAppResources(projectData, { release });
4746
await platformData.platformProjectService.handleNativeDependenciesChange(projectData, { release });
4847
}

lib/services/project-changes-service.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path";
2-
import { NativePlatformStatus, PACKAGE_JSON_FILE_NAME, APP_GRADLE_FILE_NAME, BUILD_XCCONFIG_FILE_NAME } from "../constants";
2+
import { NativePlatformStatus, PACKAGE_JSON_FILE_NAME, APP_GRADLE_FILE_NAME, BUILD_XCCONFIG_FILE_NAME, PLATFORMS_DIR_NAME } from "../constants";
33
import { getHash, hook } from "../common/helpers";
44
import { PrepareData } from "../data/prepare-data";
55

@@ -9,21 +9,19 @@ class ProjectChangesInfo implements IProjectChangesInfo {
99

1010
public appResourcesChanged: boolean;
1111
public configChanged: boolean;
12-
public packageChanged: boolean;
1312
public nativeChanged: boolean;
1413
public signingChanged: boolean;
1514
public nativePlatformStatus: NativePlatformStatus;
1615

1716
public get hasChanges(): boolean {
18-
return this.packageChanged ||
17+
return this.nativeChanged ||
1918
this.appResourcesChanged ||
2019
this.configChanged ||
2120
this.signingChanged;
2221
}
2322

2423
public get changesRequireBuild(): boolean {
25-
return this.packageChanged ||
26-
this.appResourcesChanged ||
24+
return this.appResourcesChanged ||
2725
this.nativeChanged;
2826
}
2927

@@ -57,18 +55,23 @@ export class ProjectChangesService implements IProjectChangesService {
5755
this._changesInfo = new ProjectChangesInfo();
5856
const isNewPrepareInfo = await this.ensurePrepareInfo(platformData, projectData, prepareData);
5957
if (!isNewPrepareInfo) {
60-
this._changesInfo.packageChanged = this.isProjectFileChanged(projectData.projectDir, platformData);
61-
6258
const platformResourcesDir = path.join(projectData.appResourcesDirectoryPath, platformData.normalizedPlatformName);
6359
this._changesInfo.appResourcesChanged = this.containsNewerFiles(platformResourcesDir, projectData);
6460

6561
this.$nodeModulesDependenciesBuilder.getProductionDependencies(projectData.projectDir)
66-
.filter(dep => dep.nativescript && this.$fs.exists(path.join(dep.directory, "platforms", platformData.platformNameLowerCase)))
62+
.filter(dep => dep.nativescript && this.$fs.exists(path.join(dep.directory, PLATFORMS_DIR_NAME, platformData.platformNameLowerCase)))
6763
.map(dep => {
6864
this._changesInfo.nativeChanged = this._changesInfo.nativeChanged ||
69-
this.containsNewerFiles(path.join(dep.directory, "platforms", platformData.platformNameLowerCase), projectData);
65+
this.containsNewerFiles(path.join(dep.directory, PLATFORMS_DIR_NAME, platformData.platformNameLowerCase), projectData) ||
66+
this.isFileModified(path.join(dep.directory, PACKAGE_JSON_FILE_NAME));
7067
});
7168

69+
if (!this._changesInfo.nativeChanged) {
70+
const packageJsonChanged = this.isProjectFileChanged(projectData.projectDir, platformData);
71+
this._prepareInfo.projectFileHash = this.getProjectFileStrippedHash(projectData.projectDir, platformData);
72+
this._changesInfo.nativeChanged = packageJsonChanged;
73+
}
74+
7275
this.$logger.trace(`Set nativeChanged to ${this._changesInfo.nativeChanged}.`);
7376

7477
if (platformData.platformNameLowerCase === this.$devicePlatformsConstants.iOS.toLowerCase()) {
@@ -106,8 +109,6 @@ export class ProjectChangesService implements IProjectChangesService {
106109
if (this._prepareInfo.changesRequireBuild) {
107110
this._prepareInfo.changesRequireBuildTime = this._prepareInfo.time;
108111
}
109-
110-
this._prepareInfo.projectFileHash = this.getProjectFileStrippedHash(projectData.projectDir, platformData);
111112
}
112113

113114
this._changesInfo.nativePlatformStatus = this._prepareInfo.nativePlatformStatus;
@@ -223,8 +224,7 @@ export class ProjectChangesService implements IProjectChangesService {
223224
return false;
224225
}
225226

226-
const dirFileStat = this.$fs.getFsStats(dir);
227-
if (this.isFileModified(dirFileStat, dir)) {
227+
if (this.isFileModified(dir)) {
228228
this.$logger.trace(`containsNewerFiles returns true for ${dir} as the dir itself has been modified.`);
229229
return true;
230230
}
@@ -234,7 +234,7 @@ export class ProjectChangesService implements IProjectChangesService {
234234
const filePath = path.join(dir, file);
235235

236236
const fileStats = this.$fs.getFsStats(filePath);
237-
const changed = this.isFileModified(fileStats, filePath);
237+
const changed = this.isFileModified(filePath, fileStats);
238238

239239
if (changed) {
240240
this.$logger.trace(`containsNewerFiles returns true for ${dir}. The modified file is ${filePath}`);
@@ -253,9 +253,10 @@ export class ProjectChangesService implements IProjectChangesService {
253253
return false;
254254
}
255255

256-
private isFileModified(filePathStat: IFsStats, filePath: string): boolean {
257-
let changed = filePathStat.mtime.getTime() >= this._outputProjectMtime ||
258-
filePathStat.ctime.getTime() >= this._outputProjectCTime;
256+
private isFileModified(filePath: string, filePathStats?: IFsStats): boolean {
257+
filePathStats = filePathStats || this.$fs.getFsStats(filePath);
258+
let changed = filePathStats.mtime.getTime() >= this._outputProjectMtime ||
259+
filePathStats.ctime.getTime() >= this._outputProjectCTime;
259260

260261
if (!changed) {
261262
const lFileStats = this.$fs.getLsStats(filePath);

0 commit comments

Comments
 (0)