1
- import { pascalCase , kebabCase } from '@tool-pack/basic' ;
1
+ /* eslint-disable perfectionist/sort-objects */
2
+ import { pascalCase , kebabCase , camelCase } from '@tool-pack/basic' ;
2
3
import { prompt } from 'enquirer' ;
3
4
import * as Path from 'path' ;
4
5
import Fse from 'fs-extra' ;
@@ -8,11 +9,15 @@ import Fs from 'fs';
8
9
type InitRes = [ filename : string , content : string ] ;
9
10
10
11
const rootPath = Path . resolve ( __dirname , '../' ) ;
12
+ const importInsertTarget = '/* {import insert target} */' ;
13
+ const exportInsertTarget = '/* {export insert target} */' ;
11
14
12
15
const config = {
13
16
componentsPath : Path . resolve ( rootPath , 'packages/components/src' ) ,
17
+ // pascalCase
14
18
componentName : '' ,
15
19
alias : '' ,
20
+ // kebabCase
16
21
name : '' ,
17
22
} ;
18
23
async function run ( ) {
@@ -28,13 +33,77 @@ async function run() {
28
33
Utils . writeFile ( ...Actions . initDoc ( ) ) ;
29
34
Utils . writeFile ( ...Actions . initTest ( ) ) ;
30
35
Actions . appendIndex ( ) ;
36
+ Actions . initLocale ( ) ;
31
37
console . log ( chalk . cyan ( '添加组件成功' ) ) ;
32
38
} catch ( e ) {
33
39
console . log ( chalk . grey ( '添加组件失败,因为:' , ( e as Error ) . message ) ) ;
34
40
}
35
41
}
36
42
37
43
const Actions = {
44
+ initLocale ( ) {
45
+ addLocaleFiles ( ) ;
46
+ appendToReactUILocale ( ) ;
47
+ appendToConfigProvider ( ) ;
48
+
49
+ function addLocaleFiles ( ) {
50
+ const localeType = `${ pascalCase ( config . name ) } Locale` ;
51
+ const content = `
52
+ import type { ${ localeType } } from '../${ config . name } .types';
53
+
54
+ const locale: ${ localeType } = {
55
+ attr: '...',
56
+ };
57
+
58
+ export default locale;
59
+ ` . trim ( ) ;
60
+ Utils . writeFile ( 'locale/en-US.ts' , content ) ;
61
+ Utils . writeFile ( 'locale/zh-CN.ts' , content ) ;
62
+ }
63
+ function appendToReactUILocale ( ) {
64
+ const localeDirPath = Path . resolve (
65
+ __dirname ,
66
+ '../packages/react-ui/src/locale' ,
67
+ ) ;
68
+
69
+ insert ( 'en-US' ) ;
70
+ insert ( 'zh-CN' ) ;
71
+
72
+ function insert ( type : string ) {
73
+ const filePath = Path . resolve ( localeDirPath , type + '.ts' ) ;
74
+ let content = Fs . readFileSync ( filePath , 'utf-8' ) ;
75
+ const name = camelCase ( config . componentName ) ;
76
+ content = content . replace (
77
+ importInsertTarget ,
78
+ `import ${ name } from '@pkg/components/${ config . name } /locale/${ type } ';\n${ importInsertTarget } ` ,
79
+ ) ;
80
+ content = content . replace (
81
+ exportInsertTarget ,
82
+ `${ name } ,\n ${ exportInsertTarget } ` ,
83
+ ) ;
84
+ Fs . writeFileSync ( filePath , content ) ;
85
+ }
86
+ }
87
+ function appendToConfigProvider ( ) {
88
+ const typesPath = Path . resolve (
89
+ config . componentsPath ,
90
+ 'config-provider/config-provider.types.ts' ,
91
+ ) ;
92
+ let content = Fs . readFileSync ( typesPath , 'utf-8' ) ;
93
+ const localeName = config . componentName + 'Locale' ;
94
+ content = content . replace (
95
+ importInsertTarget ,
96
+ `${ localeName } ,\n ${ importInsertTarget } ` ,
97
+ ) ;
98
+ content = content . replace (
99
+ exportInsertTarget ,
100
+ `${ camelCase (
101
+ config . componentName ,
102
+ ) } : Partial<${ localeName } >;\n ${ exportInsertTarget } `,
103
+ ) ;
104
+ Fs . writeFileSync ( typesPath , content ) ;
105
+ }
106
+ } ,
38
107
appendIndex ( ) {
39
108
const tsContent = `export * from './${ config . name } ';\n` ;
40
109
Fse . appendFileSync ( Utils . getPkgPath ( Utils . getFilename ( 'index' ) ) , tsContent ) ;
@@ -60,7 +129,7 @@ const Actions = {
60
129
) ;
61
130
const routerContent = Fs . readFileSync ( routerPath , 'utf-8' ) ;
62
131
63
- const insertTarget = '/*insert target*/' ;
132
+ const insertTarget = importInsertTarget ;
64
133
const insertContent = `{
65
134
element: getDemos(import.meta.glob('~/${ config . name } /demo/*.tsx')),
66
135
name: '${ config . name } ${ config . alias } ',
@@ -79,13 +148,15 @@ const Actions = {
79
148
const filename = Utils . getFilename ( 'component' ) ;
80
149
const props = `${ componentName } Props` ;
81
150
const content = `
151
+ import { useLocale } from '~/config-provider/useLocale';
82
152
import type { RequiredPart } from '@tool-pack/types';
83
153
import type { ${ props } } from './${ Utils . getFilename ( 'types' ) . replace (
84
154
/ \. t s $ / ,
85
155
'' ,
86
156
) } ';
87
157
import { getClassNames } from '@tool-pack/basic';
88
158
import { getClasses } from '@pkg/shared';
159
+ import EnUS from './locale/en-US';
89
160
import React from 'react';
90
161
91
162
const cls = getClasses('${ config . name } ', [], []);
@@ -95,6 +166,7 @@ export const ${componentName}: React.FC<${props}> = React.forwardRef<
95
166
HTMLDivElement,
96
167
${ props }
97
168
>((props, ref) => {
169
+ const locale = useLocale('${ camelCase ( config . componentName ) } ', EnUS);
98
170
const { attrs = {}, children } = props as RequiredPart<
99
171
${ props } ,
100
172
keyof typeof defaultProps
@@ -225,14 +297,16 @@ import { PropsBase } from '@pkg/shared';
225
297
226
298
export interface ${ props } extends PropsBase<HTMLDivElement> {
227
299
name?: string;
300
+ }
301
+ export interface ${ config . componentName } Locale {
228
302
}
229
303
` ;
230
304
return [ filename , content ] ;
231
305
} ,
232
306
initIndex ( ) : InitRes {
233
307
const filename = Utils . getFilename ( 'index' ) ;
234
308
const content = `
235
- export type { ${ config . componentName } Props } from './${ config . name } .types';
309
+ export type { ${ config . componentName } Locale, ${ config . componentName } Props } from './${ config . name } .types';
236
310
export * from './${ config . componentName } ';
237
311
` ;
238
312
return [ filename , content ] ;
0 commit comments