Skip to content

Commit

Permalink
feat(cli): simplify document generation input
Browse files Browse the repository at this point in the history
  • Loading branch information
xiefenga committed Dec 19, 2023
1 parent 42e0ad6 commit dfec5ea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/gulp-postcss": "^8.0.3",
"@types/gulp-rename": "^2.0.2",
"@types/inquirer": "^9.0.3",
"@types/lodash.camelcase": "^4.3.9",
"@types/lodash.capitalize": "^4.2.7",
"@types/lodash.kebabcase": "^4.1.7",
"@types/lodash.template": "^4.5.1",
Expand Down Expand Up @@ -65,6 +66,7 @@
"gulp-typescript": "6.0.0-alpha.1",
"inquirer": "^9.2.10",
"jiti": "^1.19.1",
"lodash.camelcase": "^4.3.0",
"lodash.capitalize": "^4.2.1",
"lodash.kebabcase": "^4.1.1",
"lodash.template": "^4.5.0",
Expand Down
34 changes: 11 additions & 23 deletions packages/cli/src/commands/generate/document/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import consola from 'consola';
import inquirer from 'inquirer';
import matter from 'gray-matter';
import camelcase from 'lodash.camelcase';
import { basename, resolve } from 'node:path';
import { readFileSync, readJsonSync, readdirSync, writeFileSync } from 'fs-extra';

Expand Down Expand Up @@ -51,41 +53,27 @@ export default async () => {
message: 'Please select the document category: ',
});

const { title } = await inquirer.prompt({
name: 'title',
const { zName } = await inquirer.prompt({
name: 'zName',
type: 'input',
validate: input => !!input?.trim(),
message: 'Please select the document title (browser tab title): ',
message: 'Please input the component Chinese name: ',
});

const { label } = await inquirer.prompt({
name: 'label',
type: 'input',
validate: input => !!input?.trim(),
message: 'Please select the document label (link text): ',
});
const formatedComponent = `${(component as string).charAt(0).toUpperCase()}${camelcase(component).slice(1)}`;

const title = `${zName} ${formatedComponent}`;

// const orderMap = docs.reduce(
// (map, item) => {
// const { data } = matter(readFileSync(resolve(COMPONENTS_DOCS_DIR, `${item}.mdx`), 'utf-8'));
// if (data.category === category) {
// map[item] = data.order;
// }
// return map;
// },
// {} as Record<string, number>
// );
const label = `${formatedComponent} ${zName}`;

const order = docs.reduce((memo, item) => {
const { data } = matter(readFileSync(resolve(COMPONENTS_DOCS_DIR, `${item}.mdx`), 'utf-8'));
if (data.category === category) {
memo++;
}
return memo;
return data.category === category ? memo + 1 : memo;
}, 1);

writeFileSync(
resolve(COMPONENTS_DOCS_DIR, `${component}.mdx`),
matter.stringify(`# ${label}`, { title, label, category, order })
);
consola.success('generate successfully');
};
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfec5ea

Please sign in to comment.