@anijs/load-config
Version:
134 lines (133 loc) • 5.43 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toModule = (module2) => {
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
__export(exports, {
default: () => src_default,
findConfigFile: () => findConfigFile
});
var import_find_up = __toModule(require("find-up"));
var import_fs_extra = __toModule(require("fs-extra"));
var import_path = __toModule(require("path"));
var import_debug = __toModule(require("debug"));
var import_deepmerge = __toModule(require("deepmerge"));
var import_uuid = __toModule(require("uuid"));
var import_readJSONData = __toModule(require("./readJSONData"));
var import_readTSData = __toModule(require("./readTSData"));
var import_readJSData = __toModule(require("./readJSData"));
var import_readYAMLData = __toModule(require("./readYAMLData"));
if (process.env.DEBUG_LOAD_FILE) {
import_debug.default.enable("load-config:*");
}
const readData = async (params) => {
const { configFile, cwd = process.cwd(), configName = "", beforeConfigData } = params || {};
const filePath = await absolutePath(configFile, cwd);
const fileExt = import_path.default.extname(filePath);
if (!fileExt)
throw new Error(`No ${filePath} module found`);
let configData = {};
switch (fileExt) {
case ".json":
configData = await (0, import_readJSONData.default)(filePath, import_path.default.basename(filePath) === "package.json" ? configName : "");
break;
case ".ts":
configData = await (0, import_readTSData.default)(filePath);
break;
case ".js":
configData = await (0, import_readJSData.default)(filePath);
break;
case ".yaml":
configData = await (0, import_readYAMLData.default)(filePath);
break;
default:
throw new Error(`The ${fileExt} file type is not supported`);
}
const extendsFile = configData.extends;
const newResult = (0, import_deepmerge.default)(configData, beforeConfigData);
if (extendsFile !== void 0) {
const result = await readData({
cwd: filePath,
configFile: extendsFile,
beforeConfigData: newResult,
configName
});
return result;
}
if (newResult.extends !== void 0)
delete newResult.extends;
return newResult;
};
async function absolutePath(configFile, cwd) {
if (import_path.default.isAbsolute(configFile)) {
if (import_path.default.extname(configFile))
return configFile;
throw new Error(`No ${configFile} module found`);
}
const filename = `${(0, import_uuid.v4)()}.js`;
const fullpath = import_path.default.resolve(cwd, "../", filename);
await (0, import_fs_extra.outputFile)(fullpath, `module.exports=require.resolve('${configFile}')`);
const pathResult = require(fullpath);
return pathResult;
}
async function loadConfig(name, cwd) {
if (!name)
throw new Error("the config file name is invalid");
const cxt = cwd || process.cwd();
const fileNames = [".json", ".ts", ".js", ".yaml", ".yml"].map((ext) => `${name}.config${ext}`);
const configFile = await findConfigFile(name, ["package.json", ...fileNames], cxt);
if (!configFile)
throw new Error("not located in the config file");
const configData = await readData({
cwd: cxt,
configName: name,
configFile,
beforeConfigData: {}
});
return configData;
}
async function findConfigFile(name, fileNames, context) {
const result = await (0, import_find_up.default)(async (directory) => {
const pathExitPromiseList = fileNames.map((fileName) => (0, import_fs_extra.pathExists)(import_path.default.join(directory, fileName)));
const pathExitResultList = await Promise.all(pathExitPromiseList);
const validIndex = pathExitResultList.findIndex((pathExit, index) => {
if (pathExit) {
const file = import_path.default.join(directory, fileNames[index]);
const isPackageJson = /package\.json/.test(file);
if (isPackageJson) {
const pkgData = (0, import_fs_extra.readJsonSync)(file);
if (pkgData[name])
return true;
} else
return true;
}
return false;
});
if (validIndex > -1)
return import_path.default.join(directory, fileNames[validIndex]);
}, { cwd: context, type: "file" });
return result;
}
var src_default = loadConfig;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
findConfigFile
});