@@ -3,126 +3,31 @@ hook.postinstall();
33
44var fs = require ( "fs" ) ;
55var path = require ( "path" ) ;
6-
7- var __migrations = [
8- inlineSourceMapMigration ,
9- addDomLibs ,
10- addIterableToAngularProjects ,
11- ] ;
12-
6+ var upgrader = require ( "./tsconfig-upgrader" ) ;
137
148var projectDir = hook . findProjectDir ( ) ;
159if ( projectDir ) {
1610 var tsconfigPath = path . join ( projectDir , "tsconfig.json" ) ;
1711 if ( fs . existsSync ( tsconfigPath ) ) {
18- migrateTsconfig ( tsconfigPath ) ;
12+ upgrader . migrateTsConfig ( tsconfigPath , projectDir ) ;
1913 } else {
2014 createTsconfig ( tsconfigPath ) ;
2115 }
22- createReferenceFile ( ) ;
16+ if ( ! upgrader . hasModules30 ( projectDir ) ) {
17+ createReferenceFile ( ) ;
18+ }
2319 installTypescript ( ) ;
2420}
2521
2622function createReferenceFile ( ) {
2723 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." ;
2925
3026 if ( ! fs . existsSync ( referenceFilePath ) ) {
3127 fs . appendFileSync ( referenceFilePath , content ) ;
3228 }
3329}
3430
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-
12631function createTsconfig ( tsconfigPath ) {
12732 var tsconfig = { } ;
12833
@@ -134,8 +39,7 @@ function createTsconfig(tsconfigPath) {
13439 noEmitHelpers : true ,
13540 noEmitOnError : true ,
13641 } ;
137- addDomLibs ( tsconfig ) ;
138- addIterableToAngularProjects ( tsconfig ) ;
42+ upgrader . migrateProject ( tsconfig , tsconfigPath , projectDir ) ;
13943
14044 tsconfig . exclude = [ "node_modules" , "platforms" , "**/*.aot.ts" ] ;
14145
0 commit comments