mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:48:45 +08:00
c591ab4381
* 尝试使用 vite 跑开发环境 * 尝试使用 vite 跑开发环境 * 尝试使用 vite 跑开发环境 * 尝试使用 vite 跑开发环境 * 样式文档调整 * 修复有多个 __inline 时的报错 * scirpt 调整 * feat:chart接入事件动作 (#5669) * chore: input-group 没有 name 不应该报 warning (#5667) * chore: 处理编译TS Warning,调整TableStore中label解析的顺序 (#5670) * feat: timeline时间轴支持自定义图标 (#5668) * fix:折叠器Collapse自定义图标&inputNumber对含后缀生效 * 修改 * 正则修改 * 正则修改 * 正则修改 * feat:将默认px替换为rem 2px===0.125rem * feat:timeline时间轴支持自定义图标 Co-authored-by: xujiahao01 <xujiahao01@baidu.com> * fix: 调整echarts-stat使用方式,兼容vite打包异步非esm模块 (#5672) * feat: chart 支持配置加载 geojson 及百度地图 (#5674) * feat: chart 支持配置 geoURL 及 geoName * 增加百度地图配置 * chore: 修复 coverage 运行报错 (#5678) * chore: coverage 执行换成 v8 解决内存问题 (#5679) * 尝试使用 vite 跑开发环境 * amis-formula 也添加 esm 模块 * 代码合并有问题,做些调整 Co-authored-by: hsm-lv <80095014+hsm-lv@users.noreply.github.com> Co-authored-by: 吴多益 <wuduoyi@baidu.com> Co-authored-by: RUNZE LU <36724300+lurunze1226@users.noreply.github.com> Co-authored-by: 徐佳豪 <1440054388@qq.com> Co-authored-by: xujiahao01 <xujiahao01@baidu.com> Co-authored-by: 刘丹 <365533093@qq.com>
155 lines
3.8 KiB
TypeScript
155 lines
3.8 KiB
TypeScript
import path from 'path';
|
||
import fs from 'fs';
|
||
import doctrine from 'doctrine';
|
||
|
||
const workDir = path.resolve(path.dirname(__dirname));
|
||
const jsFile = path.join(workDir, 'src/evalutor.ts');
|
||
const outputFile = path.join(workDir, 'src/doc.ts');
|
||
const outputMD = path.join(workDir, 'src/doc.md');
|
||
|
||
function getFormulaComments(contents: string) {
|
||
const comments: Array<{
|
||
fnName: string;
|
||
comments: string;
|
||
}> = [];
|
||
|
||
contents.replace(/\/\*[\s\S]+?\*\//g, (_, index, input) => {
|
||
const pos = index + _.length;
|
||
const following = input.substring(pos, pos + 200);
|
||
|
||
if (/^\s*fn(\w+)\(/.test(following)) {
|
||
comments.push({
|
||
fnName: RegExp.$1,
|
||
comments: _
|
||
});
|
||
}
|
||
|
||
return _;
|
||
});
|
||
|
||
return comments;
|
||
}
|
||
|
||
function formatType(tag: any): string {
|
||
// console.log(tag);
|
||
if (tag.type === 'RestType') {
|
||
return `...${formatType(tag.expression)}`;
|
||
} else if (tag.type === 'TypeApplication') {
|
||
return `Array<${tag.applications
|
||
.map((item: any) => formatType(item))
|
||
.join(',')}>`;
|
||
}
|
||
|
||
return tag.name;
|
||
}
|
||
|
||
async function main(...params: Array<any>) {
|
||
const contents = fs.readFileSync(jsFile, 'utf8');
|
||
|
||
const comments = getFormulaComments(contents);
|
||
const result = comments.map(item => {
|
||
const ast = doctrine.parse(item.comments, {
|
||
unwrap: true
|
||
});
|
||
const result: any = {
|
||
name: item.fnName,
|
||
description: ast.description
|
||
};
|
||
|
||
let example = '';
|
||
let params: Array<any> = [];
|
||
let returns: any = undefined;
|
||
let namespace = '';
|
||
ast.tags.forEach(tag => {
|
||
if (tag.title === 'example') {
|
||
example = tag.description!;
|
||
} else if (tag.title === 'namespace') {
|
||
namespace = tag.name!;
|
||
} else if (tag.title === 'param') {
|
||
params.push({
|
||
type: formatType(tag.type),
|
||
name: tag.name,
|
||
description: tag.description
|
||
});
|
||
} else if (tag.title === 'returns') {
|
||
returns = {
|
||
type: formatType(tag.type),
|
||
description: tag.description
|
||
};
|
||
}
|
||
});
|
||
|
||
result.example = example;
|
||
result.params = params;
|
||
result.returns = returns;
|
||
result.namespace = namespace;
|
||
|
||
return result;
|
||
});
|
||
|
||
fs.writeFileSync(
|
||
outputFile,
|
||
`/**\n * 公式文档 请运行 \`npm run genDoc\` 自动生成\n */\nexport const doc: ${[
|
||
`{`,
|
||
` name: string;`,
|
||
` description: string;`,
|
||
` example: string;`,
|
||
` params: {`,
|
||
` type: string;`,
|
||
` name: string;`,
|
||
` description: string | null;`,
|
||
` }[];`,
|
||
` returns: {`,
|
||
` type: string;`,
|
||
` description: string | null;`,
|
||
` };`,
|
||
` namespace: string;`,
|
||
`}[]`
|
||
].join('\n')} = ${JSON.stringify(result, null, 2).replace(
|
||
/\"(\w+)\"\:/g,
|
||
(_, key) => `${key}:`
|
||
)};`,
|
||
'utf8'
|
||
);
|
||
console.log(`公式文档生成 > ${outputFile}`);
|
||
|
||
const grouped: any = {};
|
||
result.forEach((item: any) => {
|
||
const scope = item.namespace || '其他';
|
||
const arr = grouped[scope] || (grouped[scope] = []);
|
||
|
||
arr.push(item);
|
||
});
|
||
|
||
let md = '';
|
||
Object.keys(grouped).forEach(key => {
|
||
md += `## ${key}\n\n`;
|
||
|
||
grouped[key].forEach((item: any) => {
|
||
md += `### ${item.name}\n\n`;
|
||
|
||
md += `用法:\`${item.example}\`\n\n`;
|
||
|
||
if (item.params.length) {
|
||
// md += `参数:\n`;
|
||
|
||
item.params.forEach((param: any) => {
|
||
md += ` * \`${param.name}:${param.type}\` ${param.description}\n`;
|
||
});
|
||
|
||
if (item.returns) {
|
||
md += `\n返回:\`${item.returns.type}\` ${
|
||
item.returns.description || ''
|
||
}\n\n`;
|
||
}
|
||
}
|
||
|
||
md += `${item.description}\n\n`;
|
||
});
|
||
});
|
||
fs.writeFileSync(outputMD, md, 'utf8');
|
||
console.log(`公式md生成 > ${outputMD}`);
|
||
}
|
||
|
||
main().catch(e => console.error(e));
|