diff --git a/lib/commands/platform-clean.ts b/lib/commands/platform-clean.ts index 2918d9c396..579e4b1040 100644 --- a/lib/commands/platform-clean.ts +++ b/lib/commands/platform-clean.ts @@ -9,8 +9,7 @@ export class CleanCommand implements ICommand { } public async execute(args: string[]): Promise { - await this.$platformService.removePlatforms(args, this.$projectData); - await this.$platformService.addPlatforms(args, this.$options.platformTemplate, this.$projectData, { provision: this.$options.provision, sdk: this.$options.sdk }); + await this.$platformService.cleanPlatforms(args, this.$options.platformTemplate, this.$projectData, {provision: this.$options.provision, sdk: this.$options.sdk }); } public async canExecute(args: string[]): Promise { diff --git a/lib/definitions/platform.d.ts b/lib/definitions/platform.d.ts index 9268d5cca8..3ace46f88a 100644 --- a/lib/definitions/platform.d.ts +++ b/lib/definitions/platform.d.ts @@ -1,4 +1,6 @@ interface IPlatformService extends NodeJS.EventEmitter { + cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framework?: string): Promise; + addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise; /** diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index eb5f71fd18..92531e2a0a 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -46,6 +46,20 @@ export class PlatformService extends EventEmitter implements IPlatformService { super(); } + public async cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framworkPath?: string): Promise { + for (let platform of platforms) { + let version: string = this.getCurrentPlatformVersion(platform, projectData); + + let platformWithVersion: string = platform; + if (version !== undefined) { + platformWithVersion += "@" + version; + } + + await this.removePlatforms([platform], projectData); + await this.addPlatforms([platformWithVersion], platformTemplate, projectData, platformSpecificData); + } + } + public async addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise { let platformsDir = projectData.platformsDir; this.$fs.ensureDirectoryExists(platformsDir); @@ -55,6 +69,17 @@ export class PlatformService extends EventEmitter implements IPlatformService { } } + private getCurrentPlatformVersion(platform: string, projectData: IProjectData) : string { + let platformData = this.$platformsData.getPlatformData(platform, projectData); + let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); + let version: string; + if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { + version = currentPlatformData[constants.VERSION_STRING]; + }; + + return version; + } + private async addPlatform(platformParam: string, platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise { let data = platformParam.split("@"), platform = data[0].toLowerCase(), @@ -69,9 +94,9 @@ export class PlatformService extends EventEmitter implements IPlatformService { } let platformData = this.$platformsData.getPlatformData(platform, projectData); - let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName); - if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) { - version = currentPlatformData[constants.VERSION_STRING]; + + if (version === undefined) { + version = this.getCurrentPlatformVersion(platform, projectData); } // Copy platform specific files in platforms dir diff --git a/test/platform-service.ts b/test/platform-service.ts index 94aca1a1e1..67f0930677 100644 --- a/test/platform-service.ts +++ b/test/platform-service.ts @@ -245,6 +245,36 @@ describe('Platform Service Tests', () => { }); }); + describe("clean platform unit tests", () => { + it("should preserve the specified in the project nativescript version", async () => { + const versionString = "2.4.1"; + let fs = testInjector.resolve("fs"); + fs.exists = () => false; + + let nsValueObject: any = {}; + nsValueObject[VERSION_STRING] = versionString; + let projectDataService = testInjector.resolve("projectDataService"); + projectDataService.getNSValue = () => nsValueObject; + + let npmInstallationManager = testInjector.resolve("npmInstallationManager"); + npmInstallationManager.install = (packageName: string, packageDir: string, options: INpmInstallOptions) => { + assert.deepEqual(options.version, versionString); + return ""; + }; + + let projectData: IProjectData = testInjector.resolve("projectData"); + platformService.removePlatforms = (platforms: string[], prjctData: IProjectData): Promise => { + nsValueObject[VERSION_STRING] = undefined; + return Promise.resolve(); + }; + + await platformService.cleanPlatforms(["android"], "", projectData, null); + + nsValueObject[VERSION_STRING] = versionString; + await platformService.cleanPlatforms(["ios"], "", projectData, null); + }); + }); + // TODO: Commented as it doesn't seem correct. Check what's the case and why it's been expected to fail. // describe("list platform unit tests", () => { // it("fails when platforms are not added", () => { diff --git a/test/stubs.ts b/test/stubs.ts index f3aa5bce99..800c41d1d4 100644 --- a/test/stubs.ts +++ b/test/stubs.ts @@ -581,6 +581,10 @@ export class PlatformServiceStub extends EventEmitter implements IPlatformServic return Promise.resolve(true); } + public cleanPlatforms(platforms: string[]): Promise { + return Promise.resolve(); + } + public addPlatforms(platforms: string[]): Promise { return Promise.resolve(); }