1
- import { EOL } from "os" ;
2
1
import * as constants from "../constants" ;
2
+ import * as helpers from "../common/helpers" ;
3
3
import * as semver from "semver" ;
4
4
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
+ }
6
11
7
12
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" ;
11
16
12
17
private projectData : IProjectData ;
13
18
14
19
constructor ( private $fs : IFileSystem ,
15
20
private $npmInstallationManager : INpmInstallationManager ,
16
21
private $injector : IInjector ,
22
+ private $logger : ILogger ,
17
23
private $staticConfig : Config . IStaticConfig ,
18
24
private $pluginsService : IPluginsService ,
19
- private $logger : ILogger ) {
25
+ private $terminalSpinnerService : ITerminalSpinnerService ) {
20
26
this . projectData = this . getProjectData ( ) ;
21
27
}
22
28
@@ -86,30 +92,6 @@ class VersionsService implements IVersionsService {
86
92
return runtimesVersions ;
87
93
}
88
94
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
-
113
95
public async getAllComponentsVersions ( ) : Promise < IVersionInformation [ ] > {
114
96
let allComponents : IVersionInformation [ ] = [ ] ;
115
97
@@ -127,30 +109,50 @@ class VersionsService implements IVersionsService {
127
109
128
110
allComponents = allComponents . concat ( runtimesVersions ) ;
129
111
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
+ } ) ;
131
129
}
132
130
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 ( ) ) ;
149
135
150
- data . push ( row ) ;
151
- } ) ;
136
+ if ( ! helpers . isInteractive ( ) ) {
137
+ versionsInformation . map ( componentInformation => this . $logger . out ( componentInformation . message ) ) ;
138
+ }
152
139
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
+ } ) ;
154
156
}
155
157
156
158
private getProjectData ( ) : IProjectData {
0 commit comments