1
1
import * as path from "path" ;
2
- import * as shelljs from "shelljs " ;
2
+ import * as constants from "../constants " ;
3
3
4
4
export class UpdateCommand implements ICommand {
5
5
public allowedParameters : ICommandParameter [ ] = [ ] ;
@@ -15,49 +15,41 @@ export class UpdateCommand implements ICommand {
15
15
this . $projectData . initializeProjectData ( ) ;
16
16
}
17
17
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
+
18
28
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 ) ;
21
30
22
31
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 ) ;
32
33
} catch ( error ) {
33
- this . $logger . error ( "Could not backup project folders!" ) ;
34
+ this . $logger . error ( UpdateCommand . backupFailMessage ) ;
35
+ this . $fs . deleteDirectory ( tmpDir ) ;
34
36
return ;
35
37
}
36
38
37
39
try {
38
- await this . executeCore ( args , folders ) ;
40
+ await this . executeCore ( args ) ;
39
41
} 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 ) ;
52
44
} finally {
53
- shelljs . rm ( "-fr" , tmpDir ) ;
45
+ this . $fs . deleteDirectory ( tmpDir ) ;
54
46
}
55
47
}
56
48
57
49
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 ) {
61
53
const platformData = this . $platformsData . getPlatformData ( platform , this . $projectData ) ;
62
54
const platformProjectService = platformData . platformProjectService ;
63
55
await platformProjectService . validate ( this . $projectData ) ;
@@ -66,42 +58,79 @@ export class UpdateCommand implements ICommand {
66
58
return args . length < 2 && this . $projectData . projectDir !== "" ;
67
59
}
68
60
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 ( ) ;
73
63
74
- for ( const platform of availablePlatforms ) {
64
+ for ( const platform of _ . xor ( platforms . installed , platforms . packagePlatforms ) ) {
75
65
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 ) ;
81
67
}
82
68
83
- await this . $platformService . removePlatforms ( platforms , this . $projectData ) ;
69
+ await this . $platformService . removePlatforms ( platforms . installed , this . $projectData ) ;
84
70
await this . $pluginsService . remove ( "tns-core-modules" , this . $projectData ) ;
85
71
await this . $pluginsService . remove ( "tns-core-modules-widgets" , this . $projectData ) ;
86
72
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 ) ) ;
89
75
}
90
76
91
- platforms = platforms . concat ( packagePlatforms ) ;
92
77
if ( args . length === 1 ) {
93
- for ( const platform of platforms ) {
78
+ for ( const platform of platforms . packagePlatforms ) {
94
79
await this . $platformService . addPlatforms ( [ platform + "@" + args [ 0 ] ] , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
95
80
}
96
81
97
82
await this . $pluginsService . add ( "tns-core-modules@" + args [ 0 ] , this . $projectData ) ;
98
83
} 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 ) ;
100
85
await this . $pluginsService . add ( "tns-core-modules" , this . $projectData ) ;
101
86
}
102
87
103
88
await this . $pluginsService . ensureAllDependenciesAreInstalled ( this . $projectData ) ;
104
89
}
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
+ }
105
134
}
106
135
107
136
$injector . registerCommand ( "update" , UpdateCommand ) ;
0 commit comments