-
Notifications
You must be signed in to change notification settings - Fork 190
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
79 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,53 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { findTsConfig } from "../../src/Utils/findTsConfig.js"; | ||
|
||
jest.mock("fs"); | ||
|
||
describe("findTsConfig", () => { | ||
const mockCwd = "/mock/project"; | ||
const mockTsConfigPath = path.join(mockCwd, "tsconfig.json"); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
jest.spyOn(process, "cwd").mockReturnValue(mockCwd); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("returns the path to the tsconfig.json file when it exists", () => { | ||
(fs.existsSync as jest.Mock).mockReturnValue(true); | ||
const foundTsConfig = findTsConfig(); | ||
expect(foundTsConfig).toBe(mockTsConfigPath); | ||
}); | ||
|
||
it("returns undefined when tsconfig.json is not found in any parent directory", () => { | ||
(fs.existsSync as jest.Mock).mockReturnValue(false); | ||
const foundTsConfig = findTsConfig(); | ||
expect(foundTsConfig).toBeUndefined(); | ||
}); | ||
|
||
it("searches in parent directories until tsconfig.json is found", () => { | ||
(fs.existsSync as jest.Mock).mockReturnValueOnce(false).mockReturnValueOnce(false).mockReturnValueOnce(true); | ||
|
||
const expectedPath = path.join(path.dirname(path.dirname(mockCwd)), "tsconfig.json"); | ||
const foundTsConfig = findTsConfig(); | ||
expect(foundTsConfig).toBe(expectedPath); | ||
}); | ||
|
||
it("returns undefined when reaching the root directory without finding tsconfig.json", () => { | ||
(fs.existsSync as jest.Mock).mockReturnValue(false); | ||
const foundTsConfig = findTsConfig("/"); | ||
expect(foundTsConfig).toBeUndefined(); | ||
}); | ||
|
||
it("uses the provided filename when searching for the config file", () => { | ||
(fs.existsSync as jest.Mock).mockReturnValue(true); | ||
const customFilename = "custom-tsconfig.json"; | ||
const expectedPath = path.join(mockCwd, customFilename); | ||
const foundTsConfig = findTsConfig(undefined, customFilename); | ||
expect(foundTsConfig).toBe(expectedPath); | ||
}); | ||
}); |
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