@@ -41,17 +41,26 @@ import { Project } from "./common";
41
41
import { TypeTable } from "./type_table" ;
42
42
import { VirtualSourceRoot } from "./virtual_source_root" ;
43
43
44
+ // Remove limit on stack trace depth.
45
+ Error . stackTraceLimit = Infinity ;
46
+
44
47
interface ParseCommand {
45
48
command : "parse" ;
46
49
filename : string ;
47
50
}
48
- interface OpenProjectCommand {
49
- command : "open-project" ;
51
+ interface LoadCommand {
50
52
tsConfig : string ;
53
+ sourceRoot : string | null ;
51
54
virtualSourceRoot : string | null ;
52
55
packageEntryPoints : [ string , string ] [ ] ;
53
56
packageJsonFiles : [ string , string ] [ ] ;
54
57
}
58
+ interface OpenProjectCommand extends LoadCommand {
59
+ command : "open-project" ;
60
+ }
61
+ interface GetOwnFilesCommand extends LoadCommand {
62
+ command : "get-own-files" ;
63
+ }
55
64
interface CloseProjectCommand {
56
65
command : "close-project" ;
57
66
tsConfig : string ;
@@ -72,7 +81,7 @@ interface PrepareFilesCommand {
72
81
interface GetMetadataCommand {
73
82
command : "get-metadata" ;
74
83
}
75
- type Command = ParseCommand | OpenProjectCommand | CloseProjectCommand
84
+ type Command = ParseCommand | OpenProjectCommand | GetOwnFilesCommand | CloseProjectCommand
76
85
| GetTypeTableCommand | ResetCommand | QuitCommand | PrepareFilesCommand | GetMetadataCommand ;
77
86
78
87
/** The state to be shared between commands. */
@@ -362,15 +371,22 @@ function parseSingleFile(filename: string): {ast: ts.SourceFile, code: string} {
362
371
*/
363
372
const nodeModulesRex = / [ / \\ ] n o d e _ m o d u l e s [ / \\ ] ( (?: @ [ \w . - ] + [ / \\ ] ) ? \w [ \w . - ] * ) [ / \\ ] ( .* ) / ;
364
373
365
- function handleOpenProjectCommand ( command : OpenProjectCommand ) {
366
- Error . stackTraceLimit = Infinity ;
367
- let tsConfigFilename = String ( command . tsConfig ) ;
368
- let tsConfig = ts . readConfigFile ( tsConfigFilename , ts . sys . readFile ) ;
369
- let basePath = pathlib . dirname ( tsConfigFilename ) ;
374
+ interface LoadedConfig {
375
+ config : ts . ParsedCommandLine ;
376
+ basePath : string ;
377
+ packageEntryPoints : Map < string , string > ;
378
+ packageJsonFiles : Map < string , string > ;
379
+ virtualSourceRoot : VirtualSourceRoot ;
380
+ ownFiles : string [ ] ;
381
+ }
382
+
383
+ function loadTsConfig ( command : LoadCommand ) : LoadedConfig {
384
+ let tsConfig = ts . readConfigFile ( command . tsConfig , ts . sys . readFile ) ;
385
+ let basePath = pathlib . dirname ( command . tsConfig ) ;
370
386
371
387
let packageEntryPoints = new Map ( command . packageEntryPoints ) ;
372
388
let packageJsonFiles = new Map ( command . packageJsonFiles ) ;
373
- let virtualSourceRoot = new VirtualSourceRoot ( process . cwd ( ) , command . virtualSourceRoot ) ;
389
+ let virtualSourceRoot = new VirtualSourceRoot ( command . sourceRoot , command . virtualSourceRoot ) ;
374
390
375
391
/**
376
392
* Rewrites path segments of form `node_modules/PACK/suffix` to be relative to
@@ -415,7 +431,29 @@ function handleOpenProjectCommand(command: OpenProjectCommand) {
415
431
}
416
432
} ;
417
433
let config = ts . parseJsonConfigFileContent ( tsConfig . config , parseConfigHost , basePath ) ;
418
- let project = new Project ( tsConfigFilename , config , state . typeTable , packageEntryPoints , virtualSourceRoot ) ;
434
+
435
+ let ownFiles = config . fileNames . map ( file => pathlib . resolve ( file ) ) ;
436
+
437
+ return { config, basePath, packageJsonFiles, packageEntryPoints, virtualSourceRoot, ownFiles } ;
438
+ }
439
+
440
+ /**
441
+ * Returns the list of files included in the given tsconfig.json file's include pattern,
442
+ * (not including those only references through imports).
443
+ */
444
+ function handleGetFileListCommand ( command : GetOwnFilesCommand ) {
445
+ let { config, ownFiles } = loadTsConfig ( command ) ;
446
+
447
+ console . log ( JSON . stringify ( {
448
+ type : "file-list" ,
449
+ ownFiles,
450
+ } ) ) ;
451
+ }
452
+
453
+ function handleOpenProjectCommand ( command : OpenProjectCommand ) {
454
+ let { config, packageEntryPoints, virtualSourceRoot, basePath, ownFiles } = loadTsConfig ( command ) ;
455
+
456
+ let project = new Project ( command . tsConfig , config , state . typeTable , packageEntryPoints , virtualSourceRoot ) ;
419
457
project . load ( ) ;
420
458
421
459
state . project = project ;
@@ -587,9 +625,14 @@ function handleOpenProjectCommand(command: OpenProjectCommand) {
587
625
return symbol ;
588
626
}
589
627
628
+ // Unlike in the get-own-files command, this command gets all files we can possibly
629
+ // extract type information for, including files referenced outside the tsconfig's inclusion pattern.
630
+ let allFiles = program . getSourceFiles ( ) . map ( sf => pathlib . resolve ( sf . fileName ) ) ;
631
+
590
632
console . log ( JSON . stringify ( {
591
633
type : "project-opened" ,
592
- files : program . getSourceFiles ( ) . map ( sf => pathlib . resolve ( sf . fileName ) ) ,
634
+ ownFiles,
635
+ allFiles,
593
636
} ) ) ;
594
637
}
595
638
@@ -685,6 +728,9 @@ function runReadLineInterface() {
685
728
case "open-project" :
686
729
handleOpenProjectCommand ( req ) ;
687
730
break ;
731
+ case "get-own-files" :
732
+ handleGetFileListCommand ( req ) ;
733
+ break ;
688
734
case "close-project" :
689
735
handleCloseProjectCommand ( req ) ;
690
736
break ;
@@ -720,6 +766,7 @@ if (process.argv.length > 2) {
720
766
tsConfig : argument ,
721
767
packageEntryPoints : [ ] ,
722
768
packageJsonFiles : [ ] ,
769
+ sourceRoot : null ,
723
770
virtualSourceRoot : null ,
724
771
} ) ;
725
772
for ( let sf of state . project . program . getSourceFiles ( ) ) {
0 commit comments