Skip to content

Commit 8dedbcd

Browse files
authored
platform clean should preserve the nativescript version used #2496 (#2615)
* Move clean command logic to platform service * Preserve the version of {N} on clean * extract function that gets project version * Do not submit undefined if version is missing
1 parent f45ac04 commit 8dedbcd

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

lib/commands/platform-clean.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export class CleanCommand implements ICommand {
99
}
1010

1111
public async execute(args: string[]): Promise<void> {
12-
await this.$platformService.removePlatforms(args, this.$projectData);
13-
await this.$platformService.addPlatforms(args, this.$options.platformTemplate, this.$projectData, { provision: this.$options.provision, sdk: this.$options.sdk });
12+
await this.$platformService.cleanPlatforms(args, this.$options.platformTemplate, this.$projectData, {provision: this.$options.provision, sdk: this.$options.sdk });
1413
}
1514

1615
public async canExecute(args: string[]): Promise<boolean> {

lib/definitions/platform.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
interface IPlatformService extends NodeJS.EventEmitter {
2+
cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framework?: string): Promise<void>;
3+
24
addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise<void>;
35

46
/**

lib/services/platform-service.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ export class PlatformService extends EventEmitter implements IPlatformService {
4646
super();
4747
}
4848

49+
public async cleanPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, framworkPath?: string): Promise<void> {
50+
for (let platform of platforms) {
51+
let version: string = this.getCurrentPlatformVersion(platform, projectData);
52+
53+
let platformWithVersion: string = platform;
54+
if (version !== undefined) {
55+
platformWithVersion += "@" + version;
56+
}
57+
58+
await this.removePlatforms([platform], projectData);
59+
await this.addPlatforms([platformWithVersion], platformTemplate, projectData, platformSpecificData);
60+
}
61+
}
62+
4963
public async addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise<void> {
5064
let platformsDir = projectData.platformsDir;
5165
this.$fs.ensureDirectoryExists(platformsDir);
@@ -55,6 +69,17 @@ export class PlatformService extends EventEmitter implements IPlatformService {
5569
}
5670
}
5771

72+
private getCurrentPlatformVersion(platform: string, projectData: IProjectData) : string {
73+
let platformData = this.$platformsData.getPlatformData(platform, projectData);
74+
let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
75+
let version: string;
76+
if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) {
77+
version = currentPlatformData[constants.VERSION_STRING];
78+
};
79+
80+
return version;
81+
}
82+
5883
private async addPlatform(platformParam: string, platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData, frameworkPath?: string): Promise<void> {
5984
let data = platformParam.split("@"),
6085
platform = data[0].toLowerCase(),
@@ -69,9 +94,9 @@ export class PlatformService extends EventEmitter implements IPlatformService {
6994
}
7095

7196
let platformData = this.$platformsData.getPlatformData(platform, projectData);
72-
let currentPlatformData: any = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
73-
if (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) {
74-
version = currentPlatformData[constants.VERSION_STRING];
97+
98+
if (version === undefined) {
99+
version = this.getCurrentPlatformVersion(platform, projectData);
75100
}
76101

77102
// Copy platform specific files in platforms dir

test/platform-service.ts

+30
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,36 @@ describe('Platform Service Tests', () => {
245245
});
246246
});
247247

248+
describe("clean platform unit tests", () => {
249+
it("should preserve the specified in the project nativescript version", async () => {
250+
const versionString = "2.4.1";
251+
let fs = testInjector.resolve("fs");
252+
fs.exists = () => false;
253+
254+
let nsValueObject: any = {};
255+
nsValueObject[VERSION_STRING] = versionString;
256+
let projectDataService = testInjector.resolve("projectDataService");
257+
projectDataService.getNSValue = () => nsValueObject;
258+
259+
let npmInstallationManager = testInjector.resolve("npmInstallationManager");
260+
npmInstallationManager.install = (packageName: string, packageDir: string, options: INpmInstallOptions) => {
261+
assert.deepEqual(options.version, versionString);
262+
return "";
263+
};
264+
265+
let projectData: IProjectData = testInjector.resolve("projectData");
266+
platformService.removePlatforms = (platforms: string[], prjctData: IProjectData): Promise<void> => {
267+
nsValueObject[VERSION_STRING] = undefined;
268+
return Promise.resolve();
269+
};
270+
271+
await platformService.cleanPlatforms(["android"], "", projectData, null);
272+
273+
nsValueObject[VERSION_STRING] = versionString;
274+
await platformService.cleanPlatforms(["ios"], "", projectData, null);
275+
});
276+
});
277+
248278
// TODO: Commented as it doesn't seem correct. Check what's the case and why it's been expected to fail.
249279
// describe("list platform unit tests", () => {
250280
// it("fails when platforms are not added", () => {

test/stubs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ export class PlatformServiceStub extends EventEmitter implements IPlatformServic
585585
return Promise.resolve(true);
586586
}
587587

588+
public cleanPlatforms(platforms: string[]): Promise<void> {
589+
return Promise.resolve();
590+
}
591+
588592
public addPlatforms(platforms: string[]): Promise<void> {
589593
return Promise.resolve();
590594
}

0 commit comments

Comments
 (0)