11import * as path from "path" ;
2- import * as shelljs from "shelljs " ;
2+ import * as constants from "../constants " ;
33
44export class UpdateCommand implements ICommand {
55 public allowedParameters : ICommandParameter [ ] = [ ] ;
@@ -15,49 +15,41 @@ export class UpdateCommand implements ICommand {
1515 this . $projectData . initializeProjectData ( ) ;
1616 }
1717
18+ static readonly folders : string [ ] = [
19+ constants . LIB_DIR_NAME ,
20+ constants . HOOKS_DIR_NAME ,
21+ constants . PLATFORMS_DIR_NAME ,
22+ constants . NODE_MODULES_FOLDER_NAME
23+ ] ;
24+ static readonly tempFolder : string = ".tmp_backup" ;
25+ static readonly updateFailMessage : string = "Could not update the project!" ;
26+ static readonly backupFailMessage : string = "Could not backup project folders!" ;
27+
1828 public async execute ( args : string [ ] ) : Promise < void > {
19- const folders = [ "lib" , "hooks" , "platforms" , "node_modules" ] ;
20- const tmpDir = path . join ( this . $projectData . projectDir , ".tmp_backup" ) ;
29+ const tmpDir = path . join ( this . $projectData . projectDir , UpdateCommand . tempFolder ) ;
2130
2231 try {
23- shelljs . rm ( "-fr" , tmpDir ) ;
24- shelljs . mkdir ( tmpDir ) ;
25- shelljs . cp ( path . join ( this . $projectData . projectDir , "package.json" ) , tmpDir ) ;
26- for ( const folder of folders ) {
27- const folderToCopy = path . join ( this . $projectData . projectDir , folder ) ;
28- if ( this . $fs . exists ( folderToCopy ) ) {
29- shelljs . cp ( "-rf" , folderToCopy , tmpDir ) ;
30- }
31- }
32+ this . backup ( tmpDir ) ;
3233 } catch ( error ) {
33- this . $logger . error ( "Could not backup project folders!" ) ;
34+ this . $logger . error ( UpdateCommand . backupFailMessage ) ;
35+ this . $fs . deleteDirectory ( tmpDir ) ;
3436 return ;
3537 }
3638
3739 try {
38- await this . executeCore ( args , folders ) ;
40+ await this . executeCore ( args ) ;
3941 } catch ( error ) {
40- shelljs . cp ( "-f" , path . join ( tmpDir , "package.json" ) , this . $projectData . projectDir ) ;
41- for ( const folder of folders ) {
42- shelljs . rm ( "-rf" , path . join ( this . $projectData . projectDir , folder ) ) ;
43-
44- const folderToCopy = path . join ( tmpDir , folder ) ;
45-
46- if ( this . $fs . exists ( folderToCopy ) ) {
47- shelljs . cp ( "-fr" , folderToCopy , this . $projectData . projectDir ) ;
48- }
49- }
50-
51- this . $logger . error ( "Could not update the project!" ) ;
42+ this . restoreBackup ( tmpDir ) ;
43+ this . $logger . error ( UpdateCommand . updateFailMessage ) ;
5244 } finally {
53- shelljs . rm ( "-fr" , tmpDir ) ;
45+ this . $fs . deleteDirectory ( tmpDir ) ;
5446 }
5547 }
5648
5749 public async canExecute ( args : string [ ] ) : Promise < boolean > {
58- for ( const arg of args ) {
59- const platform = arg . split ( "@" ) [ 0 ] ;
60- this . $platformService . validatePlatformInstalled ( platform , this . $projectData ) ;
50+ const platforms = this . getPlatforms ( ) ;
51+
52+ for ( const platform of platforms . packagePlatforms ) {
6153 const platformData = this . $platformsData . getPlatformData ( platform , this . $projectData ) ;
6254 const platformProjectService = platformData . platformProjectService ;
6355 await platformProjectService . validate ( this . $projectData ) ;
@@ -66,42 +58,79 @@ export class UpdateCommand implements ICommand {
6658 return args . length < 2 && this . $projectData . projectDir !== "" ;
6759 }
6860
69- private async executeCore ( args : string [ ] , folders : string [ ] ) : Promise < void > {
70- let platforms = this . $platformService . getInstalledPlatforms ( this . $projectData ) ;
71- const availablePlatforms = this . $platformService . getAvailablePlatforms ( this . $projectData ) ;
72- const packagePlatforms : string [ ] = [ ] ;
61+ private async executeCore ( args : string [ ] ) : Promise < void > {
62+ const platforms = this . getPlatforms ( ) ;
7363
74- for ( const platform of availablePlatforms ) {
64+ for ( const platform of _ . xor ( platforms . installed , platforms . packagePlatforms ) ) {
7565 const platformData = this . $platformsData . getPlatformData ( platform , this . $projectData ) ;
76- const platformVersion = this . $projectDataService . getNSValue ( this . $projectData . projectDir , platformData . frameworkPackageName ) ;
77- if ( platformVersion ) {
78- packagePlatforms . push ( platform ) ;
79- this . $projectDataService . removeNSProperty ( this . $projectData . projectDir , platformData . frameworkPackageName ) ;
80- }
66+ this . $projectDataService . removeNSProperty ( this . $projectData . projectDir , platformData . frameworkPackageName ) ;
8167 }
8268
83- await this . $platformService . removePlatforms ( platforms , this . $projectData ) ;
69+ await this . $platformService . removePlatforms ( platforms . installed , this . $projectData ) ;
8470 await this . $pluginsService . remove ( "tns-core-modules" , this . $projectData ) ;
8571 await this . $pluginsService . remove ( "tns-core-modules-widgets" , this . $projectData ) ;
8672
87- for ( const folder of folders ) {
88- shelljs . rm ( "-fr" , folder ) ;
73+ for ( const folder of UpdateCommand . folders ) {
74+ this . $fs . deleteDirectory ( path . join ( this . $projectData . projectDir , folder ) ) ;
8975 }
9076
91- platforms = platforms . concat ( packagePlatforms ) ;
9277 if ( args . length === 1 ) {
93- for ( const platform of platforms ) {
78+ for ( const platform of platforms . packagePlatforms ) {
9479 await this . $platformService . addPlatforms ( [ platform + "@" + args [ 0 ] ] , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
9580 }
9681
9782 await this . $pluginsService . add ( "tns-core-modules@" + args [ 0 ] , this . $projectData ) ;
9883 } else {
99- await this . $platformService . addPlatforms ( platforms , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
84+ await this . $platformService . addPlatforms ( platforms . packagePlatforms , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
10085 await this . $pluginsService . add ( "tns-core-modules" , this . $projectData ) ;
10186 }
10287
10388 await this . $pluginsService . ensureAllDependenciesAreInstalled ( this . $projectData ) ;
10489 }
90+
91+ private getPlatforms ( ) : { installed : string [ ] , packagePlatforms : string [ ] } {
92+ const installedPlatforms = this . $platformService . getInstalledPlatforms ( this . $projectData ) ;
93+ const availablePlatforms = this . $platformService . getAvailablePlatforms ( this . $projectData ) ;
94+ const packagePlatforms : string [ ] = [ ] ;
95+
96+ for ( const platform of availablePlatforms ) {
97+ const platformData = this . $platformsData . getPlatformData ( platform , this . $projectData ) ;
98+ const platformVersion = this . $projectDataService . getNSValue ( this . $projectData . projectDir , platformData . frameworkPackageName ) ;
99+ if ( platformVersion ) {
100+ packagePlatforms . push ( platform ) ;
101+ }
102+ }
103+
104+ return {
105+ installed : installedPlatforms ,
106+ packagePlatforms : installedPlatforms . concat ( packagePlatforms )
107+ } ;
108+ }
109+
110+ private restoreBackup ( tmpDir : string ) : void {
111+ this . $fs . copyFile ( path . join ( tmpDir , constants . PACKAGE_JSON_FILE_NAME ) , this . $projectData . projectDir ) ;
112+ for ( const folder of UpdateCommand . folders ) {
113+ this . $fs . deleteDirectory ( path . join ( this . $projectData . projectDir , folder ) ) ;
114+
115+ const folderToCopy = path . join ( tmpDir , folder ) ;
116+
117+ if ( this . $fs . exists ( folderToCopy ) ) {
118+ this . $fs . copyFile ( folderToCopy , this . $projectData . projectDir ) ;
119+ }
120+ }
121+ }
122+
123+ private backup ( tmpDir : string ) : void {
124+ this . $fs . deleteDirectory ( tmpDir ) ;
125+ this . $fs . createDirectory ( tmpDir ) ;
126+ this . $fs . copyFile ( path . join ( this . $projectData . projectDir , constants . PACKAGE_JSON_FILE_NAME ) , tmpDir ) ;
127+ for ( const folder of UpdateCommand . folders ) {
128+ const folderToCopy = path . join ( this . $projectData . projectDir , folder ) ;
129+ if ( this . $fs . exists ( folderToCopy ) ) {
130+ this . $fs . copyFile ( folderToCopy , tmpDir ) ;
131+ }
132+ }
133+ }
105134}
106135
107136$injector . registerCommand ( "update" , UpdateCommand ) ;
0 commit comments