Skip to content

Commit 2f4a926

Browse files
committed
chore(scripts): 调整 new-component 脚本,添加 locale 预设
1 parent 05167c2 commit 2f4a926

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-7
lines changed

internal/playground/src/router.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export const baseRouter = [
181181
name: 'virtual-list 虚拟列表',
182182
path: '/virtual-list',
183183
},
184-
/*insert target*/
184+
/* {import insert target} */
185185
];
186186

187187
export const router = createBrowserRouter([

packages/components/src/config-provider/config-provider.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
SelectLocale,
66
InputLocale,
77
EmptyLocale,
8+
/* {import insert target} */
89
} from '../';
910

1011
export interface Locale {
@@ -14,6 +15,7 @@ export interface Locale {
1415
select: Partial<SelectLocale>;
1516
input: Partial<InputLocale>;
1617
empty: Partial<EmptyLocale>;
18+
/* {export insert target} */
1719
}
1820

1921
export type Context = { locale: Partial<Locale> };

packages/react-ui/src/locale/en-US.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import calendar from '@pkg/components/calendar/locale/en-US';
44
import select from '@pkg/components/select/locale/en-US';
55
import input from '@pkg/components/input/locale/en-US';
66
import empty from '@pkg/components/empty/locale/en-US';
7+
/* {import insert target} */
78
import type { Locale } from '@pkg/components';
89

910
export default {
@@ -13,4 +14,5 @@ export default {
1314
select,
1415
empty,
1516
input,
17+
/* {export insert target} */
1618
} satisfies Locale;

packages/react-ui/src/locale/zh-CN.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import popConfirm from '@pkg/components/pop-confirm/locale/zh-CN';
2+
import datePicker from '@pkg/components/date-picker/locale/zh-CN';
23
import calendar from '@pkg/components/calendar/locale/zh-CN';
4+
import select from '@pkg/components/select/locale/zh-CN';
5+
import input from '@pkg/components/input/locale/zh-CN';
36
import empty from '@pkg/components/empty/locale/zh-CN';
4-
import datePicker from '~/date-picker/locale/zh-CN';
7+
/* {import insert target} */
58
import type { Locale } from '@pkg/components';
6-
import select from '~/select/locale/zh-CN';
7-
import input from '~/input/locale/zh-CN';
89

910
export default {
1011
popConfirm,
@@ -13,4 +14,5 @@ export default {
1314
select,
1415
empty,
1516
input,
17+
/* {export insert target} */
1618
} satisfies Locale;

scripts/new-component.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { pascalCase, kebabCase } from '@tool-pack/basic';
1+
/* eslint-disable perfectionist/sort-objects */
2+
import { pascalCase, kebabCase, camelCase } from '@tool-pack/basic';
23
import { prompt } from 'enquirer';
34
import * as Path from 'path';
45
import Fse from 'fs-extra';
@@ -8,11 +9,15 @@ import Fs from 'fs';
89
type InitRes = [filename: string, content: string];
910

1011
const rootPath = Path.resolve(__dirname, '../');
12+
const importInsertTarget = '/* {import insert target} */';
13+
const exportInsertTarget = '/* {export insert target} */';
1114

1215
const config = {
1316
componentsPath: Path.resolve(rootPath, 'packages/components/src'),
17+
// pascalCase
1418
componentName: '',
1519
alias: '',
20+
// kebabCase
1621
name: '',
1722
};
1823
async function run() {
@@ -28,13 +33,77 @@ async function run() {
2833
Utils.writeFile(...Actions.initDoc());
2934
Utils.writeFile(...Actions.initTest());
3035
Actions.appendIndex();
36+
Actions.initLocale();
3137
console.log(chalk.cyan('添加组件成功'));
3238
} catch (e) {
3339
console.log(chalk.grey('添加组件失败,因为:', (e as Error).message));
3440
}
3541
}
3642

3743
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+
},
38107
appendIndex() {
39108
const tsContent = `export * from './${config.name}';\n`;
40109
Fse.appendFileSync(Utils.getPkgPath(Utils.getFilename('index')), tsContent);
@@ -60,7 +129,7 @@ const Actions = {
60129
);
61130
const routerContent = Fs.readFileSync(routerPath, 'utf-8');
62131

63-
const insertTarget = '/*insert target*/';
132+
const insertTarget = importInsertTarget;
64133
const insertContent = `{
65134
element: getDemos(import.meta.glob('~/${config.name}/demo/*.tsx')),
66135
name: '${config.name} ${config.alias}',
@@ -79,13 +148,15 @@ const Actions = {
79148
const filename = Utils.getFilename('component');
80149
const props = `${componentName}Props`;
81150
const content = `
151+
import { useLocale } from '~/config-provider/useLocale';
82152
import type { RequiredPart } from '@tool-pack/types';
83153
import type { ${props} } from './${Utils.getFilename('types').replace(
84154
/\.ts$/,
85155
'',
86156
)}';
87157
import { getClassNames } from '@tool-pack/basic';
88158
import { getClasses } from '@pkg/shared';
159+
import EnUS from './locale/en-US';
89160
import React from 'react';
90161
91162
const cls = getClasses('${config.name}', [], []);
@@ -95,6 +166,7 @@ export const ${componentName}: React.FC<${props}> = React.forwardRef<
95166
HTMLDivElement,
96167
${props}
97168
>((props, ref) => {
169+
const locale = useLocale('${camelCase(config.componentName)}', EnUS);
98170
const { attrs = {}, children } = props as RequiredPart<
99171
${props},
100172
keyof typeof defaultProps
@@ -225,14 +297,16 @@ import { PropsBase } from '@pkg/shared';
225297
226298
export interface ${props} extends PropsBase<HTMLDivElement> {
227299
name?: string;
300+
}
301+
export interface ${config.componentName}Locale {
228302
}
229303
`;
230304
return [filename, content];
231305
},
232306
initIndex(): InitRes {
233307
const filename = Utils.getFilename('index');
234308
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';
236310
export * from './${config.componentName}';
237311
`;
238312
return [filename, content];

0 commit comments

Comments
 (0)