-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathversions-service.ts
199 lines (164 loc) · 7.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
import * as constants from "../constants";
import * as helpers from "../common/helpers";
import * as semver from "semver";
import * as path from "path";
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 $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(): Promise<IVersionInformation[]> {
const 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);
}
const runtimesVersions: IVersionInformation[] = await Promise.all(runtimes.map(async (runtime: string) => {
const latestRuntimeVersion = await this.$packageInstallationManager.getLatestVersion(runtime);
const runtimeInformation: IVersionInformation = {
componentName: runtime,
latestVersion: latestRuntimeVersion
};
if (projectConfig) {
const projectRuntimeInformation = projectConfig.nativescript && projectConfig.nativescript[runtime];
if (projectRuntimeInformation) {
const runtimeVersionInProject = projectRuntimeInformation.version;
runtimeInformation.currentVersion = runtimeVersionInProject;
}
}
return runtimeInformation;
}));
return runtimesVersions;
}
public async getAllComponentsVersions(): Promise<IVersionInformation[]> {
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();
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;
});
}
public async printVersionsInformation(): Promise<void> {
const versionsInformation = await this.$terminalSpinnerService.execute<IVersionInformation[]>({
text: `Getting NativeScript components versions information...`
}, () => this.getAllComponentsVersions());
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.lt(component.currentVersion, component.latestVersion);
}
}
$injector.register("versionsService", VersionsService);