@visulima/tsconfig
Version:
Find and/or parse the tsconfig.json file from a directory path.
70 lines (67 loc) • 2.22 kB
JavaScript
import { findUpSync, findUp } from '@visulima/fs';
import { NotFoundError } from '@visulima/fs/error';
import { readTsConfig } from './implicitBaseUrlSymbol-CqmKQyEp.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const TsConfigFileCache = /* @__PURE__ */ new Map();
const findTsConfig = /* @__PURE__ */ __name(async (cwd, options = {}) => {
const configFileName = options.configFileName ?? "tsconfig.json";
let filePath = await findUp(configFileName, {
...cwd && { cwd },
type: "file"
});
if (!filePath) {
filePath = await findUp("jsconfig.json", {
...cwd && { cwd },
type: "file"
});
}
if (!filePath) {
throw new NotFoundError("No such file or directory, for '" + configFileName + "' or 'jsconfig.json' found.");
}
const cache = options.cache && typeof options.cache !== "boolean" ? options.cache : TsConfigFileCache;
if (options.cache && cache.has(filePath)) {
return cache.get(filePath);
}
const output = {
config: readTsConfig(filePath, {
tscCompatible: options.tscCompatible
}),
path: filePath
};
if (options.cache) {
cache.set(filePath, output);
}
return output;
}, "findTsConfig");
const findTsConfigSync = /* @__PURE__ */ __name((cwd, options = {}) => {
const configFileName = options.configFileName ?? "tsconfig.json";
let filePath = findUpSync(configFileName, {
...cwd && { cwd },
type: "file"
});
if (!filePath) {
filePath = findUpSync("jsconfig.json", {
...cwd && { cwd },
type: "file"
});
}
if (!filePath) {
throw new NotFoundError("No such file or directory, for '" + configFileName + "' or 'jsconfig.json' found.");
}
const cache = options.cache && typeof options.cache !== "boolean" ? options.cache : TsConfigFileCache;
if (options.cache && cache.has(filePath)) {
return cache.get(filePath);
}
const output = {
config: readTsConfig(filePath, {
tscCompatible: options.tscCompatible
}),
path: filePath
};
if (options.cache) {
cache.set(filePath, output);
}
return output;
}, "findTsConfigSync");
export { findTsConfig, findTsConfigSync };