vendor: amis 6.13.0 base (fork root)
This commit is contained in:
142
packages/amis-formula/scripts/genDoc.ts
Normal file
142
packages/amis-formula/scripts/genDoc.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
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 */\n
|
||||
import {bulkRegisterFunctionDoc} from './function';
|
||||
|
||||
bulkRegisterFunctionDoc(${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));
|
||||
55
packages/amis-formula/scripts/lib.ts
Normal file
55
packages/amis-formula/scripts/lib.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {parse, evaluate} from '../src';
|
||||
import moment from 'moment';
|
||||
|
||||
// 来自 https://vanillajstoolkit.com/polyfills/arrayfind/
|
||||
if (!Array.prototype.find) {
|
||||
Array.prototype.find = function (callback: any) {
|
||||
// 1. Let O be ? ToObject(this value).
|
||||
if (this == null) {
|
||||
throw new TypeError('"this" is null or not defined');
|
||||
}
|
||||
|
||||
var o = Object(this);
|
||||
|
||||
// 2. Let len be ? ToLength(? Get(O, "length")).
|
||||
var len = o.length >>> 0;
|
||||
|
||||
// 3. If IsCallable(callback) is false, throw a TypeError exception.
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('callback must be a function');
|
||||
}
|
||||
|
||||
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
|
||||
var thisArg = arguments[1];
|
||||
|
||||
// 5. Let k be 0.
|
||||
var k = 0;
|
||||
|
||||
// 6. Repeat, while k < len
|
||||
while (k < len) {
|
||||
// a. Let Pk be ! ToString(k).
|
||||
// b. Let kValue be ? Get(O, Pk).
|
||||
// c. Let testResult be ToBoolean(? Call(callback, T, « kValue, k, O »)).
|
||||
// d. If testResult is true, return kValue.
|
||||
var kValue = o[k];
|
||||
if (callback.call(thisArg, kValue, k, o)) {
|
||||
return kValue;
|
||||
}
|
||||
// e. Increase k by 1.
|
||||
k++;
|
||||
}
|
||||
|
||||
// 7. Return undefined.
|
||||
return undefined;
|
||||
};
|
||||
}
|
||||
|
||||
export function momentFormat(
|
||||
input: any,
|
||||
inputFormat: string,
|
||||
outputFormat: string
|
||||
) {
|
||||
return moment(input, inputFormat).format(outputFormat);
|
||||
}
|
||||
|
||||
export {parse, evaluate, moment};
|
||||
Reference in New Issue
Block a user