Skip to content

Commit c859f5e

Browse files
committed
Add spinner when checking for NativeScript components versions
Currently when doctor command is executed, NativeScript components versions information is displayed as table. With this PR NativeScript components versions will be displayed with terminal spinners. In case when component is up to date with latest available version, success spinner will be displayed. In case when for component update is available, warn spinner will be displayed. In case when component is not installed, fail spinner will be displayed. This PR changes also the behaviour of `tns info` command. When `tns info` command is executed, NativeScript components versions information also will be displayed with terminal spinners.
1 parent 6dd03cb commit c859f5e

File tree

5 files changed

+60
-70
lines changed

5 files changed

+60
-70
lines changed

lib/common

lib/declarations.d.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,23 +698,17 @@ interface IVersionsService {
698698
*/
699699
getRuntimesVersions(): Promise<IVersionInformation[]>;
700700

701-
/**
702-
* Checks version information about the nativescript components and prints available updates if any.
703-
*/
704-
checkComponentsForUpdate(): Promise<void>;
705-
706701
/**
707702
* Gets versions information about all nativescript components.
708703
* @return {Promise<IVersionInformation[]>} The version information.
709704
*/
710705
getAllComponentsVersions(): Promise<IVersionInformation[]>;
711706

712707
/**
713-
* Creates table with versions information.
714-
* @param {IVersionInformation[]} The versions information to push in the table.
715-
* @return {any} The created table.
708+
* Checks version information about the nativescript components and prints versions information.
709+
* @return {Promise<void>}
716710
*/
717-
createTableWithVersionsInformation(versionsInformation: IVersionInformation[]): any;
711+
printVersionsInformation(): Promise<void>;
718712
}
719713

