@@ -2,6 +2,7 @@ import * as path from "path";
2
2
import * as semver from "semver" ;
3
3
import * as constants from "../constants" ;
4
4
import { UpdateControllerBase } from "./update-controller-base" ;
5
+ import { fromWindowsRelativePathToUnix } from "../common/helpers" ;
5
6
6
7
export class MigrateController extends UpdateControllerBase implements IMigrateController {
7
8
constructor (
@@ -15,20 +16,26 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
15
16
private $errors : IErrors ,
16
17
private $addPlatformService : IAddPlatformService ,
17
18
private $pluginsService : IPluginsService ,
18
- private $projectDataService : IProjectDataService ) {
19
+ private $projectDataService : IProjectDataService ,
20
+ private $resources : IResourceLoader ) {
19
21
super ( $fs , $platformCommandHelper , $platformsDataService , $packageInstallationManager , $packageManager ) ;
20
22
}
21
23
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
+
22
28
static readonly folders : string [ ] = [
23
29
constants . LIB_DIR_NAME ,
24
30
constants . HOOKS_DIR_NAME ,
25
31
constants . WEBPACK_CONFIG_NAME ,
26
32
constants . PACKAGE_JSON_FILE_NAME ,
27
33
constants . PACKAGE_LOCK_JSON_FILE_NAME ,
28
- constants . TSCCONFIG_TNS_JSON_NAME
34
+ constants . TSCCONFIG_TNS_JSON_NAME ,
35
+ constants . KARMA_CONFIG_NAME
29
36
] ;
30
37
31
- static readonly migrationDependencies : IMigrationDependency [ ] = [
38
+ private migrationDependencies : IMigrationDependency [ ] = [
32
39
{ packageName : constants . TNS_CORE_MODULES_NAME , verifiedVersion : "6.0.0-next-2019-06-20-155941-01" } ,
33
40
{ packageName : constants . TNS_CORE_MODULES_WIDGETS_NAME , verifiedVersion : "6.0.0-next-2019-06-20-155941-01" } ,
34
41
{ packageName : "node-sass" , isDev : true , verifiedVersion : "4.12.0" } ,
@@ -57,13 +64,13 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
57
64
//TODO update with no prerelease version compatible with webpack only hooks
58
65
{ packageName : "nativescript-vue" , verifiedVersion : "2.3.0-rc.0" } ,
59
66
{ 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
+ }
61
72
] ;
62
73
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
-
67
74
get verifiedPlatformVersions ( ) : IDictionary < string > {
68
75
return {
69
76
[ this . $devicePlatformsConstants . Android . toLowerCase ( ) ] : "6.0.0-2019-06-11-172137-01" ,
@@ -97,10 +104,14 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
97
104
public async shouldMigrate ( { projectDir } : IProjectDir ) : Promise < boolean > {
98
105
const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
99
106
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 ] ;
102
109
const hasDependency = this . hasDependency ( dependency , projectData ) ;
103
110
111
+ if ( hasDependency && dependency . shouldMigrateAction && dependency . shouldMigrateAction ( projectData ) ) {
112
+ return true ;
113
+ }
114
+
104
115
if ( hasDependency && dependency . replaceWith ) {
105
116
return true ;
106
117
}
@@ -138,32 +149,18 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
138
149
139
150
private async migrateDependencies ( projectData : IProjectData ) : Promise < void > {
140
151
this . $logger . info ( "Start dependencies migration." ) ;
141
- for ( let i = 0 ; i < MigrateController . migrationDependencies . length ; i ++ ) {
142
- const dependency = MigrateController . migrationDependencies [ i ] ;
152
+ for ( let i = 0 ; i < this . migrationDependencies . length ; i ++ ) {
153
+ const dependency = this . migrationDependencies [ i ] ;
143
154
const hasDependency = this . hasDependency ( dependency , projectData ) ;
144
155
145
- if ( hasDependency && dependency . replaceWith ) {
146
- this . $pluginsService . removeFromPackageJson ( dependency . packageName , dependency . isDev , projectData . projectDir ) ;
147
- const replacementDep = _ . find ( MigrateController . migrationDependencies , migrationPackage => migrationPackage . packageName === dependency . replaceWith ) ;
148
- if ( ! replacementDep ) {
149
- this . $errors . failWithoutHelp ( "Failed to find replacement dependency." ) ;
156
+ if ( hasDependency && dependency . migrateAction && dependency . shouldMigrateAction ( projectData ) ) {
157
+ const newDependencies = await dependency . migrateAction ( projectData , path . join ( projectData . projectDir , MigrateController . backupFolder ) ) ;
158
+ for ( const newDependency of newDependencies ) {
159
+ await this . migrateDependency ( newDependency , projectData ) ;
150
160
}
151
- this . $logger . info ( `Replacing '${ dependency . packageName } ' with '${ replacementDep . packageName } '.` ) ;
152
- this . $pluginsService . addToPackageJson ( replacementDep . packageName , replacementDep . verifiedVersion , replacementDep . isDev , projectData . projectDir ) ;
153
- continue ;
154
161
}
155
162
156
- if ( hasDependency && await this . shouldMigrateDependencyVersion ( dependency , projectData ) ) {
157
- this . $logger . info ( `Updating '${ dependency . packageName } ' to compatible version '${ dependency . verifiedVersion } '` ) ;
158
- this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
159
- continue ;
160
- }
161
-
162
- if ( ! hasDependency && dependency . shouldAddIfMissing ) {
163
- this . $logger . info ( `Adding '${ dependency . packageName } ' with version '${ dependency . verifiedVersion } '` ) ;
164
- this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
165
- continue ;
166
- }
163
+ await this . migrateDependency ( dependency , projectData ) ;
167
164
}
168
165
169
166
for ( const platform in this . $devicePlatformsConstants ) {
@@ -188,6 +185,32 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
188
185
this . $logger . info ( "Migration complete." ) ;
189
186
}
190
187
188
+ private async migrateDependency ( dependency : IMigrationDependency , projectData : IProjectData ) : Promise < void > {
189
+ const hasDependency = this . hasDependency ( dependency , projectData ) ;
190
+
191
+ if ( hasDependency && dependency . replaceWith ) {
192
+ this . $pluginsService . removeFromPackageJson ( dependency . packageName , dependency . isDev , projectData . projectDir ) ;
193
+ const replacementDep = _ . find ( this . migrationDependencies , migrationPackage => migrationPackage . packageName === dependency . replaceWith ) ;
194
+ if ( ! replacementDep ) {
195
+ this . $errors . failWithoutHelp ( "Failed to find replacement dependency." ) ;
196
+ }
197
+ this . $logger . info ( `Replacing '${ dependency . packageName } ' with '${ replacementDep . packageName } '.` ) ;
198
+ this . $pluginsService . addToPackageJson ( replacementDep . packageName , replacementDep . verifiedVersion , replacementDep . isDev , projectData . projectDir ) ;
199
+ return ;
200
+ }
201
+
202
+ if ( hasDependency && await this . shouldMigrateDependencyVersion ( dependency , projectData ) ) {
203
+ this . $logger . info ( `Updating '${ dependency . packageName } ' to compatible version '${ dependency . verifiedVersion } '` ) ;
204
+ this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
205
+ return ;
206
+ }
207
+
208
+ if ( ! hasDependency && dependency . shouldAddIfMissing ) {
209
+ this . $logger . info ( `Adding '${ dependency . packageName } ' with version '${ dependency . verifiedVersion } '` ) ;
210
+ this . $pluginsService . addToPackageJson ( dependency . packageName , dependency . verifiedVersion , dependency . isDev , projectData . projectDir ) ;
211
+ }
212
+ }
213
+
191
214
private async shouldMigrateDependencyVersion ( dependency : IMigrationDependency , projectData : IProjectData ) : Promise < boolean > {
192
215
const collection = dependency . isDev ? projectData . devDependencies : projectData . dependencies ;
193
216
const maxSatisfyingVersion = await this . getMaxDependencyVersion ( dependency . packageName , collection [ dependency . packageName ] ) ;
@@ -200,6 +223,35 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
200
223
201
224
return ! ( maxRuntimeVersion && semver . gte ( maxRuntimeVersion , targetVersion ) ) ;
202
225
}
226
+
227
+ private async migrateUnitTestRunner ( projectData : IProjectData , migrationBackupDirPath : string ) : Promise < IMigrationDependency [ ] > {
228
+ // Migrate karma.conf.js
229
+ const oldKarmaContent = this . $fs . readText ( path . join ( migrationBackupDirPath , constants . KARMA_CONFIG_NAME ) ) ;
230
+
231
+ const regExp = / f r a m e w o r k s : \s + \[ ( [ \S \s ] * ?) \] / g;
232
+ const matches = regExp . exec ( oldKarmaContent ) ;
233
+ const frameworks = ( matches && matches [ 1 ] && matches [ 1 ] . trim ( ) ) || '["jasmine"]' ;
234
+
235
+ const testsDir = path . join ( projectData . appDirectoryPath , 'tests' ) ;
236
+ const relativeTestsDir = path . relative ( projectData . projectDir , testsDir ) ;
237
+ const testFiles = `'${ fromWindowsRelativePathToUnix ( relativeTestsDir ) } /**/*.*'` ;
238
+
239
+ const karmaConfTemplate = this . $resources . readText ( 'test/karma.conf.js' ) ;
240
+ const karmaConf = _ . template ( karmaConfTemplate ) ( { frameworks, testFiles } ) ;
241
+ this . $fs . writeFile ( path . join ( projectData . projectDir , constants . KARMA_CONFIG_NAME ) , karmaConf ) ;
242
+
243
+ // Dependencies to migrate
244
+ const dependencies = [
245
+ { packageName : "karma-webpack" , verifiedVersion : "3.0.5" , isDev : true , shouldAddIfMissing : ! this . hasDependency ( { packageName : "karma-webpack" , isDev : true } , projectData ) } ,
246
+ { packageName : "karma-jasmine" , verifiedVersion : "2.0.1" , isDev : true } ,
247
+ { packageName : "karma-mocha" , verifiedVersion : "1.3.0" , isDev : true } ,
248
+ { packageName : "karma-chai" , verifiedVersion : "0.1.0" , isDev : true } ,
249
+ { packageName : "karma-qunit" , verifiedVersion : "3.1.2" , isDev : true } ,
250
+ { packageName : "karma" , verifiedVersion : "4.1.0" , isDev : true } ,
251
+ ] ;
252
+
253
+ return dependencies ;
254
+ }
203
255
}
204
256
205
257
$injector . register ( "migrateController" , MigrateController ) ;
0 commit comments