@@ -7,78 +7,100 @@ var upgrader = require("./tsconfig-upgrader");
77
88var projectDir = hook . findProjectDir ( ) ;
99if ( projectDir ) {
10- var tsconfigPath = path . join ( projectDir , "tsconfig.json" ) ;
11- var hasModules30 = upgrader . hasModules30 ( projectDir ) ;
12- if ( fs . existsSync ( tsconfigPath ) ) {
13- upgrader . migrateTsConfig ( tsconfigPath , projectDir ) ;
14- } else {
15- createTsconfig ( tsconfigPath ) ;
16- }
17-
18- if ( ! hasModules30 ) {
19- createReferenceFile ( ) ;
20- }
21-
22- installTypescript ( { force : hasModules30 } ) ;
10+ const tsconfigPath = path . join ( projectDir , "tsconfig.json" ) ;
11+ if ( fs . existsSync ( tsconfigPath ) ) {
12+ upgrader . migrateTsConfig ( tsconfigPath , projectDir ) ;
13+ } else {
14+ createTsconfig ( tsconfigPath ) ;
15+ }
16+
17+ const hasModules30 = upgrader . hasModules30 ( projectDir ) ;
18+ if ( ! hasModules30 ) {
19+ createReferenceFile ( ) ;
20+ }
21+
22+ installTypescript ( hasModules30 ) ;
2323}
2424
2525function createReferenceFile ( ) {
26- var referenceFilePath = path . join ( projectDir , "references.d.ts" ) ,
27- content = "/// <reference path=\"./node_modules/tns-core-modules/tns-core-modules.d.ts\" /> Needed for autocompletion and compilation." ;
26+ var referenceFilePath = path . join ( projectDir , "references.d.ts" ) ,
27+ content = "/// <reference path=\"./node_modules/tns-core-modules/tns-core-modules.d.ts\" /> Needed for autocompletion and compilation." ;
2828
29- if ( ! fs . existsSync ( referenceFilePath ) ) {
30- fs . appendFileSync ( referenceFilePath , content ) ;
31- }
29+ if ( ! fs . existsSync ( referenceFilePath ) ) {
30+ fs . appendFileSync ( referenceFilePath , content ) ;
31+ }
3232}
3333
3434function createTsconfig ( tsconfigPath ) {
35- var tsconfig = { } ;
35+ var tsconfig = { } ;
3636
37- tsconfig . compilerOptions = {
38- module : "commonjs" ,
39- target : "es5" ,
40- experimentalDecorators : true ,
41- emitDecoratorMetadata : true ,
42- noEmitHelpers : true ,
43- noEmitOnError : true ,
44- } ;
45- upgrader . migrateProject ( tsconfig , tsconfigPath , projectDir ) ;
37+ tsconfig . compilerOptions = {
38+ module : "commonjs" ,
39+ target : "es5" ,
40+ experimentalDecorators : true ,
41+ emitDecoratorMetadata : true ,
42+ noEmitHelpers : true ,
43+ noEmitOnError : true ,
44+ } ;
45+ upgrader . migrateProject ( tsconfig , tsconfigPath , projectDir ) ;
4646
47- tsconfig . exclude = [ "node_modules" , "platforms" , "**/*.aot.ts" ] ;
47+ tsconfig . exclude = [ "node_modules" , "platforms" , "**/*.aot.ts" ] ;
4848
49- fs . writeFileSync ( tsconfigPath , JSON . stringify ( tsconfig , null , 4 ) ) ;
49+ fs . writeFileSync ( tsconfigPath , JSON . stringify ( tsconfig , null , 4 ) ) ;
5050}
5151
5252function getProjectTypeScriptVersion ( ) {
53- try {
54- var packageJsonPath = path . join ( projectDir , "package.json" ) ,
55- packageJsonContent = fs . readFileSync ( packageJsonPath , "utf8" ) ,
56- jsonContent = JSON . parse ( packageJsonContent ) ;
53+ try {
54+ var packageJsonPath = path . join ( projectDir , "package.json" ) ,
55+ packageJsonContent = fs . readFileSync ( packageJsonPath , "utf8" ) ,
56+ jsonContent = JSON . parse ( packageJsonContent ) ;
5757
58- return ( jsonContent . dependencies && jsonContent . dependencies . typescript ) ||
58+ return ( jsonContent . dependencies && jsonContent . dependencies . typescript ) ||
5959 ( jsonContent . devDependencies && jsonContent . devDependencies . typescript ) ;
60- } catch ( err ) {
61- console . error ( err ) ;
62- return null ;
63- }
60+ } catch ( err ) {
61+ console . error ( err ) ;
62+ return null ;
63+ }
6464}
6565
66- function installTypescript ( { force = false } = { } ) {
67- const installedTypeScriptVersion = getProjectTypeScriptVersion ( ) ;
66+ function installTypescript ( hasModules30 ) {
67+ const installedTypeScriptVersion = getProjectTypeScriptVersion ( ) ;
68+ const force = shouldInstallLatest ( installedTypeScriptVersion , hasModules30 ) ;
6869
69- if ( installedTypeScriptVersion && ! force ) {
70- console . log ( "Project already targets TypeScript " + installedTypeScriptVersion ) ;
71- } else {
72- const command = force ? "npm install -D typescript@latest" : "npm update -D typescript" ;
70+ if ( installedTypeScriptVersion && ! force ) {
71+ console . log ( `Project already targets TypeScript ${ installedTypeScriptVersion } ` ) ;
72+ } else {
73+ const command = force ?
74+ "npm install -D typescript@latest" :
75+ "npm install -D -E typescript@2.1.6" ; // install exactly 2.1.6
7376
7477 console . log ( "Installing TypeScript..." ) ;
7578
76- require ( "child_process" ) . exec ( command , { cwd : projectDir } , function ( err , stdout , stderr ) {
77- if ( err ) {
78- console . warn ( "npm: " + err . toString ( ) ) ;
79- }
80- process . stdout . write ( stdout ) ;
81- process . stderr . write ( stderr ) ;
82- } ) ;
83- }
79+ require ( "child_process" ) . exec ( command , { cwd : projectDir } , ( err , stdout , stderr ) => {
80+ if ( err ) {
81+ console . warn ( `npm: ${ err . toString ( ) } ` ) ;
82+ }
83+
84+ process . stdout . write ( stdout ) ;
85+ process . stderr . write ( stderr ) ;
86+ } ) ;
87+ }
88+ }
89+
90+ function shouldInstallLatest ( tsVersion , hasModules30 ) {
91+ if ( ! hasModules30 ) {
92+ return false ;
93+ }
94+
95+ const justVersion = clearPatch ( tsVersion ) ;
96+ return ! tsVersion ||
97+ tsVersion === "2.2.0" ||
98+ justVersion [ 0 ] < 2 || // ex. 1.8.10
99+ justVersion [ 2 ] < 2 ; // ex. 2.1.6
100+ }
101+
102+ function clearPatch ( version ) {
103+ return version && ( version . startsWith ( "~" ) || version . startsWith ( "^" ) ) ?
104+ version . substring ( 1 ) :
105+ version ;
84106}
0 commit comments