-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This looks up the default tsconfig.json file for the current project when used from the CLI, to avoid possible compliation errors. Fixes #2062
- Loading branch information
Showing
4 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
/** | ||
* Find the tsconfig.json file in the current working directory or any parent directory. | ||
* This is the behavior from tsc: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html | ||
* | ||
* @param directory - The directory to start searching from. Defaults to the current working directory. | ||
* @param filename - The filename of the tsconfig.json file. Defaults to "tsconfig.json". | ||
* @returns The path to the tsconfig.json file or undefined if no such file was found. | ||
*/ | ||
export function findTsConfig(directory: string = process.cwd(), filename = "tsconfig.json"): string | undefined { | ||
const tsConfigPath = path.join(directory, filename); | ||
if (fs.existsSync(tsConfigPath)) { | ||
return tsConfigPath; | ||
} | ||
const parentDir = path.dirname(directory); | ||
if (parentDir === directory) { | ||
return undefined; | ||
} | ||
return findTsConfig(parentDir, filename); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import path from "path"; | ||
import { findTsConfig } from "../../src/Utils/findTsConfig.js"; | ||
|
||
describe("findTsConfig", () => { | ||
it("returns the path to the tsconfig.json file", () => { | ||
const TS_CONFIG_FILE = path.resolve(__dirname, "../../tsconfig.json"); | ||
const foundTsConfig = findTsConfig(); | ||
expect(foundTsConfig).toBeDefined(); | ||
expect(path.resolve(foundTsConfig as string)).toEqual(TS_CONFIG_FILE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters