|
| 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