-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathversions-service.ts
171 lines (136 loc) · 5.94 KB
/
versions-service.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { EOL } from "os";
import * as constants from "../constants";
import * as semver from "semver";
import * as path from "path";
import { createTable } from "../common/helpers";
class VersionsService implements IVersionsService {
private static UP_TO_DATE_MESSAGE = "Up to date".green.toString();
private static UPDATE_AVAILABLE_MESSAGE = "Update available".yellow.toString();
private static NOT_INSTALLED_MESSAGE = "Not installed".grey.toString();
private projectData: IProjectData;
constructor(private $fs: IFileSystem,
private $npmInstallationManager: INpmInstallationManager,
private $injector: IInjector,
private $staticConfig: Config.IStaticConfig,
private $pluginsService: IPluginsService,
private $logger: ILogger) {
this.projectData = this.getProjectData();
}
public async getNativescriptCliVersion(): Promise<IVersionInformation> {
let currentCliVersion = this.$staticConfig.version;
let latestCliVersion = await this.$npmInstallationManager.getLatestVersion(constants.NATIVESCRIPT_KEY_NAME);
return {
componentName: constants.NATIVESCRIPT_KEY_NAME,
currentVersion: currentCliVersion,
latestVersion: latestCliVersion
};
}
public async getTnsCoreModulesVersion(): Promise<IVersionInformation> {
let latestTnsCoreModulesVersion = await this.$npmInstallationManager.getLatestVersion(constants.TNS_CORE_MODULES_NAME);
let nativescriptCoreModulesInfo: IVersionInformation = {
componentName: constants.TNS_CORE_MODULES_NAME,
latestVersion: latestTnsCoreModulesVersion
};
if (this.projectData) {
let nodeModulesPath = path.join(this.projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME);
let tnsCoreModulesPath = path.join(nodeModulesPath, constants.TNS_CORE_MODULES_NAME);
if (!this.$fs.exists(nodeModulesPath) ||
!this.$fs.exists(tnsCoreModulesPath)) {
await this.$pluginsService.ensureAllDependenciesAreInstalled(this.projectData);
}
let currentTnsCoreModulesVersion = this.$fs.readJson(path.join(tnsCoreModulesPath, constants.PACKAGE_JSON_FILE_NAME)).version;
nativescriptCoreModulesInfo.currentVersion = currentTnsCoreModulesVersion;
}
return nativescriptCoreModulesInfo;
}
public async getRuntimesVersions(): Promise<IVersionInformation[]> {
let runtimes: string[] = [
constants.TNS_ANDROID_RUNTIME_NAME,
constants.TNS_IOS_RUNTIME_NAME
];
let projectConfig: any;
if (this.projectData) {
projectConfig = this.$fs.readJson(this.projectData.projectFilePath);
}
let runtimesVersions: IVersionInformation[] = await Promise.all(runtimes.map(async (runtime: string) => {
let latestRuntimeVersion = await this.$npmInstallationManager.getLatestVersion(runtime);
let runtimeInformation: IVersionInformation = {
componentName: runtime,
latestVersion: latestRuntimeVersion
};
if (projectConfig) {
let projectRuntimeInformation = projectConfig.nativescript && projectConfig.nativescript[runtime];
if (projectRuntimeInformation) {
let runtimeVersionInProject = projectRuntimeInformation.version;
runtimeInformation.currentVersion = runtimeVersionInProject;
}
}
return runtimeInformation;
}));
return runtimesVersions;
}
public async checkComponentsForUpdate(): Promise<void> {
let allComponents: IVersionInformation[] = await this.getAllComponentsVersions();
let componentsForUpdate: IVersionInformation[] = [];
_.forEach(allComponents, (component: IVersionInformation) => {
if (component.currentVersion && this.hasUpdate(component)) {
componentsForUpdate.push(component);
}
});
this.printVersionsInformation(componentsForUpdate, allComponents);
}
private printVersionsInformation(versionsInformation: IVersionInformation[], allComponents: IVersionInformation[]): void {
if (versionsInformation && versionsInformation.length) {
let table: any = this.createTableWithVersionsInformation(versionsInformation);
this.$logger.warn("Updates available");
this.$logger.out(table.toString() + EOL);
} else {
this.$logger.out(`Your components are up-to-date: ${EOL}${allComponents.map(component => component.componentName)}${EOL}`);
}
}
public async getAllComponentsVersions(): Promise<IVersionInformation[]> {
let allComponents: IVersionInformation[] = [];
let nativescriptCliInformation: IVersionInformation = await this.getNativescriptCliVersion();
if (nativescriptCliInformation) {
allComponents.push(nativescriptCliInformation);
}
let nativescriptCoreModulesInformation: IVersionInformation = await this.getTnsCoreModulesVersion();
if (nativescriptCoreModulesInformation) {
allComponents.push(nativescriptCoreModulesInformation);
}
let runtimesVersions: IVersionInformation[] = await this.getRuntimesVersions();
allComponents = allComponents.concat(runtimesVersions);
return allComponents;
}
public createTableWithVersionsInformation(versionsInformation: IVersionInformation[]): any {
let headers = ["Component", "Current version", "Latest version", "Information"];
let data: string[][] = [];
_.forEach(versionsInformation, (componentInformation: IVersionInformation) => {
let row: string[] = [
componentInformation.componentName,
componentInformation.currentVersion,
componentInformation.latestVersion
];
if (componentInformation.currentVersion) {
semver.lt(componentInformation.currentVersion, componentInformation.latestVersion) ? row.push(VersionsService.UPDATE_AVAILABLE_MESSAGE) : row.push(VersionsService.UP_TO_DATE_MESSAGE);
} else {
row.push(VersionsService.NOT_INSTALLED_MESSAGE);
}
data.push(row);
});
return createTable(headers, data);
}
private getProjectData(): IProjectData {
try {
let projectData: IProjectData = this.$injector.resolve("projectData");
projectData.initializeProjectData();
return projectData;
} catch (error) {
return null;
}
}
private hasUpdate(component: IVersionInformation): boolean {
return semver.lt(component.currentVersion, component.latestVersion);
}
}
$injector.register("versionsService", VersionsService);