@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
36 lines (34 loc) • 1.7 kB
JavaScript
import { existsSync } from "./exists.mjs";
import { resolve } from "./resolve.mjs";
import { readJsonFile } from "./json.mjs";
import { joinPaths } from "@stryke/path/join-paths";
import defu from "defu";
import { findFileExtension, findFilePath } from "@stryke/path/file-path-fns";
import { toArray } from "@stryke/convert/to-array";
import { cwd } from "@stryke/path/cwd";
import { isNpmScopedPackage } from "@stryke/path/is-type";
//#region src/tsconfig.ts
/**
* Loads a tsconfig.json file and returns the parsed JSON object.
*
* @param filePath - The directory to start searching for the tsconfig.json file.
* @returns The parsed tsconfig.json object or null if not found.
*/
async function loadTsConfig(filePath = cwd()) {
let tsconfigFilePath = findFileExtension(filePath) === "json" ? filePath : joinPaths(filePath, "tsconfig.json");
if (!existsSync(tsconfigFilePath)) {
tsconfigFilePath = await resolve(filePath, { extensions: ["json"] });
if (!existsSync(tsconfigFilePath)) throw new Error(`tsconfig.json not found at ${tsconfigFilePath}. Please ensure the file exists.`);
}
let config = await readJsonFile(tsconfigFilePath);
if (config?.compilerOptions?.rootDir) config.compilerOptions.rootDir = joinPaths(findFilePath(tsconfigFilePath), config.compilerOptions.rootDir);
if (config?.extends) for (const extendsName of toArray(config.extends)) {
const parentConfig = await loadTsConfig(isNpmScopedPackage(extendsName) ? extendsName : joinPaths(findFilePath(tsconfigFilePath), extendsName));
if (parentConfig) config = defu(config, parentConfig ?? {});
}
config.extends = void 0;
return config;
}
//#endregion
export { loadTsConfig };
//# sourceMappingURL=tsconfig.mjs.map