Skip to content

Commit 82641ad

Browse files
committed
Fix update command and add tests.
1 parent be3d4f1 commit 82641ad

File tree

3 files changed

+384
-42
lines changed

3 files changed

+384
-42
lines changed

lib/commands/update.ts

+64-42
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,34 @@ export class UpdateCommand implements ICommand {
1515
this.$projectData.initializeProjectData();
1616
}
1717

18+
private folders: string[] = ["lib", "hooks", "platforms", "node_modules"];
19+
private tempFolder: string = ".tmp_backup";
20+
1821
public async execute(args: string[]): Promise<void> {
19-
const folders = ["lib", "hooks", "platforms", "node_modules"];
20-
const tmpDir = path.join(this.$projectData.projectDir, ".tmp_backup");
22+
const tmpDir = path.join(this.$projectData.projectDir, this.tempFolder);
2123

2224
try {
23-
shelljs.rm("-fr", tmpDir);
24-
shelljs.mkdir(tmpDir);
25-
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir);
26-
for (const folder of folders) {
27-
const folderToCopy = path.join(this.$projectData.projectDir, folder);
28-
if (this.$fs.exists(folderToCopy)) {
29-
shelljs.cp("-rf", folderToCopy, tmpDir);
30-
}
31-
}
25+
this.backup(tmpDir);
3226
} catch (error) {
3327
this.$logger.error("Could not backup project folders!");
28+
shelljs.rm("-fr", tmpDir);
3429
return;
3530
}
3631

3732
try {
38-
await this.executeCore(args, folders);
33+
await this.executeCore(args);
3934
} catch (error) {
40-
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir);
41-
for (const folder of folders) {
42-
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder));
43-
44-
const folderToCopy = path.join(tmpDir, folder);
45-
46-
if (this.$fs.exists(folderToCopy)) {
47-
shelljs.cp("-fr", folderToCopy, this.$projectData.projectDir);
48-
}
49-
}
50-
35+
this.restoreBackup(tmpDir);
5136
this.$logger.error("Could not update the project!");
5237
} finally {
5338
shelljs.rm("-fr", tmpDir);
5439
}
5540
}
5641

