@@ -3,126 +3,31 @@ hook.postinstall();
3
3
4
4
var fs = require ( "fs" ) ;
5
5
var path = require ( "path" ) ;
6
-
7
- var __migrations = [
8
- inlineSourceMapMigration ,
9
- addDomLibs ,
10
- addIterableToAngularProjects ,
11
- ] ;
12
-
6
+ var upgrader = require ( "./tsconfig-upgrader" ) ;
13
7
14
8
var projectDir = hook . findProjectDir ( ) ;
15
9
if ( projectDir ) {
16
10
var tsconfigPath = path . join ( projectDir , "tsconfig.json" ) ;
17
11
if ( fs . existsSync ( tsconfigPath ) ) {
18
- migrateTsconfig ( tsconfigPath ) ;
12
+ upgrader . migrateTsConfig ( tsconfigPath , projectDir ) ;
19
13
} else {
20
14
createTsconfig ( tsconfigPath ) ;
21
15
}
22
- createReferenceFile ( ) ;
16
+ if ( ! upgrader . hasModules30 ( projectDir ) ) {
17
+ createReferenceFile ( ) ;
18
+ }
23
19
installTypescript ( ) ;
24
20
}
25
21
26
22
function createReferenceFile ( ) {
27
23
var referenceFilePath = path . join ( projectDir , "references.d.ts" ) ,
28
- content = ' /// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" /> Needed for autocompletion and compilation.' ;
24
+ content = " /// <reference path=\ "./node_modules/tns-core-modules/tns-core-modules.d.ts\ " /> Needed for autocompletion and compilation." ;
29
25
30
26
if ( ! fs . existsSync ( referenceFilePath ) ) {
31
27
fs . appendFileSync ( referenceFilePath , content ) ;
32
28
}
33
29
}
34
30
35
- function inlineSourceMapMigration ( existingConfig , displayableTsconfigPath ) {
36
- if ( existingConfig . compilerOptions ) {
37
- if ( "sourceMap" in existingConfig [ "compilerOptions" ] ) {
38
- delete existingConfig [ "compilerOptions" ] [ "sourceMap" ] ;
39
- console . warn ( "> Deleted \"compilerOptions.sourceMap\" setting in \"" + displayableTsconfigPath + "\"." ) ;
40
- console . warn ( "> Inline source maps will be used when building in Debug configuration from now on." ) ;
41
- }
42
- }
43
- }
44
-
45
- function addIterableToAngularProjects ( existingConfig ) {
46
- var packageJsonPath = path . join ( projectDir , "package.json" ) ;
47
- var packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath ) ) ;
48
- var dependencies = packageJson . dependencies || [ ] ;
49
-
50
- var hasAngular = Object . keys ( dependencies ) . includes ( "nativescript-angular" ) ;
51
- var hasRelevantAngularVersion = / [ 4 - 9 ] \. \d + \. \d + / i. test ( dependencies [ "@angular/core" ] ) ;
52
- if ( hasAngular && hasRelevantAngularVersion ) {
53
- console . log ( "Adding 'es2015.iterable' lib to tsconfig.json..." ) ;
54
- addTsLib ( existingConfig , "es2015.iterable" ) ;
55
- }
56
- }
57
-
58
- function addDomLibs ( existingConfig ) {
59
- function relevantModulesVersion ( version ) {
60
- return / [ 3 - 9 ] \. \d + \. \d + / i. test ( version ) ;
61
- }
62
-
63
- function hasRelevantModulesDependency ( ) {
64
- var packageJsonPath = path . join ( projectDir , "package.json" ) ;
65
- var packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath ) ) ;
66
- var dependencies = packageJson . dependencies || [ ] ;
67
-
68
- return relevantModulesVersion ( dependencies [ "tns-core-modules" ] ) ;
69
- }
70
-
71
- function hasRelevantModulesPackage ( ) {
72
- var packageJsonPath = path . join ( projectDir , "node_modules" , "tns-core-modules" , "package.json" ) ;
73
- if ( ! fs . existsSync ( packageJsonPath ) ) {
74
- return false ;
75
- }
76
-
77
- var packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath ) ) ;
78
- return relevantModulesVersion ( packageJson . version ) ;
79
- }
80
-
81
- if ( hasRelevantModulesDependency ( ) || hasRelevantModulesPackage ( ) ) {
82
- console . log ( "Adding 'es6' lib to tsconfig.json..." ) ;
83
- addTsLib ( existingConfig , "es6" ) ;
84
- console . log ( "Adding 'dom' lib to tsconfig.json..." ) ;
85
- addTsLib ( existingConfig , "dom" ) ;
86
- }
87
- }
88
-
89
- function addTsLib ( existingConfig , libName ) {
90
- if ( existingConfig . compilerOptions ) {
91
- var options = existingConfig . compilerOptions ;
92
- if ( ! options . lib ) {
93
- options . lib = [ ] ;
94
- }
95
- if ( ! options . lib . find ( function ( l ) {
96
- return libName . toLowerCase ( ) === l . toLowerCase ( ) ;
97
- } ) ) {
98
- options . lib . push ( libName ) ;
99
- }
100
- }
101
- }
102
-
103
- function migrateTsconfig ( tsconfigPath ) {
104
- var displayableTsconfigPath = path . relative ( projectDir , tsconfigPath ) ;
105
-
106
- function withTsConfig ( action ) {
107
- var existingConfig = null ;
108
- try {
109
- var existingConfigContents = fs . readFileSync ( tsconfigPath ) ;
110
- existingConfig = JSON . parse ( existingConfigContents ) ;
111
- } catch ( e ) {
112
- console . error ( "Invalid " + displayableTsconfigPath + ": " + e ) ;
113
- return ;
114
- }
115
- action ( existingConfig ) ;
116
- fs . writeFileSync ( tsconfigPath , JSON . stringify ( existingConfig , null , 4 ) ) ;
117
- }
118
-
119
- withTsConfig ( function ( existingConfig ) {
120
- __migrations . forEach ( function ( migration ) {
121
- migration ( existingConfig , displayableTsconfigPath ) ;
122
- } ) ;
123
- } ) ;
124
- }
125
-
126
31
function createTsconfig ( tsconfigPath ) {
127
32
var tsconfig = { } ;
128
33
@@ -134,8 +39,7 @@ function createTsconfig(tsconfigPath) {
134
39
noEmitHelpers : true ,
135
40
noEmitOnError : true ,
136
41
} ;
137
- addDomLibs ( tsconfig ) ;
138
- addIterableToAngularProjects ( tsconfig ) ;
42
+ upgrader . migrateProject ( tsconfig , tsconfigPath , projectDir ) ;
139
43
140
44
tsconfig . exclude = [ "node_modules" , "platforms" , "**/*.aot.ts" ] ;
141
45
0 commit comments