UNPKG

@ant-design/tools

Version:
173 lines (167 loc) 6.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _commander = require("commander"); var _majo = require("majo"); var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _chalk = _interopRequireDefault(require("chalk")); var _unified = _interopRequireDefault(require("unified")); var _remarkParse = _interopRequireDefault(require("remark-parse")); var _remarkStringify = _interopRequireDefault(require("remark-stringify")); var _remarkYamlConfig = _interopRequireDefault(require("remark-yaml-config")); var _remarkFrontmatter = _interopRequireDefault(require("remark-frontmatter")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Minimal interface for AST node // Define fileAPIs type let fileAPIs = {}; const remarkWithYaml = (0, _unified.default)().use(_remarkParse.default).use(_remarkStringify.default, { listItemIndent: '1', stringLength: () => 3 }).use(_remarkFrontmatter.default).use(_remarkYamlConfig.default); const stream = (0, _majo.majo)(); // Updated get with improved types function get(obj, pathStr = '', defaultValue) { return pathStr.split('.').reduce((acc, key) => { if (acc && typeof acc === 'object' && key in acc) { return acc[key]; } return undefined; }, obj) || defaultValue; } function getCellValue(node) { let cloneNode = { ...node }; // Traverse until a text node is found while (cloneNode.type !== 'text' && cloneNode.children && cloneNode.children.length > 0) { cloneNode = cloneNode.children[0]; } return cloneNode.value || ''; } function checkForCellDeletion(node) { let cloneNode = { ...node }; while (cloneNode.children && Array.isArray(cloneNode.children)) { if (cloneNode.type === 'delete') { return true; } cloneNode = cloneNode.children[0]; } return cloneNode.type === 'delete'; } // from small to large const sizeBreakPoints = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl']; const whiteMethodList = ['afterChange', 'beforeChange']; const groups = { isDynamic: val => /^on[A-Z]/.test(val) || whiteMethodList.indexOf(val) > -1, isSize: val => sizeBreakPoints.indexOf(val) > -1, isDeprecated: checkForCellDeletion }; function asciiSort(prev, next) { if (prev > next) { return 1; } if (prev < next) { return -1; } return 0; } // follow the alphabet order function alphabetSort(nodes) { return nodes.sort((a, b) => asciiSort(getCellValue(a).toLowerCase(), getCellValue(b).toLowerCase())); } function sizeSort(nodes) { return nodes.sort((a, b) => asciiSort(sizeBreakPoints.indexOf(getCellValue(a).toLowerCase()), sizeBreakPoints.indexOf(getCellValue(b).toLowerCase()))); } function sort(ast, filename) { const nameMatch = filename.match(/^components\/([^/]*)\//); const componentName = nameMatch ? nameMatch[1] : 'unknown'; fileAPIs[componentName] = fileAPIs[componentName] || { static: new Set(), size: new Set(), dynamic: new Set(), deprecated: new Set() }; ast.children.forEach(child => { // Initialize arrays with Node type const staticProps = []; const dynamicProps = []; const sizeProps = []; const deprecatedProps = []; if (child.type === 'table' && child.children) { // Skip table header (thead) child.children.slice(1).forEach(node => { const value = getCellValue(node); if (groups.isDeprecated(node)) { deprecatedProps.push(node); fileAPIs[componentName].deprecated.add(value); } else if (groups.isDynamic(value)) { dynamicProps.push(node); fileAPIs[componentName].dynamic.add(value); } else if (groups.isSize(value)) { sizeProps.push(node); fileAPIs[componentName].size.add(value); } else { staticProps.push(node); fileAPIs[componentName].static.add(value); } }); child.children = [child.children[0], ...alphabetSort(staticProps), ...sizeSort(sizeProps), ...alphabetSort(dynamicProps), ...alphabetSort(deprecatedProps) // deprecated props should be the last ]; } }); return ast; } function sortAPI(md, filename) { const ast = remarkWithYaml.parse(md); const sortedAst = sort(ast, filename); return remarkWithYaml.stringify(sortedAst); } function sortMiddleware(ctx) { Object.keys(ctx.files).forEach(filename => { const content = ctx.fileContents(filename); const sortedContent = sortAPI(content, filename); if (get(ctx.meta, 'program.report', false)) { console.log(_chalk.default.cyan(`🔍 report ${filename}`)); } else { ctx.writeContents(filename, sortedContent); } }); } var _default = () => { fileAPIs = {}; const program = new _commander.Command(); program.version('0.1.0').option('-f, --file [file]', 'Specify which file to be transformed', 'components/**/index.+(zh-CN|en-US).md').option('-o, --output [output]', 'Specify component api output path', '~component-api.json').option('-r, --report', 'Only output the report, will not modify the file', false).parse(process.argv); // inject context to the majo stream function injectContext(ctx) { if (typeof ctx.meta !== 'object') ctx.meta = {}; Object.assign(ctx.meta, { program }); } /* eslint-disable no-console */ stream.source(program.file).use(injectContext).use(sortMiddleware).dest('.').then(() => { if (program.output) { const data = {}; Object.keys(fileAPIs).forEach(componentName => { data[componentName] = { static: [...fileAPIs[componentName].static], size: [...fileAPIs[componentName].size], dynamic: [...fileAPIs[componentName].dynamic], deprecated: [...fileAPIs[componentName].deprecated] }; }); const reportPath = _path.default.resolve(program.output); _fs.default.writeFileSync(reportPath, JSON.stringify(data, null, 2), 'utf8'); console.log(_chalk.default.cyan(`API list file: ${reportPath}`)); } }).then(() => { console.log(_chalk.default.green(`sort ant-design api successfully!`)); }); /* eslint-enable no-console */ }; exports.default = _default;