@znode/config
Version:
Load config support .env, $ID.{yml,json,ini}
47 lines (46 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._loadConfig = exports.load = void 0;
const fs = require("@znode/fs");
const dotenv = require("@znode/dotenv");
const path = require("./path");
async function load(options) {
var _a;
const configPath = (_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : (await path.getConfigPath(options === null || options === void 0 ? void 0 : options.id, options));
if (!configPath) {
if (!(options === null || options === void 0 ? void 0 : options.id)) {
throw new Error(`Cannot found config .env`);
}
throw new Error(`Cannot found config for ${options === null || options === void 0 ? void 0 : options.id}`);
}
return _loadConfig(configPath);
}
exports.load = load;
async function _loadConfig(filepath) {
if (path.isDotEnv(filepath)) {
return dotenv.loadConfig({ path: filepath });
}
if (path.isYml(filepath)) {
return fs.yml.load(filepath);
}
if (path.isJSON(filepath)) {
return fs.json.load(filepath);
}
if (path.isToml(filepath)) {
return fs.toml.load(filepath);
}
if (path.isIni(filepath)) {
return fs.ini.load(filepath);
}
// @TODO
if (path.isRc(filepath)) {
try {
return await fs.yml.load(filepath);
}
catch (error) {
throw new Error(`failed to parse config for ${filepath} (yaml syntax)`);
}
}
throw new Error(`cannot parse config in ${filepath}`);
}
exports._loadConfig = _loadConfig;