|
1 |
| -import * as path from "path"; |
2 |
| -import * as constants from "../constants"; |
3 |
| -import { ValidatePlatformCommandBase } from "./command-base"; |
4 |
| - |
5 |
| -export class UpdateCommand extends ValidatePlatformCommandBase implements ICommand { |
| 1 | +export class UpdateCommand implements ICommand { |
6 | 2 | public allowedParameters: ICommandParameter[] = [];
|
| 3 | + public static readonly SHOULD_MIGRATE_PROJECT_MESSAGE = 'This project is not compatible with the current NativeScript version and cannot be updated. Use "tns migrate" to make your project compatible.'; |
| 4 | + public static readonly PROJECT_UP_TO_DATE_MESSAGE = 'This project is up to date.'; |
7 | 5 |
|
8 | 6 | constructor(
|
9 |
| - private $fs: IFileSystem, |
10 |
| - private $logger: ILogger, |
11 |
| - $options: IOptions, |
12 |
| - private $platformCommandHelper: IPlatformCommandHelper, |
13 |
| - $platformsDataService: IPlatformsDataService, |
14 |
| - $platformValidationService: IPlatformValidationService, |
15 |
| - private $pluginsService: IPluginsService, |
16 |
| - $projectData: IProjectData, |
17 |
| - private $projectDataService: IProjectDataService) { |
18 |
| - super($options, $platformsDataService, $platformValidationService, $projectData); |
| 7 | + private $updateController: IUpdateController, |
| 8 | + private $migrateController: IMigrateController, |
| 9 | + private $options: IOptions, |
| 10 | + private $errors: IErrors, |
| 11 | + private $projectData: IProjectData) { |
19 | 12 | this.$projectData.initializeProjectData();
|
20 | 13 | }
|
21 | 14 |
|
22 |
| - static readonly folders: string[] = [ |
23 |
| - constants.LIB_DIR_NAME, |
24 |
| - constants.HOOKS_DIR_NAME, |
25 |
| - constants.PLATFORMS_DIR_NAME, |
26 |
| - constants.NODE_MODULES_FOLDER_NAME |
27 |
| - ]; |
28 |
| - static readonly tempFolder: string = ".tmp_backup"; |
29 |
| - static readonly updateFailMessage: string = "Could not update the project!"; |
30 |
| - static readonly backupFailMessage: string = "Could not backup project folders!"; |
31 |
| - |
32 | 15 | public async execute(args: string[]): Promise<void> {
|
33 |
| - const tmpDir = path.join(this.$projectData.projectDir, UpdateCommand.tempFolder); |
34 |
| - |
35 |
| - try { |
36 |
| - this.backup(tmpDir); |
37 |
| - } catch (error) { |
38 |
| - this.$logger.error(UpdateCommand.backupFailMessage); |
39 |
| - this.$fs.deleteDirectory(tmpDir); |
40 |
| - return; |
41 |
| - } |
42 |
| - |
43 |
| - try { |
44 |
| - await this.executeCore(args); |
45 |
| - } catch (error) { |
46 |
| - this.restoreBackup(tmpDir); |
47 |
| - this.$logger.error(UpdateCommand.updateFailMessage); |
48 |
| - } finally { |
49 |
| - this.$fs.deleteDirectory(tmpDir); |
50 |
| - } |
| 16 | + await this.$updateController.update({projectDir: this.$projectData.projectDir, version: args[0], frameworkPath: this.$options.frameworkPath}); |
51 | 17 | }
|
52 | 18 |
|
53 |
| - public async canExecute(args: string[]): Promise<ICanExecuteCommandOutput> { |
54 |
| - const platforms = this.getPlatforms(); |
55 |
| - |
56 |
| - let canExecute = true; |
57 |
| - for (const platform of platforms.packagePlatforms) { |
58 |
| - const output = await super.canExecuteCommandBase(platform); |
59 |
| - canExecute = canExecute && output.canExecute; |
60 |
| - } |
61 |
| - |
62 |
| - let result = null; |
63 |
| - |
64 |
| - if (canExecute) { |
65 |
| - result = { |
66 |
| - canExecute: args.length < 2 && this.$projectData.projectDir !== "", |
67 |
| - suppressCommandHelp: false |
68 |
| - }; |
69 |
| - } else { |
70 |
| - result = { |
71 |
| - canExecute: false, |
72 |
| - suppressCommandHelp: true |
73 |
| - }; |
74 |
| - } |
75 |
| - |
76 |
| - return result; |
77 |
| - } |
78 |
| - |
79 |
| - private async executeCore(args: string[]): Promise<void> { |
80 |
| - const platforms = this.getPlatforms(); |
81 |
| - |
82 |
| - for (const platform of _.xor(platforms.installed, platforms.packagePlatforms)) { |
83 |
| - const platformData = this.$platformsDataService.getPlatformData(platform, this.$projectData); |
84 |
| - this.$projectDataService.removeNSProperty(this.$projectData.projectDir, platformData.frameworkPackageName); |
85 |
| - } |
86 |
| - |
87 |
| - await this.$platformCommandHelper.removePlatforms(platforms.installed, this.$projectData); |
88 |
| - await this.$pluginsService.remove(constants.TNS_CORE_MODULES_NAME, this.$projectData); |
89 |
| - if (!!this.$projectData.dependencies[constants.TNS_CORE_MODULES_WIDGETS_NAME]) { |
90 |
| - await this.$pluginsService.remove(constants.TNS_CORE_MODULES_WIDGETS_NAME, this.$projectData); |
| 19 | + public async canExecute(args: string[]): Promise<boolean> { |
| 20 | + if (await this.$migrateController.shouldMigrate({projectDir: this.$projectData.projectDir})) { |
| 21 | + this.$errors.failWithoutHelp(UpdateCommand.SHOULD_MIGRATE_PROJECT_MESSAGE); |
91 | 22 | }
|
92 | 23 |
|
93 |
| - for (const folder of UpdateCommand.folders) { |
94 |
| - this.$fs.deleteDirectory(path.join(this.$projectData.projectDir, folder)); |
| 24 | + if (!await this.$updateController.shouldUpdate({projectDir:this.$projectData.projectDir, version: args[0]})) { |
| 25 | + this.$errors.failWithoutHelp(UpdateCommand.PROJECT_UP_TO_DATE_MESSAGE); |
95 | 26 | }
|
96 | 27 |
|
97 |
| - if (args.length === 1) { |
98 |
| - for (const platform of platforms.packagePlatforms) { |
99 |
| - await this.$platformCommandHelper.addPlatforms([platform + "@" + args[0]], this.$projectData, this.$options.frameworkPath); |
100 |
| - } |
101 |
| - |
102 |
| - await this.$pluginsService.add(`${constants.TNS_CORE_MODULES_NAME}@${args[0]}`, this.$projectData); |
103 |
| - } else { |
104 |
| - await this.$platformCommandHelper.addPlatforms(platforms.packagePlatforms, this.$projectData, this.$options.frameworkPath); |
105 |
| - await this.$pluginsService.add(constants.TNS_CORE_MODULES_NAME, this.$projectData); |
106 |
| - } |
107 |
| - |
108 |
| - await this.$pluginsService.ensureAllDependenciesAreInstalled(this.$projectData); |
109 |
| - } |
110 |
| - |
111 |
| - private getPlatforms(): { installed: string[], packagePlatforms: string[] } { |
112 |
| - const installedPlatforms = this.$platformCommandHelper.getInstalledPlatforms(this.$projectData); |
113 |
| - const availablePlatforms = this.$platformCommandHelper.getAvailablePlatforms(this.$projectData); |
114 |
| - const packagePlatforms: string[] = []; |
115 |
| - |
116 |
| - for (const platform of availablePlatforms) { |
117 |
| - const platformData = this.$platformsDataService.getPlatformData(platform, this.$projectData); |
118 |
| - const platformVersion = this.$projectDataService.getNSValue(this.$projectData.projectDir, platformData.frameworkPackageName); |
119 |
| - if (platformVersion) { |
120 |
| - packagePlatforms.push(platform); |
121 |
| - } |
122 |
| - } |
123 |
| - |
124 |
| - return { |
125 |
| - installed: installedPlatforms, |
126 |
| - packagePlatforms: installedPlatforms.concat(packagePlatforms) |
127 |
| - }; |
128 |
| - } |
129 |
| - |
130 |
| - private restoreBackup(tmpDir: string): void { |
131 |
| - this.$fs.copyFile(path.join(tmpDir, constants.PACKAGE_JSON_FILE_NAME), this.$projectData.projectDir); |
132 |
| - for (const folder of UpdateCommand.folders) { |
133 |
| - this.$fs.deleteDirectory(path.join(this.$projectData.projectDir, folder)); |
134 |
| - |
135 |
| - const folderToCopy = path.join(tmpDir, folder); |
136 |
| - |
137 |
| - if (this.$fs.exists(folderToCopy)) { |
138 |
| - this.$fs.copyFile(folderToCopy, this.$projectData.projectDir); |
139 |
| - } |
140 |
| - } |
141 |
| - } |
142 |
| - |
143 |
| - private backup(tmpDir: string): void { |
144 |
| - this.$fs.deleteDirectory(tmpDir); |
145 |
| - this.$fs.createDirectory(tmpDir); |
146 |
| - this.$fs.copyFile(path.join(this.$projectData.projectDir, constants.PACKAGE_JSON_FILE_NAME), tmpDir); |
147 |
| - for (const folder of UpdateCommand.folders) { |
148 |
| - const folderToCopy = path.join(this.$projectData.projectDir, folder); |
149 |
| - if (this.$fs.exists(folderToCopy)) { |
150 |
| - this.$fs.copyFile(folderToCopy, tmpDir); |
151 |
| - } |
152 |
| - } |
| 28 | + return args.length < 2 && this.$projectData.projectDir !== ""; |
153 | 29 | }
|
154 | 30 | }
|
155 | 31 |
|
|
0 commit comments