1- import { EOL } from "os" ;
21import * as constants from "../constants" ;
2+ import * as helpers from "../common/helpers" ;
33import * as semver from "semver" ;
44import * 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
712class 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