-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathupdate.ts
129 lines (105 loc) · 4.7 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
import * as path from "path";
import * as constants from "../constants";
export class UpdateCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
constructor(private $options: IOptions,
private $projectData: IProjectData,
private $platformService: IPlatformService,
private $platformsData: IPlatformsData,
private $pluginsService: IPluginsService,
private $projectDataService: IProjectDataService,
private $fs: IFileSystem,
private $logger: ILogger) {
this.$projectData.initializeProjectData();
}
static readonly folders: string[] = ["lib", "hooks", "platforms", "node_modules"];
static readonly tempFolder: string = ".tmp_backup";
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("Could not backup project folders!");
this.$fs.deleteDirectory(tmpDir);
return;
}
try {
await this.executeCore(args);
} catch (error) {
this.restoreBackup(tmpDir);
this.$logger.error("Could not update the project!");
} finally {
this.$fs.deleteDirectory(tmpDir);
}
}
public async canExecute(args: string[]): Promise<boolean> {
const platforms = this.getPlatforms();
for (const platform of platforms.packagePlatforms) {
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
const platformProjectService = platformData.platformProjectService;
await platformProjectService.validate(this.$projectData);
}
return args.length < 2 && this.$projectData.projectDir !== "";
}
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("tns-core-modules", this.$projectData);
await this.$pluginsService.remove("tns-core-modules-widgets", 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("tns-core-modules@" + 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("tns-core-modules", 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);