Skip to content

Commit

Permalink
feat: support output report only
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxh16144 committed Dec 11, 2024
1 parent 4061802 commit 0513c2d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/sortApiTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const remarkWithYaml = unified()

const stream = majo();

function get(obj, pathStr = '', defaultValue) {
return pathStr.split('.').reduce((acc, key) => acc && acc[key], obj) || defaultValue;
}

function getCellValue(node) {
let cloneNode = { ...node };

Expand Down Expand Up @@ -150,7 +154,15 @@ function sortAPI(md, filename) {
function sortMiddleware(ctx) {
Object.keys(ctx.files).forEach(filename => {
const content = ctx.fileContents(filename);
ctx.writeContents(filename, sortAPI(content, filename));

const sortedContent = sortAPI(content, filename);

if (get(ctx.meta, 'program.report', false)) {
console.log(chalk.cyan(`🔍 report ${filename}`));
} else {
// write the sorted content back to the file
ctx.writeContents(filename, sortedContent);
}
});
}

Expand All @@ -166,12 +178,21 @@ module.exports = () => {
'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);
// Get the markdown file all need to be transformed

// 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(() => {
Expand Down

0 comments on commit 0513c2d

Please sign in to comment.