5742
public async canExecute(args: string[]): Promise<boolean> {
58-
for (const arg of args) {
59-
const platform = arg.split("@")[0];
60-
this.$platformService.validatePlatformInstalled(platform, this.$projectData);
43+
const platforms = this.getPlatforms();
44+
45+
for (const platform of platforms.packagePlatforms) {
6146
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
6247
const platformProjectService = platformData.platformProjectService;
6348
await platformProjectService.validate(this.$projectData);
@@ -66,42 +51,79 @@ export class UpdateCommand implements ICommand {
6651
return args.length < 2 && this.$projectData.projectDir !== "";
6752
}
6853

69-
private async executeCore(args: string[], folders: string[]): Promise<void> {
70-
let platforms = this.$platformService.getInstalledPlatforms(this.$projectData);
71-
const availablePlatforms = this.$platformService.getAvailablePlatforms(this.$projectData);
72-
const packagePlatforms: string[] = [];
54+
private async executeCore(args: string[]): Promise<void> {
55+
const platforms = this.getPlatforms();
7356

74-
for (const platform of availablePlatforms) {
57+
for (const platform of _.xor(platforms.installed, platforms.packagePlatforms)) {
7558
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
76-
const platformVersion = this.$projectDataService.getNSValue(this.$projectData.projectDir, platformData.frameworkPackageName);
77-
if (platformVersion) {
78-
packagePlatforms.push(platform);
79-
this.$projectDataService.removeNSProperty(this.$projectData.projectDir, platformData.frameworkPackageName);
80-
}
59+
this.$projectDataService.removeNSProperty(this.$projectData.projectDir, platformData.frameworkPackageName);
8160
}
8261

83-
await this.$platformService.removePlatforms(platforms, this.$projectData);
62+
await this.$platformService.removePlatforms(platforms.installed, this.$projectData);
8463
await this.$pluginsService.remove("tns-core-modules", this.$projectData);
8564
await this.$pluginsService.remove("tns-core-modules-widgets", this.$projectData);
8665

87-
for (const folder of folders) {
88-
shelljs.rm("-fr", folder);
66+
for (const folder of this.folders) {
67+
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder));
8968
}
9069

91-
platforms = platforms.concat(packagePlatforms);
9270
if (args.length === 1) {
93-
for (const platform of platforms) {
71+
for (const platform of platforms.packagePlatforms) {
9472
await this.$platformService.addPlatforms([platform + "@" + args[0]], this.$options.platformTemplate, this.$projectData, this.$options, this.$options.frameworkPath);
9573
}
9674

9775
await this.$pluginsService.add("tns-core-modules@" + args[0], this.$projectData);
9876
} else {
99-
await this.$platformService.addPlatforms(platforms, this.$options.platformTemplate, this.$projectData, this.$options, this.$options.frameworkPath);
77+
await this.$platformService.addPlatforms(platforms.packagePlatforms, this.$options.platformTemplate, this.$projectData, this.$options, this.$options.frameworkPath);
10078
await this.$pluginsService.add("tns-core-modules", this.$projectData);
10179
}
10280

10381
await this.$pluginsService.ensureAllDependenciesAreInstalled(this.$projectData);
10482
}
83+
84+
private getPlatforms(): {installed: string[], packagePlatforms: string[]} {
85+
const installedPlatforms = this.$platformService.getInstalledPlatforms(this.$projectData);
86+
const availablePlatforms = this.$platformService.getAvailablePlatforms(this.$projectData);
87+
const packagePlatforms: string[] = [];
88+
89+
for (const platform of availablePlatforms) {
90+
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
91+
const platformVersion = this.$projectDataService.getNSValue(this.$projectData.projectDir, platformData.frameworkPackageName);
92+
if (platformVersion) {
93+
packagePlatforms.push(platform);
94+
}
95+
}
96+
97+
return {
98+
installed: installedPlatforms,
99+
packagePlatforms: installedPlatforms.concat(packagePlatforms)
100+
};
101+
}
102+
103+
private restoreBackup(tmpDir: string): void {
104+
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir);
105+
for (const folder of this.folders) {
106+
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder));
107+
108+
const folderToCopy = path.join(tmpDir, folder);
109+
110+
if (this.$fs.exists(folderToCopy)) {
111+
shelljs.cp("-fr", folderToCopy, this.$projectData.projectDir);
112+
}
113+
}
114+
}
115+
116+
private backup(tmpDir: string): void {
117+
shelljs.rm("-fr", tmpDir);
118+
shelljs.mkdir(tmpDir);
119+
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir);
120+
for (const folder of this.folders) {
121+
const folderToCopy = path.join(this.$projectData.projectDir, folder);
122+
if (this.$fs.exists(folderToCopy)) {
123+
shelljs.cp("-rf", folderToCopy, tmpDir);
124+
}
125+
}
126+
}
105127
}
106128

107129
$injector.registerCommand("update", UpdateCommand);

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"analyze": true,
8686
"devDependencies": {
8787
"@types/chai": "4.0.1",
88+
"@types/sinon": "4.0.0",
8889
"@types/chai-as-promised": "0.0.31",
8990
"@types/chokidar": "1.6.0",
9091
"@types/lockfile": "1.0.0",
@@ -104,6 +105,7 @@
104105
"grunt-ts": "6.0.0-beta.16",
105106
"istanbul": "0.4.5",
106107
"mocha": "3.1.2",
108+
"sinon": "4.1.2",
107109
"should": "7.0.2",
108110
"source-map-support": "^0.4.14",
109111
"tslint": "5.4.3",

0 commit comments

Comments
 (0)