t-comm
Version:
专业、稳定、纯粹的工具库
69 lines (64 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fs_fs = require('../fs/fs.js');
var string_string = require('../string/string.js');
require('fs');
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 = fs_fs.readFileSync(filePath);
var propsList = [];
var match = extractRegexp.exec(data);
while (match) {
propsList.push({
name: string_string.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] 提取正则
*
* ```ts
* extractProps({
* filePath: 'xxx.vue',
* })
* ```
*/
function extractProps(_a) {
var filePath = _a.filePath,
_b = _a.targetFilePath,
targetFilePath = _b === void 0 ? './log/extract-props.md' : _b,
_c = _a.extractRegexp,
extractRegexp = _c === void 0 ? DEFAULT_EXTRACT_REGEXP : _c;
var propsList = getPropsList(filePath, extractRegexp);
var table = genTable(propsList);
fs_fs.writeFileSync(targetFilePath, table);
}
exports.extractProps = extractProps;