Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export class PlatformService extends EventEmitter implements IPlatformService {
this.validatePlatform(platform, projectData);
const platformPath = path.join(projectData.platformsDir, platform);

if (this.$fs.exists(platformPath)) {
const isPlatformAdded = this.isPlatformAdded(platform, platformPath, projectData);
if (isPlatformAdded) {
this.$errors.failWithoutHelp(`Platform ${platform} already added`);
}

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

return null;
}

private isPlatformAdded(platform: string, platformPath: string, projectData: IProjectData): boolean {
if (!this.$fs.exists(platformPath)) {
return false;
}

const prepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
if (!prepareInfo) {
return true;
}

return prepareInfo.nativePlatformStatus !== constants.NativePlatformStatus.requiresPlatformAdd;
}
}

$injector.register("platformService", PlatformService);
42 changes: 42 additions & 0 deletions test/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,48 @@ describe('Platform Service Tests', () => {

await assertCorrectDataIsPassedToPacoteService(latestCompatibleVersion);
});

// Workflow: tns preview; tns platform add
it(`should add platform when only .js part of the platform has already been added (nativePlatformStatus is ${constants.NativePlatformStatus.requiresPlatformAdd})`, async () => {
const fs = testInjector.resolve("fs");
fs.exists = () => true;
const projectChangesService = testInjector.resolve("projectChangesService");
projectChangesService.getPrepareInfo = () => ({ nativePlatformStatus: constants.NativePlatformStatus.requiresPlatformAdd });
const projectData = testInjector.resolve("projectData");
let isJsPlatformAdded = false;
const preparePlatformJSService = testInjector.resolve("preparePlatformJSService");
preparePlatformJSService.addPlatform = async () => isJsPlatformAdded = true;
let isNativePlatformAdded = false;
const preparePlatformNativeService = testInjector.resolve("preparePlatformNativeService");
preparePlatformNativeService.addPlatform = async () => isNativePlatformAdded = true;

await platformService.addPlatforms(["android"], "", projectData, config);

assert.isTrue(isJsPlatformAdded);
assert.isTrue(isNativePlatformAdded);
});

// Workflow: tns platform add; tns platform add
it("shouldn't add platform when platforms folder exist and no .nsprepare file", async () => {
const fs = testInjector.resolve("fs");
fs.exists = () => true;
const projectChangesService = testInjector.resolve("projectChangesService");
projectChangesService.getPrepareInfo = () => <any>null;
const projectData = testInjector.resolve("projectData");

await assert.isRejected(platformService.addPlatforms(["android"], "", projectData, config), "Platform android already added");
});

// Workflow: tns run; tns platform add
it(`shouldn't add platform when both native and .js parts of the platform have already been added (nativePlatformStatus is ${constants.NativePlatformStatus.alreadyPrepared})`, async () => {
const fs = testInjector.resolve("fs");
fs.exists = () => true;
const projectChangesService = testInjector.resolve("projectChangesService");
projectChangesService.getPrepareInfo = () => ({ nativePlatformStatus: constants.NativePlatformStatus.alreadyPrepared });
const projectData = testInjector.resolve("projectData");

await assert.isRejected(platformService.addPlatforms(["android"], "", projectData, config), "Platform android already added");
});
});
});

Expand Down