UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

68 lines (65 loc) 2.23 kB
import { hyphenate } from '../string/string.mjs'; import { writeFileSync, readFileSync } from '../fs/fs.mjs'; import '../nodejs/crypto.mjs'; import '../nodejs/fs.mjs'; import '../nodejs/os.mjs'; var DEFAULT_EXTRACT_REGEXP = /([\w]+):\s*\{\s+type:\s*([\w]+),\s+default:\s(.*),/g; function parseDefaultValue(value) { if (value.indexOf('=>') > -1) { value = value.replace(/\(\)\s*=>\s*\(?/, '').replace(/\)?$/, '').trim(); } if (['\'\'', '[]', '{}'].indexOf(value) > -1) { return '-'; } value = value.replace(/^'|'$/g, ''); return "`".concat(value, "`"); } function getPropsList(filePath, extractRegexp) { var data = readFileSync(filePath); var propsList = []; var match = extractRegexp.exec(data); while (match) { propsList.push({ name: hyphenate(match[1]), type: match[2].toLocaleLowerCase(), defaultValue: parseDefaultValue(match[3]) }); match = extractRegexp.exec(data); } return propsList; } function genTable(list) { var table = ['| 参数 | 说明 | 类型 | 默认值 |', '| ------------------ | ---------------- | --------- | ------ |']; list.forEach(function (item) { var name = item.name, type = item.type, defaultValue = item.defaultValue; table.push("| ".concat(name, " | | _").concat(type, "_ | ").concat(defaultValue, " |")); }); return table.join('\n'); } /** * 提取 Vue 组件的 props * @param {obj} params 参数 * @param {string} params.filePath 源文件地址 * @param {string} [params.targetFilePath] 输出文件地址 * @param {Regexp} [params.extractRegexp] 提取正则 * @example * * ```ts * extractProps({ * filePath: 'xxx.vue', * }) * ``` */ function extractProps(_ref) { var filePath = _ref.filePath, _ref$targetFilePath = _ref.targetFilePath, targetFilePath = _ref$targetFilePath === void 0 ? './log/extract-props.md' : _ref$targetFilePath, _ref$extractRegexp = _ref.extractRegexp, extractRegexp = _ref$extractRegexp === void 0 ? DEFAULT_EXTRACT_REGEXP : _ref$extractRegexp; var propsList = getPropsList(filePath, extractRegexp); var table = genTable(propsList); writeFileSync(targetFilePath, table); } export { extractProps };