UNPKG

@znode/config

Version:

Load config support .env, $ID.{yml,json,ini}

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