@@ -2,6 +2,7 @@ import * as path from "path";
22import * as semver from "semver" ;
33import * as constants from "../constants" ;
44import { UpdateControllerBase } from "./update-controller-base" ;
5+ import { fromWindowsRelativePathToUnix } from "../common/helpers" ;
56
67export class MigrateController extends UpdateControllerBase implements IMigrateController {
78 constructor (
@@ -15,20 +16,26 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
1516 private $errors : IErrors ,
1617 private $addPlatformService : IAddPlatformService ,
1718 private $pluginsService : IPluginsService ,
18- private $projectDataService : IProjectDataService ) {
19+ private $projectDataService : IProjectDataService ,
20+ private $resources : IResourceLoader ) {
1921 super ( $fs , $platformCommandHelper , $platformsDataService , $packageInstallationManager , $packageManager ) ;
2022 }
2123
24+ static readonly backupFolder : string = ".migration_backup" ;
25+ static readonly migrateFailMessage : string = "Could not migrate the project!" ;
26+ static readonly backupFailMessage : string = "Could not backup project folders!" ;
27+
2228 static readonly folders : string [ ] = [
2329 constants . LIB_DIR_NAME ,
2430 constants . HOOKS_DIR_NAME ,
2531 constants . WEBPACK_CONFIG_NAME ,
2632 constants . PACKAGE_JSON_FILE_NAME ,
2733 constants . PACKAGE_LOCK_JSON_FILE_NAME ,
28- constants . TSCCONFIG_TNS_JSON_NAME
34+ constants . TSCCONFIG_TNS_JSON_NAME ,
35+ constants . KARMA_CONFIG_NAME
2936 ] ;
3037
31- static readonly migrationDependencies : IMigrationDependency [ ] = [
38+ private migrationDependencies : IMigrationDependency [ ] = [
3239 { packageName : constants . TNS_CORE_MODULES_NAME , verifiedVersion : "6.0.0-next-2019-06-20-155941-01" } ,
3340 { packageName : constants . TNS_CORE_MODULES_WIDGETS_NAME , verifiedVersion : "6.0.0-next-2019-06-20-155941-01" } ,
3441 { packageName : "node-sass" , isDev : true , verifiedVersion : "4.12.0" } ,
@@ -57,13 +64,13 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
5764 //TODO update with no prerelease version compatible with webpack only hooks
5865 { packageName : "nativescript-vue" , verifiedVersion : "2.3.0-rc.0" } ,
5966 { packageName : "nativescript-permissions" , verifiedVersion : "1.3.0" } ,
60- { packageName : "nativescript-cardview" , verifiedVersion : "3.2.0" }
67+ { packageName : "nativescript-cardview" , verifiedVersion : "3.2.0" } ,
68+ { packageName : "nativescript-unit-test-runner" , verifiedVersion : "0.6.3" ,
69+ shouldMigrateAction : ( projectData : IProjectData ) => this . hasDependency ( { packageName : "nativescript-unit-test-runner" , isDev : false } , projectData ) ,
70+ migrateAction : this . migrateUnitTestRunner . bind ( this )
71+ }
6172 ] ;
6273
63- static readonly backupFolder : string = ".migration_backup" ;
64- static readonly migrateFailMessage : string = "Could not migrate the project!" ;
65- static readonly backupFailMessage : string = "Could not backup project folders!" ;
66-
6774 get verifiedPlatformVersions ( ) : IDictionary < string > {
6875 return {
6976 [ this . $devicePlatformsConstants . Android . toLowerCase ( ) ] : "6.0.0-2019-06-11-172137-01" ,
@@ -97,10 +104,14 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
97104 public async shouldMigrate ( { projectDir } : IProjectDir ) : Promise < boolean > {
98105 const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
99106
100- for ( let i = 0 ; i < MigrateController . migrationDependencies . length ; i ++ ) {
101- const dependency = MigrateController . migrationDependencies [ i ] ;
107+ for ( let i = 0 ; i < this . migrationDependencies . length ; i ++ ) {
108+ const dependency = this . migrationDependencies [ i ] ;
102109 const hasDependency = this . hasDependency ( dependency , projectData ) ;
103110
111+ if ( hasDependency && dependency . shouldMigrateAction && dependency . shouldMigrateAction ( projectData ) ) {
112+ return true ;
113+ }
114+
104115 if ( hasDependency && dependency . replaceWith ) {
105116 return true ;
106117 }
@@ -135,32 +146,18 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
135146
136147 private async migrateDependencies ( projectData : IProjectData ) : Promise < void > {
137148 this . $logger . info ( "Start dependencies migration." ) ;
138- for ( let i = 0 ; i < MigrateController . migrationDependencies . length ; i ++ ) {
139- const dependency = MigrateController . migrationDependencies [ i ] ;
149+ for ( let i = 0 ; i < this . migrationDependencies . length ; i ++ ) {
150+ const dependency = this . migrationDependencies [ i ] ;
140151 const hasDependency = this . hasDependency ( dependency , projectData ) ;
141152
142- if ( hasDependency && dependency . replaceWith ) {
143- this . $pluginsService . removeFromPackageJson ( dependency . packageName , dependency . isDev , projectData . projectDir ) ;
144- const replacementDep = _ . find ( MigrateController . migrationDependencies , migrationPackage => migrationPackage . packageName === dependency . replaceWith ) ;
145- if ( ! replacementDep ) {
146- this . $errors . failWithoutHelp ( "Failed to find replacement dependency." ) ;
153+ if ( hasDependency && dependency . migrateAction && dependency . shouldMigrateAction ( projectData ) ) {
154+ const newDependencies = await dependency . migrateAction ( projectData , path . join ( projectData . projectDir , MigrateController . backupFolder ) ) ;
155+ for ( const newDependency of newDependencies ) {
156+ await this . migrateDependency ( newDependency , projectData ) ;
147157 }
148- this . $logger . info ( `Replacing '${ dependency . packageName } ' with '${ replacementDep . packageName } '.` ) ;
149- this . $pluginsService . addToPackageJson ( replacementDep . packageName , replacementDep . verifiedVersion , replacementDep . isDev , projectData . projectDir ) ;
150- continue ;
151- }
152-
153- if ( hasDependency && await this . shouldMigrateDependencyVersion ( dependency , projectData ) ) {
154- this . $logger . info ( `Updating '${ dependency . packageName } ' to compatible version '${ dependency . verifiedVersion } '` ) ;
155- this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
156- continue ;
157158 }
158159
159- if ( ! hasDependency && dependency . shouldAddIfMissing ) {
160- this . $logger . info ( `Adding '${ dependency . packageName } ' with version '${ dependency . verifiedVersion } '` ) ;
161- this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
162- continue ;
163- }
160+ await this . migrateDependency ( dependency , projectData ) ;
164161 }
165162
166163 for ( const platform in this . $devicePlatformsConstants ) {
@@ -185,6 +182,32 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
185182 this . $logger . info ( "Migration complete." ) ;
186183 }
187184
185+ private async migrateDependency ( dependency : IMigrationDependency , projectData : IProjectData ) : Promise < void > {
186+ const hasDependency = this . hasDependency ( dependency , projectData ) ;
187+
188+ if ( hasDependency && dependency . replaceWith ) {
189+ this . $pluginsService . removeFromPackageJson ( dependency . packageName , dependency . isDev , projectData . projectDir ) ;
190+ const replacementDep = _ . find ( this . migrationDependencies , migrationPackage => migrationPackage . packageName === dependency . replaceWith ) ;
191+ if ( ! replacementDep ) {
192+ this . $errors . failWithoutHelp ( "Failed to find replacement dependency." ) ;
193+ }
194+ this . $logger . info ( `Replacing '${ dependency . packageName } ' with '${ replacementDep . packageName } '.` ) ;
195+ this . $pluginsService . addToPackageJson ( replacementDep . packageName , replacementDep . verifiedVersion , replacementDep . isDev , projectData . projectDir ) ;
196+ return ;
197+ }
198+
199+ if ( hasDependency && await this . shouldMigrateDependencyVersion ( dependency , projectData ) ) {
200+ this . $logger . info ( `Updating '${ dependency . packageName } ' to compatible version '${ dependency . verifiedVersion } '` ) ;
201+ this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
202+ return ;
203+ }
204+
205+ if ( ! hasDependency && dependency . shouldAddIfMissing ) {
206+ this . $logger . info ( `Adding '${ dependency . packageName } ' with version '${ dependency . verifiedVersion } '` ) ;
207+ this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
208+ }
209+ }
210+
188211 private async shouldMigrateDependencyVersion ( dependency : IMigrationDependency , projectData : IProjectData ) : Promise < boolean > {
189212 const collection = dependency . isDev ? projectData . devDependencies : projectData . dependencies ;
190213 const maxSatisfyingVersion = await this . getMaxDependencyVersion ( dependency . packageName , collection [ dependency . packageName ] ) ;
@@ -197,6 +220,35 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
197220
198221 return ! ( maxRuntimeVersion && semver . gte ( maxRuntimeVersion , targetVersion ) ) ;
199222 }
223+
224+ private async migrateUnitTestRunner ( projectData : IProjectData , migrationBackupDirPath : string ) : Promise < IMigrationDependency [ ] > {
225+ // Migrate karma.conf.js
226+ const oldKarmaContent = this . $fs . readText ( path . join ( migrationBackupDirPath , constants . KARMA_CONFIG_NAME ) ) ;
227+
228+ const regExp = / f r a m e w o r k s : \s + \[ ( \S + ) \] \, / g;
229+ const matches = regExp . exec ( oldKarmaContent ) ;
230+ const frameworks = ( matches && matches [ 1 ] ) || '["jasmine"]' ;
231+
232+ const testsDir = path . join ( projectData . appDirectoryPath , 'tests' ) ;
233+ const relativeTestsDir = path . relative ( projectData . projectDir , testsDir ) ;
234+ const testFiles = `'${ fromWindowsRelativePathToUnix ( relativeTestsDir ) } /**/*.*'` ;
235+
236+ const karmaConfTemplate = this . $resources . readText ( 'test/karma.conf.js' ) ;
237+ const karmaConf = _ . template ( karmaConfTemplate ) ( { frameworks, testFiles } ) ;
238+ this . $fs . writeFile ( path . join ( projectData . projectDir , constants . KARMA_CONFIG_NAME ) , karmaConf ) ;
239+
240+ // Dependencies to migrate
241+ const dependencies = [
242+ { packageName : "karma-webpack" , verifiedVersion : "3.0.5" , isDev : true , shouldAddIfMissing : ! this . hasDependency ( { packageName : "karma-webpack" , isDev : true } , projectData ) } ,
243+ { packageName : "karma-jasmine" , verifiedVersion : "2.0.1" , isDev : true } ,
244+ { packageName : "karma-mocha" , verifiedVersion : "1.3.0" , isDev : true } ,
245+ { packageName : "karma-chai" , verifiedVersion : "0.1.0" , isDev : true } ,
246+ { packageName : "karma-qunit" , verifiedVersion : "3.1.2" , isDev : true } ,
247+ { packageName : "karma" , verifiedVersion : "4.1.0" , isDev : true } ,
248+ ] ;
249+
250+ return dependencies ;
251+ }
200252}
201253
202254$injector . register ( "migrateController" , MigrateController ) ;
0 commit comments