@@ -7,78 +7,100 @@ var upgrader = require("./tsconfig-upgrader");
7
7
8
8
var projectDir = hook . findProjectDir ( ) ;
9
9
if ( 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 ) ;
23
23
}
24
24
25
25
function 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." ;
28
28
29
- if ( ! fs . existsSync ( referenceFilePath ) ) {
30
- fs . appendFileSync ( referenceFilePath , content ) ;
31
- }
29
+ if ( ! fs . existsSync ( referenceFilePath ) ) {
30
+ fs . appendFileSync ( referenceFilePath , content ) ;
31
+ }
32
32
}
33
33
34
34
function createTsconfig ( tsconfigPath ) {
35
- var tsconfig = { } ;
35
+ var tsconfig = { } ;
36
36
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 ) ;
46
46
47
- tsconfig . exclude = [ "node_modules" , "platforms" , "**/*.aot.ts" ] ;
47
+ tsconfig . exclude = [ "node_modules" , "platforms" , "**/*.aot.ts" ] ;
48
48
49
- fs . writeFileSync ( tsconfigPath , JSON . stringify ( tsconfig , null , 4 ) ) ;
49
+ fs . writeFileSync ( tsconfigPath , JSON . stringify ( tsconfig , null , 4 ) ) ;
50
50
}
51
51
52
52
function 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 ) ;
57
57
58
- return ( jsonContent . dependencies && jsonContent . dependencies . typescript ) ||
58
+ return ( jsonContent . dependencies && jsonContent . dependencies . typescript ) ||
59
59
( 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
+ }
64
64
}
65
65
66
- function installTypescript ( { force = false } = { } ) {
67
- const installedTypeScriptVersion = getProjectTypeScriptVersion ( ) ;
66
+ function installTypescript ( hasModules30 ) {
67
+ const installedTypeScriptVersion = getProjectTypeScriptVersion ( ) ;
68
+ const force = shouldInstallLatest ( installedTypeScriptVersion , hasModules30 ) ;
68
69
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
73
76
74
77
console . log ( "Installing TypeScript..." ) ;
75
78
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 ;
84
106
}
0 commit comments