Skip to content

Commit d3f5d54

Browse files
author
Tsvetan Raikov
committed
Added tns update command
1 parent 6b049a8 commit d3f5d54

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
platform update
2+
==========
3+
4+
Usage | Synopsis
5+
------|-------
6+
Update with the latest version |`$ tns update`
7+
Update with specific version | `$ tns update <version>`
8+
9+
Updates a NativeScript project to the latest (or specified) version.
10+
11+
### Related Commands
12+
13+
Command | Description
14+
----------|----------
15+
[install](install.html) | Installs all platforms and dependencies described in the `package.json` file in the current directory.
16+
[platform add](platform-add.html) | Configures the current project to target the selected platform.
17+
[platform remove](platform-remove.html) | Removes the selected platform from the platforms that the project currently targets.
18+
[platform](platform.html) | Lists all platforms that the project currently targets.
19+
[prepare](prepare.html) | Copies common and relevant platform-specific content from the app directory to the subdirectory for the selected target platform in the platforms directory.
20+
[platform update](platform-update.html) | Updates the NativeScript runtime for the specified platform.

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,6 @@ $injector.require("xmlValidator", "./xml-validator");
118118

119119
$injector.requireCommand("devices", "./commands/devices");
120120
$injector.requireCommand("post-install-cli", "./commands/post-install");
121+
$injector.requireCommand("update", "./commands/update");
121122

122123
$injector.require("iOSLogFilter", "./services/ios-log-filter");

lib/commands/update.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as path from "path";
2+
import * as shelljs from "shelljs";
3+
4+
export class UpdateCommand implements ICommand {
5+
constructor(
6+
private $projectData: IProjectData,
7+
private $platformService: IPlatformService,
8+
private $pluginsService: IPluginsService,
9+
private $logger: ILogger,
10+
private $options: IOptions,
11+
private $errors: IErrors) { }
12+
13+
public execute(args: string[]): IFuture<void> {
14+
return (() => {
15+
let folders = [ "lib", "hooks", "platforms", "node_modules" ];
16+
let tmpDir = path.join(this.$projectData.projectDir, ".tmp_backup");
17+
18+
try {
19+
shelljs.rm("-fr", tmpDir);
20+
shelljs.mkdir(tmpDir);
21+
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir);
22+
for (let folder of folders) {
23+
shelljs.cp("-rf", path.join(this.$projectData.projectDir, folder), tmpDir);
24+
}
25+
} catch(error) {
26+
this.$logger.error("Could not backup project folders!");
27+
return;
28+
}
29+
30+
try {
31+
this.executeCore(args, folders);
32+
} catch (error) {
33+
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir);
34+
for (let folder of folders) {
35+
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder));
36+
shelljs.cp("-fr", path.join(tmpDir, folder), this.$projectData.projectDir);
37+
}
38+
this.$logger.error("Could not update the project!");
39+
} finally {
40+
shelljs.rm("-fr", tmpDir);
41+
}
42+
43+
}).future<void>()();
44+
}
45+
46+
public canExecute(args: string[]): IFuture<boolean> {
47+
return (() => {
48+
return args.length < 2 && this.$projectData.projectDir !== "";
49+
}).future<boolean>()();
50+
}
51+
52+
private executeCore(args: string[], folders: string[]) {
53+
let platforms = this.$platformService.getInstalledPlatforms().wait();
54+
55+
this.$platformService.removePlatforms(platforms).wait();
56+
this.$pluginsService.remove("tns-core-modules").wait();
57+
this.$pluginsService.remove("tns-core-modules-widgets").wait();
58+
59+
for (let folder of folders) {
60+
shelljs.rm("-fr", folder);
61+
}
62+
63+
if (args.length === 1) {
64+
for (let platform of platforms) {
65+
this.$platformService.addPlatforms([ platform+"@"+args[0] ]).wait();
66+
}
67+
this.$pluginsService.add("tns-core-modules@" + args[0]).wait();
68+
} else {
69+
this.$platformService.addPlatforms(platforms).wait();
70+
this.$pluginsService.add("tns-core-modules").wait();
71+
}
72+
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
73+
}
74+
75+
allowedParameters: ICommandParameter[] = [];
76+
}
77+
$injector.registerCommand("update", UpdateCommand);

0 commit comments

Comments
 (0)