1
1
import * as path from "path" ;
2
2
import * as semver from "semver" ;
3
3
import * as constants from "../constants" ;
4
- import { BaseUpdateController } from "./base- update-controller" ;
4
+ import { UpdateControllerBase } from "./update-controller-base " ;
5
5
6
- export class UpdateController extends BaseUpdateController implements IUpdateController {
6
+ export class UpdateController extends UpdateControllerBase implements IUpdateController {
7
7
constructor (
8
8
protected $fs : IFileSystem ,
9
9
protected $platformsDataService : IPlatformsDataService ,
@@ -25,34 +25,32 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
25
25
static readonly folders : string [ ] = [
26
26
constants . LIB_DIR_NAME ,
27
27
constants . HOOKS_DIR_NAME ,
28
- //constants.PLATFORMS_DIR_NAME,
29
- //constants.NODE_MODULES_FOLDER_NAME,
30
28
constants . WEBPACK_CONFIG_NAME ,
31
29
constants . PACKAGE_JSON_FILE_NAME ,
32
30
constants . PACKAGE_LOCK_JSON_FILE_NAME
33
31
] ;
34
32
35
- static readonly tempFolder : string = ".update_backup" ;
33
+ static readonly backupFolder : string = ".update_backup" ;
36
34
static readonly updateFailMessage : string = "Could not update the project!" ;
37
35
static readonly backupFailMessage : string = "Could not backup project folders!" ;
38
36
39
- public async update ( projectDir : string , version ?: string ) : Promise < void > {
40
- const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
41
- const tmpDir = path . join ( projectDir , UpdateController . tempFolder ) ;
37
+ public async update ( updateOptions : IUpdateOptions ) : Promise < void > {
38
+ const projectData = this . $projectDataService . getProjectData ( updateOptions . projectDir ) ;
39
+ const backupDir = path . join ( updateOptions . projectDir , UpdateController . backupFolder ) ;
42
40
43
41
try {
44
- this . backup ( UpdateController . folders , tmpDir , projectData ) ;
42
+ this . backup ( UpdateController . folders , backupDir , projectData . projectDir ) ;
45
43
} catch ( error ) {
46
44
this . $logger . error ( UpdateController . backupFailMessage ) ;
47
- this . $fs . deleteDirectory ( tmpDir ) ;
45
+ this . $fs . deleteDirectory ( backupDir ) ;
48
46
return ;
49
47
}
50
48
51
49
try {
52
50
await this . cleanUpProject ( projectData ) ;
53
- await this . updateProject ( projectData , version ) ;
51
+ await this . updateProject ( projectData , updateOptions . version ) ;
54
52
} catch ( error ) {
55
- this . restoreBackup ( UpdateController . folders , tmpDir , projectData ) ;
53
+ this . restoreBackup ( UpdateController . folders , backupDir , projectData . projectDir ) ;
56
54
this . $logger . error ( UpdateController . updateFailMessage ) ;
57
55
}
58
56
}
@@ -74,9 +72,9 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
74
72
for ( const platform in this . $devicePlatformsConstants ) {
75
73
const lowercasePlatform = platform . toLowerCase ( ) ;
76
74
const platformData = this . $platformsDataService . getPlatformData ( lowercasePlatform , projectData ) ;
77
- const currentPlatformData = this . $projectDataService . getNSValueFromContent ( templateManifest , platformData . frameworkPackageName ) ;
78
- const runtimeVersion = currentPlatformData && currentPlatformData . version ;
79
- if ( runtimeVersion && await this . shouldUpdateRuntimeVersion ( { targetVersion : runtimeVersion , platform, projectData, shouldAdd : false } ) ) {
75
+ const templatePlatformData = this . $projectDataService . getNSValueFromContent ( templateManifest , platformData . frameworkPackageName ) ;
76
+ const templateRuntimeVersion = templatePlatformData && templatePlatformData . version ;
77
+ if ( templateRuntimeVersion && await this . shouldUpdateRuntimeVersion ( templateRuntimeVersion , platformData . frameworkPackageName , platform , projectData ) ) {
80
78
return true ;
81
79
}
82
80
}
@@ -119,7 +117,7 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
119
117
private async updateDependencies ( { dependencies, areDev, projectData} : { dependencies : IDictionary < string > , areDev : boolean , projectData : IProjectData } ) {
120
118
for ( const dependency in dependencies ) {
121
119
const templateVersion = dependencies [ dependency ] ;
122
- if ( this . shouldSkipDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
120
+ if ( ! this . hasDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
123
121
continue ;
124
122
}
125
123
@@ -133,18 +131,16 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
133
131
private async shouldUpdateDependency ( dependency : string , targetVersion : string , isDev : boolean , projectData : IProjectData ) {
134
132
const collection = isDev ? projectData . devDependencies : projectData . dependencies ;
135
133
const projectVersion = collection [ dependency ] ;
136
- const maxSatisfyingTargetVersion = await this . $packageInstallationManager . maxSatisfyingVersion ( dependency , targetVersion ) ;
134
+ const maxSatisfyingTargetVersion = await this . getMaxDependencyVersion ( dependency , targetVersion ) ;
137
135
const maxSatisfyingProjectVersion = await this . getMaxDependencyVersion ( dependency , projectVersion ) ;
138
136
139
- if ( maxSatisfyingProjectVersion && semver . gt ( maxSatisfyingTargetVersion , maxSatisfyingProjectVersion ) ) {
140
- return true ;
141
- }
137
+ return maxSatisfyingProjectVersion && maxSatisfyingTargetVersion && semver . gt ( maxSatisfyingTargetVersion , maxSatisfyingProjectVersion ) ;
142
138
}
143
139
144
140
private async hasDependenciesToUpdate ( { dependencies, areDev, projectData} : { dependencies : IDictionary < string > , areDev : boolean , projectData :IProjectData } ) {
145
141
for ( const dependency in dependencies ) {
146
142
const templateVersion = dependencies [ dependency ] ;
147
- if ( this . shouldSkipDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
143
+ if ( ! this . hasDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
148
144
continue ;
149
145
}
150
146
@@ -154,19 +150,32 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
154
150
}
155
151
}
156
152
157
- private async updateRuntimes ( templateData : Object , projectData : IProjectData ) {
153
+ private async updateRuntimes ( templateManifest : Object , projectData : IProjectData ) {
158
154
for ( const platform in this . $devicePlatformsConstants ) {
159
155
const lowercasePlatform = platform . toLowerCase ( ) ;
160
156
const platformData = this . $platformsDataService . getPlatformData ( lowercasePlatform , projectData ) ;
161
- const currentPlatformData = this . $projectDataService . getNSValueFromContent ( templateData , platformData . frameworkPackageName ) ;
162
- const runtimeVersion = currentPlatformData && currentPlatformData . version ;
163
- if ( runtimeVersion && await this . shouldUpdateRuntimeVersion ( { targetVersion : runtimeVersion , platform, projectData, shouldAdd : false } ) ) {
164
- this . $logger . info ( `Updating ${ platform } platform to version '${ runtimeVersion } '.` ) ;
165
- await this . $addPlatformService . setPlatformVersion ( platformData , projectData , runtimeVersion ) ;
157
+ const templatePlatformData = this . $projectDataService . getNSValueFromContent ( templateManifest , platformData . frameworkPackageName ) ;
158
+ const templateRuntimeVersion = templatePlatformData && templatePlatformData . version ;
159
+ if ( templateRuntimeVersion && await this . shouldUpdateRuntimeVersion ( templateRuntimeVersion , platformData . frameworkPackageName , platform , projectData ) ) {
160
+ this . $logger . info ( `Updating ${ platform } platform to version '${ templateRuntimeVersion } '.` ) ;
161
+ await this . $addPlatformService . setPlatformVersion ( platformData , projectData , templateRuntimeVersion ) ;
166
162
}
167
163
}
168
164
}
169
165
166
+ private async shouldUpdateRuntimeVersion ( templateRuntimeVersion : string , frameworkPackageName : string , platform : string , projectData : IProjectData ) : Promise < boolean > {
167
+ const hasRuntimeDependency = this . hasRuntimeDependency ( { platform, projectData} ) ;
168
+
169
+ if ( ! hasRuntimeDependency ) {
170
+ return false ;
171
+ }
172
+
173
+ const maxTemplateRuntimeVersion = await this . getMaxDependencyVersion ( frameworkPackageName , templateRuntimeVersion ) ;
174
+ const maxRuntimeVersion = await this . getMaxRuntimeVersion ( { platform, projectData} ) ;
175
+
176
+ return maxTemplateRuntimeVersion && maxRuntimeVersion && semver . gt ( maxTemplateRuntimeVersion , maxRuntimeVersion ) ;
177
+ }
178
+
170
179
private async _getTemplateManifest ( templateName : string , version : string ) {
171
180
let packageVersion = version ? version : await this . $packageInstallationManager . getLatestCompatibleVersionSafe ( templateName ) ;
172
181
packageVersion = semver . valid ( version ) ? version : await this . $packageManager . getTagVersion ( templateName , packageVersion ) ;
0 commit comments