Skip to content

Commit ac777fc

Browse files
author
Toke Voltelen
committed
Optimize tsconfig.json file finding
1 parent 3c80f10 commit ac777fc

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/lib/utils/options/readers/tsconfig.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,33 @@ export class TSConfigReader extends OptionsComponent {
3737
onDiscover(event: DiscoverEvent) {
3838
// Do nothing until were fetching options
3939
if (event.mode !== OptionsReadMode.Fetch) {
40-
return;
40+
return;
4141
}
4242

43+
let file: string;
44+
4345
if (TSConfigReader.OPTIONS_KEY in event.data) {
44-
this.load(event, Path.resolve(event.data[TSConfigReader.OPTIONS_KEY]));
46+
const tsconfig = event.data[TSConfigReader.OPTIONS_KEY];
47+
48+
file = /tsconfig\.json$/.test(tsconfig)
49+
? Path.resolve(tsconfig)
50+
: ts.findConfigFile(tsconfig, ts.sys.fileExists);
51+
52+
if (!file || !FS.existsSync(file)) {
53+
event.addError('The tsconfig file %s does not exist.', file);
54+
return;
55+
}
4556
} else if (TSConfigReader.PROJECT_KEY in event.data) {
46-
// The `project` option may be a directory or file, so use TS to find it
47-
let file: string = ts.findConfigFile(event.data[TSConfigReader.PROJECT_KEY], ts.sys.fileExists);
48-
// If file is undefined, we found no file to load.
49-
if (file) {
50-
this.load(event, file);
51-
}
57+
// The `project` option may be a directory or file, so use TS to find it
58+
file = ts.findConfigFile(event.data[TSConfigReader.PROJECT_KEY], ts.sys.fileExists);
5259
} else if (this.application.isCLI) {
53-
let file: string = ts.findConfigFile('.', ts.sys.fileExists);
54-
// If file is undefined, we found no file to load.
55-
if (file) {
56-
this.load(event, file);
57-
}
60+
// No file or directory has been specified so find the file in the root of the project
61+
file = ts.findConfigFile('.', ts.sys.fileExists);
62+
}
63+
64+
// If file is undefined, we found no file to load.
65+
if (file) {
66+
this.load(event, file);
5867
}
5968
}
6069

@@ -65,11 +74,6 @@ export class TSConfigReader extends OptionsComponent {
6574
* @param fileName The absolute path and file name of the tsconfig file.
6675
*/
6776
load(event: DiscoverEvent, fileName: string) {
68-
if (!FS.existsSync(fileName)) {
69-
event.addError('The tsconfig file %s does not exist.', fileName);
70-
return;
71-
}
72-
7377
const { config } = ts.readConfigFile(fileName, ts.sys.readFile);
7478
if (config === undefined) {
7579
event.addError('The tsconfig file %s does not contain valid JSON.', fileName);

0 commit comments

Comments
 (0)