@@ -39,57 +39,25 @@ export function getConfigFileName(baseDir: string, configFileName: string): stri
39
39
}
40
40
41
41
export function readFilesFromTsconfig ( configPath : string ) : string [ ] {
42
+ return readTsconfig ( configPath ) . fileNames ;
43
+ }
42
44
43
- interface TsConfigJSON {
44
- files ?: string [ ] ;
45
- include ?: string [ ] ;
46
- exclude ?: string [ ] ;
47
- }
48
-
49
- let tsconfigDir = path . dirname ( configPath ) ;
50
- let tsconfig : TsConfigJSON = parseJSON ( fs . readFileSync ( configPath , "utf-8" ) ) ;
51
- if ( tsconfig . files && ( tsconfig . include || tsconfig . exclude ) ) {
52
- return tsconfig . files . concat ( tsMatchFiles ( tsconfig . exclude || [ ] , tsconfig . include || [ ] ) ) ;
53
- } else if ( tsconfig . files ) {
54
- let files : string [ ] = tsconfig . files ;
55
- return files . map ( filePath => path . resolve ( tsconfigDir , filePath ) ) ;
56
- } else if ( tsconfig . include || tsconfig . exclude ) {
57
- return tsMatchFiles ( tsconfig . exclude || [ ] , tsconfig . include || [ ] ) ;
58
- } else {
59
- return tsMatchFiles ( [ ] , [ ] ) ;
45
+ export function readTsconfig ( configPath : string ) : ts . ParsedCommandLine {
46
+ // for `extends` support. It supported from TypeScript 2.1.1.
47
+ // `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
48
+ const host : ts . ParseConfigHost & { readFile ( path : string ) : string ; } = {
49
+ useCaseSensitiveFileNames : ts . sys . useCaseSensitiveFileNames ,
50
+ readDirectory : ts . sys . readDirectory ,
51
+ fileExists : path => fs . existsSync ( path ) ,
52
+ readFile : ( path : string ) => fs . readFileSync ( path , "utf-8" ) ,
53
+ } ;
54
+ let rootConfig = parseJSON ( fs . readFileSync ( configPath , "utf-8" ) ) ;
55
+ let parsed = ts . parseJsonConfigFileContent ( rootConfig , host , path . dirname ( configPath ) ) ;
56
+ if ( parsed . errors && parsed . errors . length !== 0 ) {
57
+ throw new Error ( parsed . errors . map ( e => e . messageText ) . join ( "\n" ) ) ;
60
58
}
61
59
62
- function tsMatchFiles ( excludes : string [ ] , includes : string [ ] ) {
63
- interface TsMatchFiles {
64
- ( path : string , extensions : string [ ] , excludes : string [ ] , includes : string [ ] , useCaseSensitiveFileNames : boolean , currentDirectory : string , getFileSystemEntries : ( path : string ) => TsFileSystemEntries ) : string [ ] ;
65
- }
66
- interface TsFileSystemEntries {
67
- files : string [ ] ;
68
- directories : string [ ] ;
69
- }
70
-
71
- let f : TsMatchFiles = ( ts as any ) . matchFiles ;
72
- if ( ! f ) {
73
- throw new Error ( "ts.matchFiles is not exists. typescript@^2.0.0 required" ) ;
74
- }
75
- return f ( tsconfigDir , [ ".ts" , ".tsx" ] , excludes , includes , true , tsconfigDir , dirPath => {
76
- let stat = fs . statSync ( dirPath ) ;
77
- if ( stat . isDirectory ( ) ) {
78
- let result : TsFileSystemEntries = { files : [ ] , directories : [ ] } ;
79
- let dirEntries = fs . readdirSync ( dirPath ) ;
80
- dirEntries . forEach ( entry => {
81
- let stat = fs . statSync ( path . join ( dirPath , entry ) ) ;
82
- if ( stat . isDirectory ( ) ) {
83
- result . directories . push ( entry ) ;
84
- } else if ( stat . isFile ( ) ) {
85
- result . files . push ( entry ) ;
86
- }
87
- } ) ;
88
- return result ;
89
- }
90
- return { files : [ ] , directories : [ ] } ;
91
- } ) ;
92
- }
60
+ return parsed ;
93
61
}
94
62
95
63
export function parseJSON ( jsonText : string ) : any {
0 commit comments