Skip to content

Commit bcf6160

Browse files
committed
feat: lookup project tsconfig
This looks up the default tsconfig.json file for the current project when used from the CLI, to avoid possible compliation errors. Fixes vega#2062
1 parent 46f638e commit bcf6160

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Note that different platforms (e.g. Windows) may use different path separators s
3535

3636
Also note that you need to quote paths with `*` as otherwise the shell will expand the paths and therefore only pass the first path to the generator.
3737

38+
By default, the command-line generator will use the `tsconfig.json` file in the current working directory, or the first parent directory that contains a `tsconfig.json` file up to the root of the filesystem. If you want to use a different `tsconfig.json` file, you can use the `--tsconfig` option. In particular, if you need to use different compilation options for types, you may want to create a separate `tsconfig.json` file for the schema generation only.
39+
3840
### Options
3941

4042
```

src/Utils/findTsConfig.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
/**
5+
* Find the tsconfig.json file in the current working directory or any parent directory.
6+
* This is the behavior from tsc: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
7+
*
8+
* @param directory - The directory to start searching from. Defaults to the current working directory.
9+
* @param filename - The filename of the tsconfig.json file. Defaults to "tsconfig.json".
10+
* @returns The path to the tsconfig.json file or undefined if no such file was found.
11+
*/
12+
export function findTsConfig(directory: string = process.cwd(), filename = "tsconfig.json"): string | undefined {
13+
const tsConfigPath = path.join(directory, filename);
14+
if (fs.existsSync(tsConfigPath)) {
15+
return tsConfigPath;
16+
}
17+
const parentDir = path.dirname(directory);
18+
if (parentDir === directory) {
19+
return undefined;
20+
}
21+
return findTsConfig(parentDir, filename);
22+
}

test/unit/findTsConfig.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import path from "path";
2+
import { findTsConfig } from "../../src/Utils/findTsConfig.js";
3+
4+
describe("findTsConfig", () => {
5+
it("returns the path to the tsconfig.json file", () => {
6+
const TS_CONFIG_FILE = path.resolve(__dirname, "../../tsconfig.json");
7+
const foundTsConfig = findTsConfig();
8+
expect(foundTsConfig).toBeDefined();
9+
expect(path.resolve(foundTsConfig as string)).toEqual(TS_CONFIG_FILE);
10+
});
11+
});

ts-json-schema-generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import stableStringify from "safe-stable-stringify";
55
import { createGenerator } from "./factory/generator.js";
66
import type { Config } from "./src/Config.js";
77
import { BaseError } from "./src/Error/BaseError.js";
8+
import { findTsConfig } from "./src/Utils/findTsConfig.js";
89

910
import pkg from "./package.json";
1011

@@ -56,7 +57,7 @@ const args = new Command()
5657
const config: Config = {
5758
minify: args.minify,
5859
path: args.path,
59-
tsconfig: args.tsconfig,
60+
tsconfig: args.tsconfig ?? findTsConfig(),
6061
type: args.type,
6162
schemaId: args.id,
6263
expose: args.expose,

0 commit comments

Comments
 (0)