Skip to content

Commit b3b2044

Browse files
author
Dimitar Tachev
authored
Merge pull request #4092 from NativeScript/fatme/fix-platform-add-after-preview
fix(platform-add): respect nativePlatformStatus property from .nsprepare file when adding the platform
2 parents 8f80a3c + 6bee138 commit b3b2044

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/services/platform-service.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export class PlatformService extends EventEmitter implements IPlatformService {
6969
this.validatePlatform(platform, projectData);
7070
const platformPath = path.join(projectData.platformsDir, platform);
7171

72-
if (this.$fs.exists(platformPath)) {
72+
const isPlatformAdded = this.isPlatformAdded(platform, platformPath, projectData);
73+
if (isPlatformAdded) {
7374
this.$errors.failWithoutHelp(`Platform ${platform} already added`);
7475
}
7576

@@ -965,6 +966,19 @@ export class PlatformService extends EventEmitter implements IPlatformService {
965966

966967
return null;
967968
}
969+
970+
private isPlatformAdded(platform: string, platformPath: string, projectData: IProjectData): boolean {
971+
if (!this.$fs.exists(platformPath)) {
972+
return false;
973+
}
974+
975+
const prepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
976+
if (!prepareInfo) {
977+
return true;
978+
}
979+
980+
return prepareInfo.nativePlatformStatus !== constants.NativePlatformStatus.requiresPlatformAdd;
981+
}
968982
}
969983

970984
$injector.register("platformService", PlatformService);

test/platform-service.ts

+42
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,48 @@ describe('Platform Service Tests', () => {
302302

303303
await assertCorrectDataIsPassedToPacoteService(latestCompatibleVersion);
304304
});
305+
306+
// Workflow: tns preview; tns platform add
307+
it(`should add platform when only .js part of the platform has already been added (nativePlatformStatus is ${constants.NativePlatformStatus.requiresPlatformAdd})`, async () => {
308+
const fs = testInjector.resolve("fs");
309+
fs.exists = () => true;
310+
const projectChangesService = testInjector.resolve("projectChangesService");
311+
projectChangesService.getPrepareInfo = () => ({ nativePlatformStatus: constants.NativePlatformStatus.requiresPlatformAdd });
312+
const projectData = testInjector.resolve("projectData");
313+
let isJsPlatformAdded = false;
314+
const preparePlatformJSService = testInjector.resolve("preparePlatformJSService");
315+
preparePlatformJSService.addPlatform = async () => isJsPlatformAdded = true;
316+
let isNativePlatformAdded = false;
317+
const preparePlatformNativeService = testInjector.resolve("preparePlatformNativeService");
318+
preparePlatformNativeService.addPlatform = async () => isNativePlatformAdded = true;
319+
320+
await platformService.addPlatforms(["android"], "", projectData, config);
321+
322+
assert.isTrue(isJsPlatformAdded);
323+
assert.isTrue(isNativePlatformAdded);
324+
});
325+
326+
// Workflow: tns platform add; tns platform add
327+
it("shouldn't add platform when platforms folder exist and no .nsprepare file", async () => {
328+
const fs = testInjector.resolve("fs");
329+
fs.exists = () => true;
330+
const projectChangesService = testInjector.resolve("projectChangesService");
331+
projectChangesService.getPrepareInfo = () => <any>null;
332+
const projectData = testInjector.resolve("projectData");
333+
334+
await assert.isRejected(platformService.addPlatforms(["android"], "", projectData, config), "Platform android already added");
335+
});
336+
337+
// Workflow: tns run; tns platform add
338+
it(`shouldn't add platform when both native and .js parts of the platform have already been added (nativePlatformStatus is ${constants.NativePlatformStatus.alreadyPrepared})`, async () => {
339+
const fs = testInjector.resolve("fs");
340+
fs.exists = () => true;
341+
const projectChangesService = testInjector.resolve("projectChangesService");
342+
projectChangesService.getPrepareInfo = () => ({ nativePlatformStatus: constants.NativePlatformStatus.alreadyPrepared });
343+
const projectData = testInjector.resolve("projectData");
344+
345+
await assert.isRejected(platformService.addPlatforms(["android"], "", projectData, config), "Platform android already added");
346+
});
305347
});
306348
});
307349

0 commit comments

Comments
 (0)