-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathversions-service.ts
264 lines (231 loc) · 8.57 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import * as constants from "../constants";
import * as helpers from "../common/helpers";
import * as semver from "semver";
import * as path from "path";
import { IVersionsService, IPackageInstallationManager } from "../declarations";
import { IProjectData, IProjectDataService } from "../definitions/project";
import { IPluginsService, IBasePluginData } from "../definitions/plugins";
import { IFileSystem, IVersionInformation } from "../common/declarations";
import { IInjector } from "../common/definitions/yok";
import * as _ from "lodash";
import { injector } from "../common/yok";
import { PlatformTypes } from "../constants";
export enum VersionInformationType {
UpToDate = "UpToDate",
UpdateAvailable = "UpdateAvailable",
NotInstalled = "NotInstalled",
}
class VersionsService implements IVersionsService {
private static UP_TO_DATE_MESSAGE = "up to date";
private static UPDATE_AVAILABLE_MESSAGE = "Update available";
private static NOT_INSTALLED_MESSAGE = "not installed";
private projectData: IProjectData;
constructor(
private $fs: IFileSystem,
private $packageInstallationManager: IPackageInstallationManager,
private $injector: IInjector,
private $logger: ILogger,
private $staticConfig: Config.IStaticConfig,
private $pluginsService: IPluginsService,
private $projectDataService: IProjectDataService,
private $terminalSpinnerService: ITerminalSpinnerService
) {
this.projectData = this.getProjectData();
}
public async getNativescriptCliVersion(): Promise<IVersionInformation> {
const currentCliVersion = this.$staticConfig.version;
const latestCliVersion = await this.$packageInstallationManager.getLatestVersion(
constants.NATIVESCRIPT_KEY_NAME
);
return {
componentName: constants.NATIVESCRIPT_KEY_NAME,
currentVersion: currentCliVersion,
latestVersion: latestCliVersion,
};
}
public async getTnsCoreModulesVersion(): Promise<IVersionInformation[]> {
const latestTnsCoreModulesVersion = await this.$packageInstallationManager.getLatestVersion(
constants.TNS_CORE_MODULES_NAME
);
const nativescriptCoreModulesInfo: IVersionInformation = {
componentName: constants.TNS_CORE_MODULES_NAME,
latestVersion: latestTnsCoreModulesVersion,
};
const versionInformations: IVersionInformation[] = [];
if (this.projectData) {
const nodeModulesPath = path.join(
this.projectData.projectDir,
constants.NODE_MODULES_FOLDER_NAME
);
const scopedPackagePath = path.join(
nodeModulesPath,
constants.SCOPED_TNS_CORE_MODULES
);
const tnsCoreModulesPath = path.join(
nodeModulesPath,
constants.TNS_CORE_MODULES_NAME
);
const dependsOnNonScopedPackage = !!this.projectData.dependencies[
constants.TNS_CORE_MODULES_NAME
];
const dependsOnScopedPackage = !!this.projectData.dependencies[
constants.SCOPED_TNS_CORE_MODULES
];
// ensure the dependencies are installed, so we can get their actual versions from node_modules
if (
!this.$fs.exists(nodeModulesPath) ||
(dependsOnNonScopedPackage && !this.$fs.exists(tnsCoreModulesPath)) ||
(dependsOnScopedPackage && !this.$fs.exists(scopedPackagePath))
) {
await this.$pluginsService.ensureAllDependenciesAreInstalled(
this.projectData
);
}
if (dependsOnNonScopedPackage && this.$fs.exists(tnsCoreModulesPath)) {
const currentTnsCoreModulesVersion = this.$fs.readJson(
path.join(tnsCoreModulesPath, constants.PACKAGE_JSON_FILE_NAME)
).version;
nativescriptCoreModulesInfo.currentVersion = currentTnsCoreModulesVersion;
versionInformations.push(nativescriptCoreModulesInfo);
}
if (dependsOnScopedPackage && this.$fs.exists(scopedPackagePath)) {
const scopedModulesInformation: IVersionInformation = {
componentName: constants.SCOPED_TNS_CORE_MODULES,
latestVersion: await this.$packageInstallationManager.getLatestVersion(
constants.SCOPED_TNS_CORE_MODULES
),
};
const currentScopedPackageVersion = this.$fs.readJson(
path.join(scopedPackagePath, constants.PACKAGE_JSON_FILE_NAME)
).version;
scopedModulesInformation.currentVersion = currentScopedPackageVersion;
versionInformations.push(scopedModulesInformation);
}
} else {
versionInformations.push(nativescriptCoreModulesInfo);
}
return versionInformations;
}
public async getRuntimesVersions(
platform?: string
): Promise<IVersionInformation[]> {
const iosRuntime = this.$projectDataService.getRuntimePackage(
this.projectData.projectDir,
constants.PlatformTypes.ios
);
const androidRuntime = this.$projectDataService.getRuntimePackage(
this.projectData.projectDir,
constants.PlatformTypes.android
);
let runtimes: IBasePluginData[] = [];
if (!platform) {
runtimes = [iosRuntime, androidRuntime];
} else if (platform === PlatformTypes.ios) {
runtimes.push(iosRuntime);
} else if (platform === PlatformTypes.android) {
runtimes.push(androidRuntime);
}
const runtimesVersions: IVersionInformation[] = await Promise.all(
runtimes.map(async (runtime: IBasePluginData) => {
const latestVersion = await this.$packageInstallationManager.getLatestVersion(
runtime.name
);
const runtimeInformation: IVersionInformation = {
componentName: runtime.name,
currentVersion: runtime.version,
latestVersion,
};
return runtimeInformation;
})
);
return runtimesVersions;
}
public async getAllComponentsVersions(
platform?: string
): Promise<IVersionInformation[]> {
try {
let allComponents: IVersionInformation[] = [];
const nativescriptCliInformation: IVersionInformation = await this.getNativescriptCliVersion();
if (nativescriptCliInformation) {
allComponents.push(nativescriptCliInformation);
}
if (this.projectData) {
const nativescriptCoreModulesInformation: IVersionInformation[] = await this.getTnsCoreModulesVersion();
if (nativescriptCoreModulesInformation) {
allComponents.push(...nativescriptCoreModulesInformation);
}
const runtimesVersions: IVersionInformation[] = await this.getRuntimesVersions(
platform
);
allComponents = allComponents.concat(runtimesVersions);
}
return allComponents.map((componentInformation) => {
if (componentInformation.currentVersion) {
if (this.hasUpdate(componentInformation)) {
componentInformation.type = VersionInformationType.UpdateAvailable;
componentInformation.message = `${VersionsService.UPDATE_AVAILABLE_MESSAGE} for component ${componentInformation.componentName}. Your current version is ${componentInformation.currentVersion} and the latest available version is ${componentInformation.latestVersion}.`;
} else {
componentInformation.type = VersionInformationType.UpToDate;
componentInformation.message = `Component ${componentInformation.componentName} has ${componentInformation.currentVersion} version and is ${VersionsService.UP_TO_DATE_MESSAGE}.`;
}
} else {
componentInformation.type = VersionInformationType.NotInstalled;
componentInformation.message = `Component ${componentInformation.componentName} is ${VersionsService.NOT_INSTALLED_MESSAGE}.`;
}
return componentInformation;
});
} catch (error) {
this.$logger.trace(
"Error while trying to get component information. Error is: ",
error
);
return [];
}
}
public async printVersionsInformation(platform?: string): Promise<void> {
const versionsInformation = await this.$terminalSpinnerService.execute<
IVersionInformation[]
>(
{
text: `Getting NativeScript components versions information...`,
},
() => this.getAllComponentsVersions(platform)
);
if (!helpers.isInteractive()) {
versionsInformation.map((componentInformation) =>
this.$logger.info(componentInformation.message)
);
}
_.forEach(versionsInformation, (componentInformation) => {
const spinner = this.$terminalSpinnerService.createSpinner();
spinner.text = componentInformation.message;
switch (componentInformation.type) {
case VersionInformationType.UpToDate:
spinner.succeed();
break;
case VersionInformationType.UpdateAvailable:
spinner.warn();
break;
case VersionInformationType.NotInstalled:
spinner.fail();
break;
}
});
}
private getProjectData(): IProjectData {
try {
const projectData: IProjectData = this.$injector.resolve("projectData");
projectData.initializeProjectData();
return projectData;
} catch (error) {
return null;
}
}
private hasUpdate(component: IVersionInformation): boolean {
return !semver.satisfies(
component.latestVersion,
semver.validRange(component.currentVersion)
);
}
}
injector.register("versionsService", VersionsService);