1
1
import fs from 'node:fs' ;
2
- import fg from 'fast-glob' ;
3
-
4
- function getComponentFiles ( module : string ) {
5
- const kbName = toKebabCase ( module ) ;
6
- const dir = `packages/ui/src/components/${ kbName } ` ;
7
-
8
- const files = fg . sync ( `**/*.vue` , { onlyFiles : true , cwd : dir } ) ;
9
-
10
- const componentNames : string [ ] = [ ] ;
11
-
12
- files
13
- . filter ( file => file . includes ( '-' ) )
14
- . forEach ( file => {
15
- const componentName = file . replace ( '.vue' , '' ) ;
16
-
17
- componentNames . push ( toKebabCase ( componentName ) ) ;
18
-
19
- generateComponent ( toKebabCase ( componentName ) , module , dir ) ;
20
- } ) ;
21
-
22
- generateTypes ( componentNames , dir ) ;
23
- generateExports ( componentNames , dir ) ;
24
- generateVariants ( componentNames , module ) ;
25
- }
26
-
27
- function toKebabCase ( str : string ) {
28
- return str . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, '$1-$2' ) . toLowerCase ( ) ;
29
- }
30
-
31
- function toPascalCase ( str : string ) {
32
- return str . replace ( / ( ^ \w | - \w ) / g, letter => letter . toUpperCase ( ) ) . replace ( / - / g, '' ) ;
33
- }
34
-
35
- function toCamelCase ( str : string ) {
36
- const res = str . replace ( / - ( [ a - z ] ) / g, ( _ , letter ) => letter . toUpperCase ( ) ) ;
37
- return res . charAt ( 0 ) . toLowerCase ( ) + res . slice ( 1 ) ;
38
- }
2
+ import enquirer from 'enquirer' ;
3
+ import { toCamelCase , toKebabCase , toPascalCase } from './_shared' ;
39
4
40
5
function getVueTemplate ( componentName : string , module : string ) {
41
6
const cpName = toPascalCase ( componentName ) ;
@@ -109,17 +74,18 @@ function generateExports(componentNames: string[], dir: string) {
109
74
fs . writeFileSync ( filePath , template ) ;
110
75
}
111
76
112
- function generateVariants ( componentNames : string [ ] , module : string ) {
113
- const kbName = toKebabCase ( module ) ;
114
- const variantsDir = `packages/variants/src/variants/${ kbName } .ts` ;
77
+ function generateVariants ( componentNames : string [ ] , module : string , dir : string ) {
115
78
let template = `// @unocss-include
116
79
import { tv } from 'tailwind-variants';
117
80
118
81
export const ${ module } Variants = tv({
119
82
slots: {\n` ;
120
83
121
84
componentNames . forEach ( componentName => {
122
- const slotName = toCamelCase ( toCamelCase ( componentName ) . replace ( module , '' ) ) || 'root' ;
85
+ const slotName = toCamelCase ( toCamelCase ( componentName ) . replace ( module , '' ) ) ;
86
+ if ( ! slotName ) {
87
+ return ;
88
+ }
123
89
124
90
template += ` ${ slotName } : '',\n` ;
125
91
} ) ;
@@ -129,11 +95,61 @@ export const ${module}Variants = tv({
129
95
130
96
template += `export type ${ toPascalCase ( module ) } Slots = keyof typeof ${ module } Variants.slots;` ;
131
97
132
- fs . writeFileSync ( variantsDir , template ) ;
98
+ fs . writeFileSync ( dir , template ) ;
133
99
}
134
100
135
- function start ( ) {
136
- getComponentFiles ( 'numberField' ) ;
101
+ interface GenerateOptions {
102
+ module : string ;
103
+ components : string [ ] ;
104
+ }
105
+
106
+ async function resolvePrompt ( ) {
107
+ const { module, components : _components } = await enquirer . prompt < GenerateOptions > ( [
108
+ {
109
+ type : 'input' ,
110
+ name : 'module' ,
111
+ message : '请输入组件模块名称'
112
+ } ,
113
+ {
114
+ type : 'list' ,
115
+ name : 'components' ,
116
+ message : '请输入组件名称'
117
+ }
118
+ ] ) ;
119
+
120
+ const kbName = toKebabCase ( module ) ;
121
+ const dir = `packages/ui/src/components/${ kbName } ` ;
122
+ const variantsDir = `packages/variants/src/variants/${ kbName } .ts` ;
123
+
124
+ const components = _components . map ( component => {
125
+ let name = toKebabCase ( component ) ;
126
+
127
+ if ( ! name . startsWith ( kbName ) ) {
128
+ name = `${ kbName } -${ name } ` ;
129
+ }
130
+
131
+ return name ;
132
+ } ) ;
133
+
134
+ components . unshift ( kbName ) ;
135
+
136
+ return { module, components, dir, variantsDir } ;
137
+ }
138
+
139
+ async function start ( ) {
140
+ const { module, components, dir, variantsDir } = await resolvePrompt ( ) ;
141
+
142
+ if ( ! fs . existsSync ( dir ) ) {
143
+ fs . mkdirSync ( dir , { recursive : true } ) ;
144
+ }
145
+
146
+ generateTypes ( components , dir ) ;
147
+ generateExports ( components , dir ) ;
148
+ generateVariants ( components , module , variantsDir ) ;
149
+
150
+ components . forEach ( component => {
151
+ generateComponent ( component , module , dir ) ;
152
+ } ) ;
137
153
}
138
154
139
155
start ( ) ;
0 commit comments