@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
38 lines (36 loc) • 2.24 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
const require_exists = require('./exists.cjs');
const require_resolve = require('./resolve.cjs');
const require_json = require('./json.cjs');
let _stryke_path_join_paths = require("@stryke/path/join-paths");
let defu = require("defu");
defu = require_runtime.__toESM(defu, 1);
let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns");
let _stryke_convert_to_array = require("@stryke/convert/to-array");
let _stryke_path_cwd = require("@stryke/path/cwd");
let _stryke_path_is_type = require("@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 = (0, _stryke_path_cwd.cwd)()) {
let tsconfigFilePath = (0, _stryke_path_file_path_fns.findFileExtension)(filePath) === "json" ? filePath : (0, _stryke_path_join_paths.joinPaths)(filePath, "tsconfig.json");
if (!require_exists.existsSync(tsconfigFilePath)) {
tsconfigFilePath = await require_resolve.resolve(filePath, { extensions: ["json"] });
if (!require_exists.existsSync(tsconfigFilePath)) throw new Error(`tsconfig.json not found at ${tsconfigFilePath}. Please ensure the file exists.`);
}
let config = await require_json.readJsonFile(tsconfigFilePath);
if (config?.compilerOptions?.rootDir) config.compilerOptions.rootDir = (0, _stryke_path_join_paths.joinPaths)((0, _stryke_path_file_path_fns.findFilePath)(tsconfigFilePath), config.compilerOptions.rootDir);
if (config?.extends) for (const extendsName of (0, _stryke_convert_to_array.toArray)(config.extends)) {
const parentConfig = await loadTsConfig((0, _stryke_path_is_type.isNpmScopedPackage)(extendsName) ? extendsName : (0, _stryke_path_join_paths.joinPaths)((0, _stryke_path_file_path_fns.findFilePath)(tsconfigFilePath), extendsName));
if (parentConfig) config = (0, defu.default)(config, parentConfig ?? {});
}
config.extends = void 0;
return config;
}
//#endregion
exports.loadTsConfig = loadTsConfig;