@@ -5,37 +5,104 @@ var path = require("path");
55
66var arg1 = process . argv . length > 2 ? process . argv [ 2 ] : "" ;
77
8+ var isTranspile = arg1 . indexOf ( "t" ) >= 0 ;
89var isIncremental = arg1 . indexOf ( "i" ) >= 0 ;
10+ var isWatching = arg1 . indexOf ( "w" ) >= 0 ;
11+
12+ var opts = [ ] ;
13+
14+ if ( isTranspile ) {
15+ opts . push ( "transpile" ) ;
16+ }
17+
918if ( isIncremental ) {
10- console . log ( "incremental" ) ;
19+ opts . push ( "incremental" ) ;
20+ }
21+
22+ if ( isWatching ) {
23+ opts . push ( "watch" ) ;
24+ }
25+
26+ if ( opts . length > 0 ) {
27+ console . log ( "Options: " + opts . join ( ", " ) ) ;
28+ }
29+
30+ function isTS ( file : string ) : boolean {
31+ return file . lastIndexOf ( ".ts" ) === file . length - 3 ;
32+ }
33+
34+ function isDTS ( file : string ) : boolean {
35+ return file . lastIndexOf ( ".d.ts" ) === file . length - 5 ;
36+ }
37+
38+ function getJsPath ( tsPath : string ) : string {
39+ return path . join ( path . dirname ( tsPath ) , path . basename ( tsPath , ".ts" ) ) + ".js" ;
40+ }
41+
42+ function hasChanged ( tsName : string ) : boolean {
43+ try {
44+ var jsName = getJsPath ( tsName ) ;
45+
46+ var tsTime = fs . statSync ( tsName ) . mtime . getTime ( ) ;
47+ var jsTime = fs . statSync ( jsName ) . mtime . getTime ( ) ;
48+
49+ return jsTime < tsTime ;
50+ } catch ( e ) {
51+ return true ;
52+ }
53+ }
54+
55+ function transpile ( fileNames : string [ ] , options : ts . CompilerOptions ) {
56+ console . time ( "transpile" ) ;
57+ var files = fileNames . filter ( f => ! isDTS ( f ) ) ;
58+ if ( isIncremental ) {
59+ files = files . filter ( hasChanged ) ;
60+ }
61+ files . forEach ( tsPath => {
62+ var tsSource = fs . readFileSync ( tsPath , { encoding : "utf8" } ) ;
63+ var jsSource = ts . transpile ( tsSource , options ) ;
64+ var jsPath = getJsPath ( tsPath ) ;
65+ fs . writeFileSync ( jsPath , jsSource , { flag : "w" } , function ( err ) { console . log ( err ) ; } ) ;
66+ if ( isIncremental ) {
67+ console . log ( " - " + tsPath ) ;
68+ }
69+ } ) ;
70+ console . timeEnd ( "transpile" ) ;
71+
72+ if ( isWatching ) {
73+ console . log ( "Watching for changes..." ) ;
74+ fs . watch ( "." , { persistent : true , recursive : true , encoding : "utf8" } , ( event , file ) => {
75+ try {
76+ if ( isTS ( file ) && ! isDTS ( file ) ) {
77+ var tsPath = file ;
78+ var label = " - " + tsPath ;
79+ console . time ( label ) ;
80+ var tsSource = fs . readFileSync ( tsPath , { encoding : "utf8" } ) ;
81+ var jsSource = ts . transpile ( tsSource , options ) ;
82+ var jsPath = getJsPath ( tsPath ) ;
83+ fs . writeFileSync ( jsPath , jsSource , { flag : "w" } , function ( err ) { console . log ( err ) ; } ) ;
84+ console . timeEnd ( label ) ;
85+ }
86+ } catch ( e ) {
87+ // console.log(e);
88+ }
89+ } ) ;
90+ }
1191}
1292
1393function compile ( fileNames : string [ ] , options : ts . CompilerOptions ) {
1494 console . time ( "program" ) ;
1595 var program = ts . createProgram ( fileNames , options ) ;
1696
1797 console . timeEnd ( "program" ) ;
18- var sourceFiles = program . getSourceFiles ( ) . filter ( f => f . fileName . lastIndexOf ( ".d.ts" ) !== f . fileName . length - 5 ) ;
98+ var sourceFiles = program . getSourceFiles ( ) . filter ( f => ! isDTS ( f . fileName ) ) ;
1999
20100 var emitResults = [ ] ;
21101 var allDiagnostics = [ ] ;
22102
23103 console . time ( "transpile" ) ;
24104 if ( isIncremental ) {
25- sourceFiles = sourceFiles . filter ( srcFile => {
26- try {
27- var tsName = srcFile . fileName ;
28- var jsName = path . join ( path . dirname ( tsName ) , path . basename ( tsName , ".ts" ) ) + ".js" ;
29-
30- var tsTime = fs . statSync ( tsName ) . mtime . getTime ( ) ;
31- var jsTime = fs . statSync ( jsName ) . mtime . getTime ( ) ;
32-
33- return jsTime < tsTime ;
34- } catch ( e ) {
35- return true ;
36- }
37- } ) ;
38-
105+ sourceFiles = sourceFiles . filter ( srcFile => hasChanged ( srcFile . fileName ) ) ;
39106 sourceFiles . forEach ( srcFile => {
40107 console . log ( " - " + srcFile . fileName ) ;
41108 emitResults . push ( program . emit ( srcFile ) ) ;
@@ -66,8 +133,7 @@ function compile(fileNames: string[], options: ts.CompilerOptions) {
66133}
67134
68135var files = JSON . parse ( fs . readFileSync ( "./tsconfig.json" ) ) . files ;
69- compile ( files ,
70- {
136+ var options : ts . CompilerOptions = {
71137 noEmitOnError : true ,
72138 noEmitHelpers : true ,
73139 target : ts . ScriptTarget . ES5 ,
@@ -76,5 +142,9 @@ compile(files,
76142 noImplicitAny : false ,
77143 noImplicitUseStrict : true ,
78144 experimentalDecorators : true
79- } ) ;
80-
145+ } ;
146+ if ( isTranspile ) {
147+ transpile ( files , { module : ts . ModuleKind . CommonJS } ) ;
148+ } else {
149+ compile ( files , options ) ;
150+ }
0 commit comments