11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
33
4+ const COMPONENT_NAME = process . argv [ process . argv . indexOf ( '--NAME' ) + 1 ] ; // 在 --NAME 后面
5+
46const combine = {
57 avatar : [ 'avatar-group' , 'avatar' ] ,
68 cell : [ 'cell-group' , 'cell' ] ,
@@ -17,52 +19,110 @@ const combine = {
1719 tabs : [ 'tabs' , 'tab-panel' ] ,
1820 'tab-bar' : [ 'tab-bar' , 'tab-bar-item' ] ,
1921 grid : [ 'grid' , 'grid-item' ] ,
22+ layout : [ 'row' , 'col' ] ,
23+ form : [ 'form' , 'form-item' ] ,
2024} ;
2125
22- function resolveCwd ( ...args ) {
26+ const resolveCwd = ( ...args ) => {
2327 args . unshift ( process . cwd ( ) ) ;
2428 return path . join ( ...args ) ;
25- }
29+ } ;
2630
27- const COMPONENT_NAME = process . argv [ process . argv . indexOf ( '--NAME' ) + 1 ] ; // 在 --NAME 后面
31+ const findFilePath = ( componentName ) => resolveCwd ( `src/ ${ componentName } / ${ componentName } .less` ) ;
2832
29- const matchReg = / (?< = v a r ) .* ?(? = ; ) / g;
33+ const getAllComponentName = async ( dirPath ) => {
34+ const items = await fs . promises . readdir ( dirPath , { withFileTypes : true } ) ;
35+ return items . filter ( ( item ) => item . isDirectory ( ) ) . map ( ( item ) => item . name ) ;
36+ } ;
3037
31- // 使用 v2 文件夹下 _var.less 文件
32- const lessPath = [ ] ;
33- if ( combine [ COMPONENT_NAME ] ) {
34- combine [ COMPONENT_NAME ] . forEach ( ( item ) => {
35- lessPath . push ( resolveCwd ( `src/${ item } /${ item } .less` ) ) ;
36- } ) ;
37- } else {
38- lessPath . push ( resolveCwd ( `src/${ COMPONENT_NAME } /${ COMPONENT_NAME } .less` ) ) ;
39- }
40-
41- // 追加到文件
42- const cssVariableHeadContent = `\n\n### CSS Variables\n\n组件提供了下列 CSS 变量,可用于自定义样式。\n名称 | 默认值 | 描述 \n-- | -- | --\n` ;
43- const cssVariableHeadContentEn = `\n\n### CSS Variables\n\nThe component provides the following CSS variables, which can be used to customize styles.\nName | Default Value | Description \n-- | -- | --\n` ;
44-
45- fs . appendFileSync ( resolveCwd ( `src/${ COMPONENT_NAME } /README.md` ) , cssVariableHeadContent ) ;
46- fs . appendFileSync ( resolveCwd ( `src/${ COMPONENT_NAME } /README.en-US.md` ) , cssVariableHeadContentEn ) ;
47-
48- // 读取 less 文件内容
49- lessPath . forEach ( ( item ) => {
50- if ( fs . existsSync ( item ) ) {
51- fs . readFile ( item , 'utf8' , ( err , file ) => {
52- if ( err ) {
53- console . log ( 'please execute npm run update:css first!' , err ) ;
54- return ;
55- }
56- const list = file . match ( matchReg ) ?. sort ( ) ;
57- let cssVariableBodyContent = '' ;
58- list ?. forEach ( ( item ) => {
59- cssVariableBodyContent += `${ item . slice ( 1 , item . indexOf ( ',' ) ) } | ${ item . slice (
60- item . indexOf ( ',' ) + 2 ,
61- item . length - 1 ,
62- ) } | - \n`;
63- } ) ;
64- fs . appendFileSync ( resolveCwd ( `src/${ COMPONENT_NAME } /README.md` ) , cssVariableBodyContent ) ;
65- fs . appendFileSync ( resolveCwd ( `src/${ COMPONENT_NAME } /README.en-US.md` ) , cssVariableBodyContent ) ;
38+ const generateCssVariables = async ( componentName ) => {
39+ const lessPath = [ ] ;
40+
41+ let cssVariableBodyContent = '' ;
42+
43+ if ( combine [ componentName ] ) {
44+ combine [ componentName ] . forEach ( ( item ) => {
45+ lessPath . push ( findFilePath ( item ) ) ;
6646 } ) ;
47+ } else {
48+ lessPath . push ( findFilePath ( componentName ) ) ;
6749 }
68- } ) ;
50+
51+ const validPaths = lessPath . filter ( ( item ) => fs . existsSync ( item ) ) ;
52+
53+ // 使用 fs.promises.readFile 并行读取文件
54+ const fileContents = await Promise . all ( validPaths . map ( ( item ) => fs . promises . readFile ( item , 'utf8' ) ) ) ;
55+
56+ fileContents . forEach ( ( file ) => {
57+ const matchReg = / (?< = v a r ) [ \s \S ] * ?(? = ; ) / g;
58+
59+ const list = file . match ( matchReg ) ?. sort ( ) ;
60+
61+ list ?. forEach ( ( item ) => {
62+ cssVariableBodyContent += `${ item . slice ( 1 , item . indexOf ( ',' ) ) . trim ( ) } | ${ item
63+ . slice ( item . indexOf ( ',' ) + 2 , item . length - 1 )
64+ . trim ( ) } | - \n`;
65+ } ) ;
66+ } ) ;
67+
68+ return cssVariableBodyContent ;
69+ } ;
70+
71+ /**
72+ * 替换文档中的 CSS 变量部分
73+ * @param {string } filePath - 文档路径
74+ * @param {string } headContent - 变量表头部内容
75+ * @param {string } variables - 生成的变量内容
76+ */
77+ const updateDocVariables = ( filePath , headContent , variables ) => {
78+ const path = resolveCwd ( filePath ) ;
79+
80+ if ( ! fs . existsSync ( path ) ) return ;
81+
82+ const content = fs . readFileSync ( path , 'utf8' ) ;
83+ const cssVariablesSection = `\n${ headContent } ${ variables } ` ;
84+
85+ // 检查是否存在 ### CSS Variables 部分
86+ if ( content . includes ( '### CSS Variables' ) ) {
87+ // 替换现有部分
88+ const newContent = content . replace ( / ( ^ | \n + ) # # # C S S V a r i a b l e s [ \s \S ] * ?(? = # # # | $ ) / , cssVariablesSection ) ;
89+ fs . writeFileSync ( path , newContent , 'utf8' ) ;
90+ } else {
91+ // 追加到文件末尾
92+ const trimmedContent = content . trimEnd ( ) ;
93+ const newContent = `${ trimmedContent } \n${ cssVariablesSection } ` ;
94+ fs . writeFileSync ( path , newContent , 'utf8' ) ;
95+ }
96+ } ;
97+
98+ // 批量处理所有组件
99+ const processAllComponents = async ( ) => {
100+ const cssVariableHeadContent = `\n### CSS Variables\n\n组件提供了下列 CSS 变量,可用于自定义样式。\n名称 | 默认值 | 描述 \n-- | -- | --\n` ;
101+ const cssVariableHeadContentEn = `\n### CSS Variables\n\nThe component provides the following CSS variables, which can be used to customize styles.\nName | Default Value | Description \n-- | -- | --\n` ;
102+
103+ let COMPONENT_NAMES = [ ] ;
104+ if ( COMPONENT_NAME === 'all' ) {
105+ COMPONENT_NAMES = await getAllComponentName ( resolveCwd ( 'src' ) ) ;
106+ } else {
107+ COMPONENT_NAMES = [ COMPONENT_NAME ] ;
108+ }
109+
110+ // 并行处理所有组件
111+ await Promise . all (
112+ COMPONENT_NAMES . map ( async ( name ) => {
113+ const variables = await generateCssVariables ( name ) ;
114+ if ( variables ) {
115+ updateDocVariables ( `src/${ name } /README.md` , cssVariableHeadContent , variables ) ;
116+ updateDocVariables ( `src/${ name } /README.en-US.md` , cssVariableHeadContentEn , variables ) ;
117+ console . log ( `✅ 组件 "${ name } " 文档更新完成` ) ;
118+ } else {
119+ console . log ( `${ name } " 没有找到 CSS 变量` ) ;
120+ }
121+ } ) ,
122+ ) ;
123+ } ;
124+
125+ // 执行入口
126+ processAllComponents ( ) . catch ( ( err ) =>
127+ console . error ( `${ COMPONENT_NAME === 'all' ? '❌ 批量处理失败:' : `${ COMPONENT_NAME } 处理失败` } ` , err ) ,
128+ ) ;
0 commit comments