720714
/**

lib/services/doctor-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DoctorService implements IDoctorService {
4747
}
4848

4949
try {
50-
await this.$versionsService.checkComponentsForUpdate();
50+
await this.$versionsService.printVersionsInformation();
5151
} catch (err) {
5252
this.$logger.error("Cannot get the latest versions information from npm. Please try again later.");
5353
}

lib/services/info-service.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
export class InfoService implements IInfoService {
2-
constructor(private $versionsService: IVersionsService,
3-
private $logger: ILogger) { }
2+
constructor(private $versionsService: IVersionsService) { }
43

5-
public async printComponentsInfo(): Promise<void> {
6-
const allComponentsInfo = await this.$versionsService.getAllComponentsVersions();
7-
8-
const table: any = this.$versionsService.createTableWithVersionsInformation(allComponentsInfo);
9-
10-
this.$logger.out("All NativeScript components versions information");
11-
this.$logger.out(table.toString());
4+
public printComponentsInfo(): Promise<void> {
5+
return this.$versionsService.printVersionsInformation();
126
}
137
}
148

lib/services/versions-service.ts

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
import { EOL } from "os";
21
import * as constants from "../constants";
2+
import * as helpers from "../common/helpers";
33
import * as semver from "semver";
44
import * as path from "path";
5-
import { createTable } from "../common/helpers";
5+
6+
export enum VersionInformationType {
7+
UpToDate = "UpToDate",
8+
UpdateAvailable = "UpdateAvailable",
9+
NotInstalled = "NotInstalled"
10+
}
611

712
class VersionsService implements IVersionsService {
8-
private static UP_TO_DATE_MESSAGE = "Up to date".green.toString();
9-
private static UPDATE_AVAILABLE_MESSAGE = "Update available".yellow.toString();
10-
private static NOT_INSTALLED_MESSAGE = "Not installed".grey.toString();
13+
private static UP_TO_DATE_MESSAGE = "up to date";
14+
private static UPDATE_AVAILABLE_MESSAGE = "Update available";
15+
private static NOT_INSTALLED_MESSAGE = "not installed";
1116

1217
private projectData: IProjectData;
1318

1419
constructor(private $fs: IFileSystem,
1520
private $npmInstallationManager: INpmInstallationManager,
1621
private $injector: IInjector,
22+
private $logger: ILogger,
1723
private $staticConfig: Config.IStaticConfig,
1824
private $pluginsService: IPluginsService,
19-
private $logger: ILogger) {
25+
private $terminalSpinnerService: ITerminalSpinnerService) {
2026
this.projectData = this.getProjectData();
2127
}
2228

@@ -86,30 +92,6 @@ class VersionsService implements IVersionsService {
8692
return runtimesVersions;
8793
}
8894

89-
public async checkComponentsForUpdate(): Promise<void> {
90-
const allComponents: IVersionInformation[] = await this.getAllComponentsVersions();
91-
const componentsForUpdate: IVersionInformation[] = [];
92-
93-
_.forEach(allComponents, (component: IVersionInformation) => {
94-
if (component.currentVersion && this.hasUpdate(component)) {
95-
componentsForUpdate.push(component);
96-
}
97-
});
98-
99-
this.printVersionsInformation(componentsForUpdate, allComponents);
100-
}
101-
102-
private printVersionsInformation(versionsInformation: IVersionInformation[], allComponents: IVersionInformation[]): void {
103-
if (versionsInformation && versionsInformation.length) {
104-
const table: any = this.createTableWithVersionsInformation(versionsInformation);
105-
106-
this.$logger.warn("Updates available");
107-
this.$logger.out(table.toString() + EOL);
108-
} else {
109-
this.$logger.out(`Your components are up-to-date: ${EOL}${allComponents.map(component => component.componentName)}${EOL}`);
110-
}
111-
}
112-
11395
public async getAllComponentsVersions(): Promise<IVersionInformation[]> {
11496
let allComponents: IVersionInformation[] = [];
11597

@@ -127,30 +109,50 @@ class VersionsService implements IVersionsService {
127109

128110
allComponents = allComponents.concat(runtimesVersions);
129111

130-
return allComponents;
112+
return allComponents
113+
.map(componentInformation => {
114+
if (componentInformation.currentVersion) {
115+
if (this.hasUpdate(componentInformation)) {
116+
componentInformation.type = VersionInformationType.UpdateAvailable;
117+
componentInformation.message = `${VersionsService.UPDATE_AVAILABLE_MESSAGE} for component ${componentInformation.componentName}. Your current version is ${componentInformation.currentVersion} and the latest available version is ${componentInformation.latestVersion}.`;
118+
} else {
119+
componentInformation.type = VersionInformationType.UpToDate;
120+
componentInformation.message = `Component ${componentInformation.componentName} has ${componentInformation.currentVersion} version and is ${VersionsService.UP_TO_DATE_MESSAGE}.`;
121+
}
122+
} else {
123+
componentInformation.type = VersionInformationType.NotInstalled;
124+
componentInformation.message = `Component ${componentInformation.componentName} is ${VersionsService.NOT_INSTALLED_MESSAGE}.`;
125+
}
126+
127+
return componentInformation;
128+
});
131129
}
132130

133-
public createTableWithVersionsInformation(versionsInformation: IVersionInformation[]): any {
134-
const headers = ["Component", "Current version", "Latest version", "Information"];
135-
const data: string[][] = [];
136-
137-
_.forEach(versionsInformation, (componentInformation: IVersionInformation) => {
138-
const row: string[] = [
139-
componentInformation.componentName,
140-
componentInformation.currentVersion,
141-
componentInformation.latestVersion
142-
];
143-
144-
if (componentInformation.currentVersion) {
145-
semver.lt(componentInformation.currentVersion, componentInformation.latestVersion) ? row.push(VersionsService.UPDATE_AVAILABLE_MESSAGE) : row.push(VersionsService.UP_TO_DATE_MESSAGE);
146-
} else {
147-
row.push(VersionsService.NOT_INSTALLED_MESSAGE);
148-
}
131+
public async printVersionsInformation(): Promise<void> {
132+
const versionsInformation = await this.$terminalSpinnerService.execute<IVersionInformation[]>({
133+
text: `Getting NativeScript components versions information...`
134+
}, () => this.getAllComponentsVersions());
149135

150-
data.push(row);
151-
});
136+
if (!helpers.isInteractive()) {
137+
versionsInformation.map(componentInformation => this.$logger.out(componentInformation.message));
138+
}
152139

153-
return createTable(headers, data);
140+
_.forEach(versionsInformation, componentInformation => {
141+
const spinner = this.$terminalSpinnerService.createSpinner();
142+
spinner.text = componentInformation.message;
143+
144+
switch (componentInformation.type) {
145+
case VersionInformationType.UpToDate:
146+
spinner.succeed();
147+
break;
148+
case VersionInformationType.UpdateAvailable:
149+
spinner.warn();
150+
break;
151+
case VersionInformationType.NotInstalled:
152+
spinner.fail();
153+
break;
154+
}
155+
});
154156
}
155157

156158
private getProjectData(): IProjectData {

0 commit comments

Comments
 (0)