Skip to content

Commit 4c4acd5

Browse files
committed
JS: Factor out loading of tsconfig files
1 parent cc3e62f commit 4c4acd5

File tree

1 file changed

+23
-7
lines changed
  • javascript/extractor/lib/typescript/src

1 file changed

+23
-7
lines changed

javascript/extractor/lib/typescript/src/main.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ interface ParseCommand {
4848
command: "parse";
4949
filename: string;
5050
}
51-
interface OpenProjectCommand {
52-
command: "open-project";
51+
interface LoadCommand {
5352
tsConfig: string;
5453
sourceRoot: string | null;
5554
virtualSourceRoot: string | null;
5655
packageEntryPoints: [string, string][];
5756
packageJsonFiles: [string, string][];
5857
}
58+
interface OpenProjectCommand extends LoadCommand {
59+
command: "open-project";
60+
}
5961
interface CloseProjectCommand {
6062
command: "close-project";
6163
tsConfig: string;
@@ -366,10 +368,17 @@ function parseSingleFile(filename: string): {ast: ts.SourceFile, code: string} {
366368
*/
367369
const nodeModulesRex = /[/\\]node_modules[/\\]((?:@[\w.-]+[/\\])?\w[\w.-]*)[/\\](.*)/;
368370

369-
function handleOpenProjectCommand(command: OpenProjectCommand) {
370-
let tsConfigFilename = String(command.tsConfig);
371-
let tsConfig = ts.readConfigFile(tsConfigFilename, ts.sys.readFile);
372-
let basePath = pathlib.dirname(tsConfigFilename);
371+
interface LoadedConfig {
372+
config: ts.ParsedCommandLine;
373+
basePath: string;
374+
packageEntryPoints: Map<string, string>;
375+
packageJsonFiles: Map<string, string>;
376+
virtualSourceRoot: VirtualSourceRoot;
377+
}
378+
379+
function loadTsConfig(command: LoadCommand): LoadedConfig {
380+
let tsConfig = ts.readConfigFile(command.tsConfig, ts.sys.readFile);
381+
let basePath = pathlib.dirname(command.tsConfig);
373382

374383
let packageEntryPoints = new Map(command.packageEntryPoints);
375384
let packageJsonFiles = new Map(command.packageJsonFiles);
@@ -418,7 +427,14 @@ function handleOpenProjectCommand(command: OpenProjectCommand) {
418427
}
419428
};
420429
let config = ts.parseJsonConfigFileContent(tsConfig.config, parseConfigHost, basePath);
421-
let project = new Project(tsConfigFilename, config, state.typeTable, packageEntryPoints, virtualSourceRoot);
430+
431+
return { config, basePath, packageJsonFiles, packageEntryPoints, virtualSourceRoot };
432+
}
433+
434+
function handleOpenProjectCommand(command: OpenProjectCommand) {
435+
let { config, packageEntryPoints, virtualSourceRoot, basePath } = loadTsConfig(command);
436+
437+
let project = new Project(command.tsConfig, config, state.typeTable, packageEntryPoints, virtualSourceRoot);
422438
project.load();
423439

424440
state.project = project;

0 commit comments

Comments
 (0)