Skip to content

Commit 5cdd15c

Browse files
author
zhaozhiwen
committed
chore(templates): 使用plop+hbs替代metalsmith+hbs手动处理组件模板
1 parent c98d24e commit 5cdd15c

File tree

17 files changed

+292
-491
lines changed

17 files changed

+292
-491
lines changed

components/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { default as Alert } from './alert';
2+
3+
/* PLOP_INJECT_EXPORT */

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"test:watch": "jest --watch",
1919
"test:coverage": "jest --coverage",
2020
"test:update": "jest --updateSnapshot",
21-
"new:comp": "node scripts/new-comp"
21+
"new": "plop --plopfile ./scripts/plopfile.ts",
22+
"postnew": "prettier --write components/**/*{ts,tsx} --loglevel silent"
2223
},
2324
"repository": {
2425
"type": "git",
@@ -59,7 +60,6 @@
5960
"@types/vfile-message": "^2.0.0",
6061
"@umijs/fabric": "^1.2.12",
6162
"chalk": "^3.0.0",
62-
"change-case": "^4.1.1",
6363
"commitizen": "^4.0.3",
6464
"cz-conventional-changelog": "^3.0.2",
6565
"docz": "^2.2.0",
@@ -71,14 +71,13 @@
7171
"gulp-babel": "^8.0.0",
7272
"gulp-cssnano": "^2.1.3",
7373
"gulp-less": "^4.0.1",
74-
"handlebars": "^4.7.3",
7574
"husky": "^3.1.0",
7675
"identity-obj-proxy": "^3.0.0",
7776
"inquirer": "^7.0.4",
7877
"jest": "^24.9.0",
7978
"less": "^3.10.3",
8079
"lint-staged": "^9.5.0",
81-
"metalsmith": "^2.3.0",
80+
"plop": "^2.6.0",
8281
"prettier": "^1.19.1",
8382
"prismjs": "^1.17.1",
8483
"raw-loader": "^4.0.0",

scripts/new-comp.js

-104
This file was deleted.

scripts/plopfile.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
import { NodePlopAPI } from 'plop';
3+
import path from 'path';
4+
5+
export default function(plop: NodePlopAPI) {
6+
plop.setGenerator('component', {
7+
description: '创建一个新组件',
8+
prompts: [
9+
{ type: 'input', name: 'name', message: '请输入组件名称(多个单词以中横线命名)' },
10+
{ type: 'input', name: 'CN', message: '请输入组件中文名称' },
11+
{ type: 'input', name: 'description', message: '请输入组件描述' },
12+
],
13+
actions: [
14+
{
15+
type: 'add',
16+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/index.ts'),
17+
templateFile: path.resolve(__dirname, '../templates/component/index.hbs'),
18+
},
19+
{
20+
type: 'add',
21+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/{{kebabCase name}}.tsx'),
22+
templateFile: path.resolve(__dirname, '../templates/component/comp.hbs'),
23+
},
24+
{
25+
type: 'add',
26+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/style/index.less'),
27+
templateFile: path.resolve(__dirname, '../templates/component/style/style.hbs'),
28+
},
29+
{
30+
type: 'add',
31+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/style/index.ts'),
32+
templateFile: path.resolve(__dirname, '../templates/component/style/index.hbs'),
33+
},
34+
{
35+
type: 'add',
36+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/index.mdx'),
37+
templateFile: path.resolve(__dirname, '../templates/component/doc.hbs'),
38+
},
39+
{
40+
type: 'add',
41+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/interface.ts'),
42+
templateFile: path.resolve(__dirname, '../templates/component/interface.hbs'),
43+
},
44+
{
45+
type: 'add',
46+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/demo/basic.tsx'),
47+
templateFile: path.resolve(__dirname, '../templates/component/demo/basic.hbs'),
48+
},
49+
{
50+
type: 'add',
51+
path: path.resolve(__dirname, '../components/{{kebabCase name}}/__tests__/index.test.tsx'),
52+
templateFile: path.resolve(__dirname, '../templates/component/__tests__/index.test.hbs'),
53+
},
54+
{
55+
type: 'append',
56+
path: path.resolve(__dirname, '../components/index.ts'),
57+
pattern: '/* PLOP_INJECT_EXPORT */',
58+
template: "export { default as {{pascalCase name}} } from './{{kebabCase name}}';",
59+
},
60+
],
61+
});
62+
}

template/__tests__/index.test.tsx

-11
This file was deleted.

template/demo/1-demo-basic.tsx

-10
This file was deleted.

template/index.mdx

-28
This file was deleted.

template/index.tsx

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
4+
import {{pascalCase name}} from '../index';
5+
6+
describe('{{pascalCase name}}', () => {
7+
it('renders without error', () => {
8+
9+
});
10+
});

templates/component/comp.hbs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { Props } from './interface';
3+
4+
const defaultProps = {
5+
6+
};
7+
8+
const {{pascalCase name}}: React.FC<Props> = userProps => {
9+
const props = { ...defaultProps, ...userProps };
10+
11+
return <>{{pascalCase name}}</>;
12+
};
13+
14+
export default {{pascalCase name}};

templates/component/demo/basic.hbs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import { {{pascalCase name}} } from 'happy-ui';
3+
4+
export default () => (
5+
<{{pascalCase name}} />
6+
);

templates/component/doc.hbs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
name: {{pascalCase name}} {{CN}}
3+
route: /components/{{kebabCase name}}
4+
menu: 组件
5+
---
6+
7+
import CodeBox from '../../doc-comps/happy-box';
8+
9+
import BasicDemo from './demo/basic';
10+
import BasicDemoCode from '!raw-loader!./demo/basic';
11+
12+
## {{pascalCase name}} {{CN}}
13+
14+
{{description}}
15+
16+
### 代码演示
17+
18+
#### 基本使用
19+
20+
<CodeBox code={BasicDemoCode} title="" desc="">
21+
<BasicDemo></BasicDemo>
22+
</CodeBox>
23+
24+
25+
### API
26+
27+
| 属性 | 说明 | 类型 | 默认值 |
28+
| ---- | ---- | ---- | ------ |

templates/component/index.hbs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './interface';
2+
3+
export { default } from './{{kebabCase name}}';

templates/component/interface.hbs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Props {
2+
3+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)