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