From 83e31c50ecbd2d6152490862819641c32c90bfcd Mon Sep 17 00:00:00 2001 From: Werner Robitza Date: Mon, 30 Sep 2024 14:49:34 +0200 Subject: [PATCH] feat: lookup project tsconfig (#2068) --- README.md | 2 ++ ts-json-schema-generator.ts | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bb7191de8..267019dc2 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ Note that different platforms (e.g. Windows) may use different path separators s 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. +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. + ### Options ``` diff --git a/ts-json-schema-generator.ts b/ts-json-schema-generator.ts index 953929041..52181cdec 100644 --- a/ts-json-schema-generator.ts +++ b/ts-json-schema-generator.ts @@ -2,6 +2,7 @@ import { Command, Option } from "commander"; import { mkdirSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; import stableStringify from "safe-stable-stringify"; +import { findConfigFile, sys as tsSys } from "typescript"; import { createGenerator } from "./factory/generator.js"; import type { Config } from "./src/Config.js"; import { BaseError } from "./src/Error/BaseError.js"; @@ -56,7 +57,8 @@ const args = new Command() const config: Config = { minify: args.minify, path: args.path, - tsconfig: args.tsconfig, + tsconfig: + typeof args.tsconfig === "string" ? args.tsconfig : findConfigFile(process.cwd(), (f) => tsSys.fileExists(f)), type: args.type, schemaId: args.id, expose: args.expose,