@@ -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
}
@@ -135,32 +146,18 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
135
146
136
147
private async migrateDependencies ( projectData : IProjectData ) : Promise < void > {
137
148
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 ] ;
140
151
const hasDependency = this . hasDependency ( dependency , projectData ) ;
141
152
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 ) ;
147
157
}
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 ;
157
158
}
158
159
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 ) ;
164
161
}
165
162
166
163
for ( const platform in this . $devicePlatformsConstants ) {
@@ -185,6 +182,32 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
185
182
this . $logger . info ( "Migration complete." ) ;
186
183
}
187
184
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
+
188
211
private async shouldMigrateDependencyVersion ( dependency : IMigrationDependency , projectData : IProjectData ) : Promise < boolean > {
189
212
const collection = dependency . isDev ? projectData . devDependencies : projectData . dependencies ;
190
213
const maxSatisfyingVersion = await this . getMaxDependencyVersion ( dependency . packageName , collection [ dependency . packageName ] ) ;
@@ -197,6 +220,35 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
197
220
198
221
return ! ( maxRuntimeVersion && semver . gte ( maxRuntimeVersion , targetVersion ) ) ;
199
222
}
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
+ }
200
252
}
201
253
202
254
$injector . register ( "migrateController" , MigrateController ) ;
0 commit comments