@@ -15,49 +15,34 @@ export class UpdateCommand implements ICommand {
15
15
this . $projectData . initializeProjectData ( ) ;
16
16
}
17
17
18
+ private folders : string [ ] = [ "lib" , "hooks" , "platforms" , "node_modules" ] ;
19
+ private tempFolder : string = ".tmp_backup" ;
20
+
18
21
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" ) ;
22
+ const tmpDir = path . join ( this . $projectData . projectDir , this . tempFolder ) ;
21
23
22
24
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
- }
25
+ this . backup ( tmpDir ) ;
32
26
} catch ( error ) {
33
27
this . $logger . error ( "Could not backup project folders!" ) ;
28
+ shelljs . rm ( "-fr" , tmpDir ) ;
34
29
return ;
35
30
}
36
31
37
32
try {
38
- await this . executeCore ( args , folders ) ;
33
+ await this . executeCore ( args ) ;
39
34
} 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
-
35
+ this . restoreBackup ( tmpDir ) ;
51
36
this . $logger . error ( "Could not update the project!" ) ;
52
37
} finally {
53
38
shelljs . rm ( "-fr" , tmpDir ) ;
54
39
}
55
40
}
56
41
57
42
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 ) ;
43
+ const platforms = this . getPlatforms ( ) ;
44
+
45
+ for ( const platform of platforms . packagePlatforms ) {
61
46
const platformData = this . $platformsData . getPlatformData ( platform , this . $projectData ) ;
62
47
const platformProjectService = platformData . platformProjectService ;
63
48
await platformProjectService . validate ( this . $projectData ) ;
@@ -66,42 +51,79 @@ export class UpdateCommand implements ICommand {
66
51
return args . length < 2 && this . $projectData . projectDir !== "" ;
67
52
}
68
53
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 [ ] = [ ] ;
54
+ private async executeCore ( args : string [ ] ) : Promise < void > {
55
+ const platforms = this . getPlatforms ( ) ;
73
56
74
- for ( const platform of availablePlatforms ) {
57
+ for ( const platform of _ . xor ( platforms . installed , platforms . packagePlatforms ) ) {
75
58
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
- }
59
+ this . $projectDataService . removeNSProperty ( this . $projectData . projectDir , platformData . frameworkPackageName ) ;
81
60
}
82
61
83
- await this . $platformService . removePlatforms ( platforms , this . $projectData ) ;
62
+ await this . $platformService . removePlatforms ( platforms . installed , this . $projectData ) ;
84
63
await this . $pluginsService . remove ( "tns-core-modules" , this . $projectData ) ;
85
64
await this . $pluginsService . remove ( "tns-core-modules-widgets" , this . $projectData ) ;
86
65
87
- for ( const folder of folders ) {
88
- shelljs . rm ( "-fr " , folder ) ;
66
+ for ( const folder of this . folders ) {
67
+ shelljs . rm ( "-rf " , path . join ( this . $projectData . projectDir , folder ) ) ;
89
68
}
90
69
91
- platforms = platforms . concat ( packagePlatforms ) ;
92
70
if ( args . length === 1 ) {
93
- for ( const platform of platforms ) {
71
+ for ( const platform of platforms . packagePlatforms ) {
94
72
await this . $platformService . addPlatforms ( [ platform + "@" + args [ 0 ] ] , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
95
73
}
96
74
97
75
await this . $pluginsService . add ( "tns-core-modules@" + args [ 0 ] , this . $projectData ) ;
98
76
} else {
99
- await this . $platformService . addPlatforms ( platforms , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
77
+ await this . $platformService . addPlatforms ( platforms . packagePlatforms , this . $options . platformTemplate , this . $projectData , this . $options , this . $options . frameworkPath ) ;
100
78
await this . $pluginsService . add ( "tns-core-modules" , this . $projectData ) ;
101
79
}
102
80
103
81
await this . $pluginsService . ensureAllDependenciesAreInstalled ( this . $projectData ) ;
104
82
}
83
+
84
+ private getPlatforms ( ) : { installed : string [ ] , packagePlatforms : string [ ] } {
85
+ const installedPlatforms = this . $platformService . getInstalledPlatforms ( this . $projectData ) ;
86
+ const availablePlatforms = this . $platformService . getAvailablePlatforms ( this . $projectData ) ;
87
+ const packagePlatforms : string [ ] = [ ] ;
88
+
89
+ for ( const platform of availablePlatforms ) {
90
+ const platformData = this . $platformsData . getPlatformData ( platform , this . $projectData ) ;
91
+ const platformVersion = this . $projectDataService . getNSValue ( this . $projectData . projectDir , platformData . frameworkPackageName ) ;
92
+ if ( platformVersion ) {
93
+ packagePlatforms . push ( platform ) ;
94
+ }
95
+ }
96
+
97
+ return {
98
+ installed : installedPlatforms ,
99
+ packagePlatforms : installedPlatforms . concat ( packagePlatforms )
100
+ } ;
101
+ }
102
+
103
+ private restoreBackup ( tmpDir : string ) : void {
104
+ shelljs . cp ( "-f" , path . join ( tmpDir , "package.json" ) , this . $projectData . projectDir ) ;
105
+ for ( const folder of this . folders ) {
106
+ shelljs . rm ( "-rf" , path . join ( this . $projectData . projectDir , folder ) ) ;
107
+
108
+ const folderToCopy = path . join ( tmpDir , folder ) ;
109
+
110
+ if ( this . $fs . exists ( folderToCopy ) ) {
111
+ shelljs . cp ( "-fr" , folderToCopy , this . $projectData . projectDir ) ;
112
+ }
113
+ }
114
+ }
115
+
116
+ private backup ( tmpDir : string ) : void {
117
+ shelljs . rm ( "-fr" , tmpDir ) ;
118
+ shelljs . mkdir ( tmpDir ) ;
119
+ shelljs . cp ( path . join ( this . $projectData . projectDir , "package.json" ) , tmpDir ) ;
120
+ for ( const folder of this . folders ) {
121
+ const folderToCopy = path . join ( this . $projectData . projectDir , folder ) ;
122
+ if ( this . $fs . exists ( folderToCopy ) ) {
123
+ shelljs . cp ( "-rf" , folderToCopy , tmpDir ) ;
124
+ }
125
+ }
126
+ }
105
127
}
106
128
107
129
$injector . registerCommand ( "update" , UpdateCommand ) ;
0 commit comments