UNPKG

@tech_query/node-toolkit

Version:
85 lines (84 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringifyEnv = void 0; exports.packageNameOf = packageNameOf; exports.patternOf = patternOf; exports.currentModulePath = currentModulePath; exports.packageOf = packageOf; exports.configOf = configOf; exports.saveEnv = saveEnv; const path_1 = require("path"); const fs_extra_1 = require("fs-extra"); const util_1 = require("util"); const yaml_1 = require("yaml"); const language_1 = require("./language"); const file_1 = require("./file"); function packageNameOf(path) { const list = (0, path_1.resolve)(path).split(/[/\\]+/); path = list.slice(list.slice(-2)[0].startsWith('@') ? -2 : -1).join('/'); return path.toLowerCase().replace(/[^@/\w]+/g, '-'); } /** * @param map - Key for RegExp source, value for replacement * * @return Key for replacement, value for RegExp */ function patternOf(map) { const patternMap = {}; for (const pattern in map) patternMap[map[pattern]] = (0, language_1.toRegExp)(pattern); for (const _key in patternMap) return patternMap; } function currentModulePath() { var _a, _b, _c, _d; try { throw Error(); } catch (error) { return (_d = (_c = (_b = (_a = error.stack) === null || _a === void 0 ? void 0 : _a.split(/[\r\n]+/)[2]) === null || _b === void 0 ? void 0 : _b.match(/at (.+?\()?(.+):\d+:\d+\)?$/)) === null || _c === void 0 ? void 0 : _c[2]) === null || _d === void 0 ? void 0 : _d.replace(/\\/g, '/'); } } function packageOf(path = './') { for (const file of (0, file_1.findUp)(path)) if ((0, path_1.basename)(file) === 'package.json') return { path: (0, path_1.dirname)(file), meta: (0, fs_extra_1.readJSONSync)(file) }; } /** * Get configuration of a Package from * `package.json`, `.${name}.json` or `.${name}.yml` in `process.cwd()` * * (`process.env.NODE_ENV` will affect the result) */ function configOf(name) { var _a, _b, _c; let config = (_b = (_a = packageOf('./test')) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b[name]; if (!config) for (const type of ['json', 'yaml', 'yml']) if ((0, fs_extra_1.existsSync)((name = `./.${name}.${type}`))) { switch (type) { case 'json': config = (0, fs_extra_1.readJSONSync)(name); break; case 'yaml': case 'yml': config = (0, yaml_1.parse)((0, fs_extra_1.readFileSync)(name) + ''); } break; } return ((_c = config === null || config === void 0 ? void 0 : config.env) === null || _c === void 0 ? void 0 : _c[process.env.NODE_ENV]) || config; } const stringifyEnv = (data) => Object.entries(data) .map(([key, value]) => `${key}=${JSON.stringify(value)}`) .join('\n'); exports.stringifyEnv = stringifyEnv; async function saveEnv(newData, filePath) { await (0, fs_extra_1.ensureFile)(filePath); const oldData = (0, util_1.parseEnv)((0, fs_extra_1.readFileSync)(filePath) + ''); newData = Object.assign(Object.assign({}, oldData), newData); await (0, fs_extra_1.outputFile)(filePath, (0, exports.stringifyEnv)(newData)); return newData; }