11import * as path from "path" ;
22import * as semver from "semver" ;
33import * as constants from "../constants" ;
4- import { BaseUpdateController } from "./base- update-controller" ;
4+ import { UpdateControllerBase } from "./update-controller-base " ;
55
6- export class UpdateController extends BaseUpdateController implements IUpdateController {
6+ export class UpdateController extends UpdateControllerBase implements IUpdateController {
77 constructor (
88 protected $fs : IFileSystem ,
99 protected $platformsDataService : IPlatformsDataService ,
@@ -25,34 +25,32 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
2525 static readonly folders : string [ ] = [
2626 constants . LIB_DIR_NAME ,
2727 constants . HOOKS_DIR_NAME ,
28- //constants.PLATFORMS_DIR_NAME,
29- //constants.NODE_MODULES_FOLDER_NAME,
3028 constants . WEBPACK_CONFIG_NAME ,
3129 constants . PACKAGE_JSON_FILE_NAME ,
3230 constants . PACKAGE_LOCK_JSON_FILE_NAME
3331 ] ;
3432
35- static readonly tempFolder : string = ".update_backup" ;
33+ static readonly backupFolder : string = ".update_backup" ;
3634 static readonly updateFailMessage : string = "Could not update the project!" ;
3735 static readonly backupFailMessage : string = "Could not backup project folders!" ;
3836
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 ) ;
4240
4341 try {
44- this . backup ( UpdateController . folders , tmpDir , projectData ) ;
42+ this . backup ( UpdateController . folders , backupDir , projectData . projectDir ) ;
4543 } catch ( error ) {
4644 this . $logger . error ( UpdateController . backupFailMessage ) ;
47- this . $fs . deleteDirectory ( tmpDir ) ;
45+ this . $fs . deleteDirectory ( backupDir ) ;
4846 return ;
4947 }
5048
5149 try {
5250 await this . cleanUpProject ( projectData ) ;
53- await this . updateProject ( projectData , version ) ;
51+ await this . updateProject ( projectData , updateOptions . version ) ;
5452 } catch ( error ) {
55- this . restoreBackup ( UpdateController . folders , tmpDir , projectData ) ;
53+ this . restoreBackup ( UpdateController . folders , backupDir , projectData . projectDir ) ;
5654 this . $logger . error ( UpdateController . updateFailMessage ) ;
5755 }
5856 }
@@ -74,9 +72,9 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
7472 for ( const platform in this . $devicePlatformsConstants ) {
7573 const lowercasePlatform = platform . toLowerCase ( ) ;
7674 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 ) ) {
8078 return true ;
8179 }
8280 }
@@ -119,7 +117,7 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
119117 private async updateDependencies ( { dependencies, areDev, projectData} : { dependencies : IDictionary < string > , areDev : boolean , projectData : IProjectData } ) {
120118 for ( const dependency in dependencies ) {
121119 const templateVersion = dependencies [ dependency ] ;
122- if ( this . shouldSkipDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
120+ if ( ! this . hasDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
123121 continue ;
124122 }
125123
@@ -133,18 +131,16 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
133131 private async shouldUpdateDependency ( dependency : string , targetVersion : string , isDev : boolean , projectData : IProjectData ) {
134132 const collection = isDev ? projectData . devDependencies : projectData . dependencies ;
135133 const projectVersion = collection [ dependency ] ;
136- const maxSatisfyingTargetVersion = await this . $packageInstallationManager . maxSatisfyingVersion ( dependency , targetVersion ) ;
134+ const maxSatisfyingTargetVersion = await this . getMaxDependencyVersion ( dependency , targetVersion ) ;
137135 const maxSatisfyingProjectVersion = await this . getMaxDependencyVersion ( dependency , projectVersion ) ;
138136
139- if ( maxSatisfyingProjectVersion && semver . gt ( maxSatisfyingTargetVersion , maxSatisfyingProjectVersion ) ) {
140- return true ;
141- }
137+ return maxSatisfyingProjectVersion && maxSatisfyingTargetVersion && semver . gt ( maxSatisfyingTargetVersion , maxSatisfyingProjectVersion ) ;
142138 }
143139
144140 private async hasDependenciesToUpdate ( { dependencies, areDev, projectData} : { dependencies : IDictionary < string > , areDev : boolean , projectData :IProjectData } ) {
145141 for ( const dependency in dependencies ) {
146142 const templateVersion = dependencies [ dependency ] ;
147- if ( this . shouldSkipDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
143+ if ( ! this . hasDependency ( { packageName : dependency , isDev : areDev } , projectData ) ) {
148144 continue ;
149145 }
150146
@@ -154,19 +150,32 @@ export class UpdateController extends BaseUpdateController implements IUpdateCon
154150 }
155151 }
156152
157- private async updateRuntimes ( templateData : Object , projectData : IProjectData ) {
153+ private async updateRuntimes ( templateManifest : Object , projectData : IProjectData ) {
158154 for ( const platform in this . $devicePlatformsConstants ) {
159155 const lowercasePlatform = platform . toLowerCase ( ) ;
160156 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 ) ;
166162 }
167163 }
168164 }
169165
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+
170179 private async _getTemplateManifest ( templateName : string , version : string ) {
171180 let packageVersion = version ? version : await this . $packageInstallationManager . getLatestCompatibleVersionSafe ( templateName ) ;
172181 packageVersion = semver . valid ( version ) ? version : await this . $packageManager . getTagVersion ( templateName , packageVersion ) ;
0 commit comments