From d3f5d5490a875f520d78ea1eb95a2af399fce263 Mon Sep 17 00:00:00 2001 From: Tsvetan Raikov Date: Mon, 17 Oct 2016 16:26:14 +0300 Subject: [PATCH] Added tns update command --- .../man_pages/project/configuration/update.md | 20 +++++ lib/bootstrap.ts | 1 + lib/commands/update.ts | 77 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 docs/man_pages/project/configuration/update.md create mode 100644 lib/commands/update.ts diff --git a/docs/man_pages/project/configuration/update.md b/docs/man_pages/project/configuration/update.md new file mode 100644 index 0000000000..f7d3f5771e --- /dev/null +++ b/docs/man_pages/project/configuration/update.md @@ -0,0 +1,20 @@ +platform update +========== + +Usage | Synopsis +------|------- +Update with the latest version |`$ tns update` +Update with specific version | `$ tns update ` + +Updates a NativeScript project to the latest (or specified) version. + +### Related Commands + +Command | Description +----------|---------- +[install](install.html) | Installs all platforms and dependencies described in the `package.json` file in the current directory. +[platform add](platform-add.html) | Configures the current project to target the selected platform. +[platform remove](platform-remove.html) | Removes the selected platform from the platforms that the project currently targets. +[platform](platform.html) | Lists all platforms that the project currently targets. +[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. +[platform update](platform-update.html) | Updates the NativeScript runtime for the specified platform. \ No newline at end of file diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index 2c04947936..22fac968ce 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -118,5 +118,6 @@ $injector.require("xmlValidator", "./xml-validator"); $injector.requireCommand("devices", "./commands/devices"); $injector.requireCommand("post-install-cli", "./commands/post-install"); +$injector.requireCommand("update", "./commands/update"); $injector.require("iOSLogFilter", "./services/ios-log-filter"); diff --git a/lib/commands/update.ts b/lib/commands/update.ts new file mode 100644 index 0000000000..0bf51e0bd6 --- /dev/null +++ b/lib/commands/update.ts @@ -0,0 +1,77 @@ +import * as path from "path"; +import * as shelljs from "shelljs"; + +export class UpdateCommand implements ICommand { + constructor( + private $projectData: IProjectData, + private $platformService: IPlatformService, + private $pluginsService: IPluginsService, + private $logger: ILogger, + private $options: IOptions, + private $errors: IErrors) { } + + public execute(args: string[]): IFuture { + return (() => { + let folders = [ "lib", "hooks", "platforms", "node_modules" ]; + let tmpDir = path.join(this.$projectData.projectDir, ".tmp_backup"); + + try { + shelljs.rm("-fr", tmpDir); + shelljs.mkdir(tmpDir); + shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir); + for (let folder of folders) { + shelljs.cp("-rf", path.join(this.$projectData.projectDir, folder), tmpDir); + } + } catch(error) { + this.$logger.error("Could not backup project folders!"); + return; + } + + try { + this.executeCore(args, folders); + } catch (error) { + shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir); + for (let folder of folders) { + shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder)); + shelljs.cp("-fr", path.join(tmpDir, folder), this.$projectData.projectDir); + } + this.$logger.error("Could not update the project!"); + } finally { + shelljs.rm("-fr", tmpDir); + } + + }).future()(); + } + + public canExecute(args: string[]): IFuture { + return (() => { + return args.length < 2 && this.$projectData.projectDir !== ""; + }).future()(); + } + + private executeCore(args: string[], folders: string[]) { + let platforms = this.$platformService.getInstalledPlatforms().wait(); + + this.$platformService.removePlatforms(platforms).wait(); + this.$pluginsService.remove("tns-core-modules").wait(); + this.$pluginsService.remove("tns-core-modules-widgets").wait(); + + for (let folder of folders) { + shelljs.rm("-fr", folder); + } + + if (args.length === 1) { + for (let platform of platforms) { + this.$platformService.addPlatforms([ platform+"@"+args[0] ]).wait(); + } + this.$pluginsService.add("tns-core-modules@" + args[0]).wait(); + } else { + this.$platformService.addPlatforms(platforms).wait(); + this.$pluginsService.add("tns-core-modules").wait(); + } + this.$pluginsService.ensureAllDependenciesAreInstalled().wait(); + } + + allowedParameters: ICommandParameter[] = []; +} +$injector.registerCommand("update", UpdateCommand);