"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findJson = findJson; exports.getStr = getStr; exports.difference = difference; exports.intersect = intersect; exports.union = union; exports.formatJsonByFile = formatJsonByFile; exports.deWeight = deWeight; exports.randomString = randomString; exports.checkNodeVersion = checkNodeVersion; const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); /** * @name 从一组路径里查找到所有json文件 * @export * @param {Array} pathArr * @returns {Set} */ function findJson(pathArr) { const result = new Set(); for (let item of pathArr) { const endIndex = item.length; const str = item.substring(endIndex - 5, endIndex); if (str === '.json') { result.add(item); } } return result; } /** * @name 已知前后文取中间文本 * @export * @param {string} str * @param {string} start * @param {string} end * @returns {(string | null)} */ function getStr(str, start, end) { const reg = new RegExp(`${start}(.*?)${end}`); let res = str.match(reg); return res ? res[1] : null; } /** * @name 差集 * @export * @template T * @param {Set} current * @param {Set} target * @returns {Set} */ function difference(current, target) { return new Set([...target].filter(x => !current.has(x))); } /** * @name 获取交集 * @export * @template T * @param {Set} current * @param {Set} target * @returns {Set} */ function intersect(current, target) { return new Set([...target].filter(x => current.has(x))); } /** * @name 获取并集 * @export * @template T * @param {Set} current * @param {Set} target * @returns {Set} */ function union(current, target) { return new Set([...current, ...target]); } /** * @name 格式化json * @export * @template T * @param {T} data * @returns {string} */ function formatJsonByFile(data) { return JSON.stringify(data, null, 2); } /** * @name 数组对象去重 * @export * @param {Array} arr 需要去重的数组或set * @param {*} [type] 需要根据哪个字段去重 * @returns */ function deWeight(arr, type) { let map = new Map(); for (let item of arr) { if (!map.has(item[type])) { map.set(item[type], item); } } return new Set([...map.values()]); } /** * @name 随机字符串 * @export * @param {number} length * @returns {string} */ function randomString(length) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * chars.length); result += chars[randomIndex]; } return result; } /** * @name 检查当前nodejs运行时版本 * @export * @throws 当版本不满足最低要求时抛出异常 */ function checkNodeVersion() { const currentNodeVersion = process.versions.node; const semver = currentNodeVersion.split('.'); const major = semver[0]; const minNodeVersion = 20; if (Number(major) < minNodeVersion) { console.error(chalk_1.default.yellow('You are running Node ' + currentNodeVersion + '.\n') + chalk_1.default.red('Oak-cli requires Node ' + minNodeVersion + ' or higher. \n' + 'Please update your version of Node.')); process.exit(-1); } }