1515import java .util .ArrayList ;
1616import java .util .Arrays ;
1717import java .util .Collections ;
18+ import java .util .LinkedHashSet ;
1819import java .util .List ;
1920import java .util .Map ;
21+ import java .util .Set ;
2022import java .util .concurrent .TimeUnit ;
2123
2224import com .google .gson .JsonArray ;
@@ -488,6 +490,29 @@ private JsonArray mapToArray(Map<String, Path> map) {
488490 return result ;
489491 }
490492
493+ private static Set <File > getFilesFromJsonArray (JsonArray array ) {
494+ Set <File > files = new LinkedHashSet <>();
495+ for (JsonElement elm : array ) {
496+ files .add (new File (elm .getAsString ()));
497+ }
498+ return files ;
499+ }
500+
501+ /**
502+ * Returns the set of files included by the inclusion pattern in the given tsconfig.json file.
503+ */
504+ public Set <File > getOwnFiles (File tsConfigFile , DependencyInstallationResult deps ) {
505+ JsonObject request = makeLoadCommand ("get-own-files" , tsConfigFile , deps );
506+ JsonObject response = talkToParserWrapper (request );
507+ try {
508+ checkResponseType (response , "file-list" );
509+ return getFilesFromJsonArray (response .get ("ownFiles" ).getAsJsonArray ());
510+ } catch (IllegalStateException e ) {
511+ throw new CatastrophicError (
512+ "TypeScript parser wrapper sent unexpected response: " + response , e );
513+ }
514+ }
515+
491516 /**
492517 * Opens a new project based on a tsconfig.json file. The compiler will analyze all files in the
493518 * project.
@@ -497,8 +522,23 @@ private JsonArray mapToArray(Map<String, Path> map) {
497522 * <p>Only one project should be opened at once.
498523 */
499524 public ParsedProject openProject (File tsConfigFile , DependencyInstallationResult deps ) {
525+ JsonObject request = makeLoadCommand ("open-project" , tsConfigFile , deps );
526+ JsonObject response = talkToParserWrapper (request );
527+ try {
528+ checkResponseType (response , "project-opened" );
529+ ParsedProject project = new ParsedProject (tsConfigFile ,
530+ getFilesFromJsonArray (response .get ("ownFiles" ).getAsJsonArray ()),
531+ getFilesFromJsonArray (response .get ("allFiles" ).getAsJsonArray ()));
532+ return project ;
533+ } catch (IllegalStateException e ) {
534+ throw new CatastrophicError (
535+ "TypeScript parser wrapper sent unexpected response: " + response , e );
536+ }
537+ }
538+
539+ private JsonObject makeLoadCommand (String command , File tsConfigFile , DependencyInstallationResult deps ) {
500540 JsonObject request = new JsonObject ();
501- request .add ("command" , new JsonPrimitive ("open-project" ));
541+ request .add ("command" , new JsonPrimitive (command ));
502542 request .add ("tsConfig" , new JsonPrimitive (tsConfigFile .getPath ()));
503543 request .add ("packageEntryPoints" , mapToArray (deps .getPackageEntryPoints ()));
504544 request .add ("packageJsonFiles" , mapToArray (deps .getPackageJsonFiles ()));
@@ -508,19 +548,7 @@ public ParsedProject openProject(File tsConfigFile, DependencyInstallationResult
508548 request .add ("virtualSourceRoot" , deps .getVirtualSourceRoot () == null
509549 ? JsonNull .INSTANCE
510550 : new JsonPrimitive (deps .getVirtualSourceRoot ().toString ()));
511- JsonObject response = talkToParserWrapper (request );
512- try {
513- checkResponseType (response , "project-opened" );
514- ParsedProject project = new ParsedProject (tsConfigFile );
515- JsonArray filesJson = response .get ("files" ).getAsJsonArray ();
516- for (JsonElement elm : filesJson ) {
517- project .addSourceFile (new File (elm .getAsString ()));
518- }
519- return project ;
520- } catch (IllegalStateException e ) {
521- throw new CatastrophicError (
522- "TypeScript parser wrapper sent unexpected response: " + response , e );
523- }
551+ return request ;
524552 }
525553
526554 /**
0 commit comments