11/* eslint-disable no-console */
2- import fg from 'fast-glob'
32import path from 'node:path'
43import fs from 'node:fs'
54
@@ -20,7 +19,7 @@ const kebab = (name: string) =>
2019 . map ( ( n ) => n [ 0 ] . toUpperCase ( ) + n . slice ( 1 ) )
2120 . join ( '' )
2221
23- // vars.less的所有文件夹, 类似 [ 'action-menu/vars.less', 'alert/vars.less',.........]
22+ // 1、 vars.less的所有文件夹, 类似 [ 'action-menu/vars.less', 'alert/vars.less',.........]
2423fg . sync ( [ '**/vars.less' ] , { cwd : srcFold , ignore : ignoreFolds } )
2524 . map ( ( file ) => file . split ( '/' ) [ 0 ] ) // 取出组件名
2625 . filter ( ( name ) => fs . existsSync ( $smb ( name ) ) ) // 过滤掉不存在在 smb-theme.js的
@@ -41,6 +40,14 @@ fg.sync(['**/vars.less'], { cwd: srcFold, ignore: ignoreFolds })
4140 exceptions . push ( { name, reason : '替换组件变量,写入2个文件时出错!' + error . toString ( ) } )
4241 }
4342 } )
43+
44+ // 2、 替换 base/basic-var.less 与 theme/smb-theme/index.js 中的变量
45+ const varsFile = fs . readFileSync ( path . join ( srcFold , 'base/basic-var.less' ) , 'utf8' )
46+ const smbThemeJsObj = await import ( 'file://' + path . join ( srcFold , 'theme/smb-theme/index.js' ) )
47+ const smbJs = Object . values ( smbThemeJsObj ) [ 0 ]
48+
49+ processComponent ( 'base' , varsFile , smbJs , true )
50+
4451console . log ( '/////执行完毕/////' )
4552console . table ( exceptions )
4653
@@ -53,7 +60,7 @@ interface QueryInfo {
5360 varsLost : boolean
5461}
5562// 处理一个组件的交换
56- function processComponent ( name : string , varsFile : string , smbJs : any ) {
63+ function processComponent ( name : string , varsFile : string , smbJs : any , isBase = false ) {
5764 const smbKeys = Object . keys ( smbJs )
5865 // 记录所有替换信息
5966 const result : QueryInfo [ ] = [ ]
@@ -65,7 +72,7 @@ function processComponent(name: string, varsFile: string, smbJs: any) {
6572
6673 if ( start === - 1 ) {
6774 info . varsLost = true
68- exceptions . push ( { name, reason : 'smb-theme中定义的变量错误, vars.less中找不到' } )
75+ exceptions . push ( { name, reason : 'smb-theme中定义的变量错误, vars.less中找不到:' + key } )
6976 return
7077 }
7178
@@ -83,16 +90,27 @@ function processComponent(name: string, varsFile: string, smbJs: any) {
8390 const end = varsFile . substring ( info . varsEnd )
8491 varsFile = `${ start } ${ info . smbValue } ${ end } `
8592 } )
86- fs . writeFileSync ( $vars ( name ) , varsFile )
93+
94+ fs . writeFileSync ( isBase ? path . join ( srcFold , 'base/basic-var.less' ) : $vars ( name ) , varsFile )
8795
8896 // 3、 根据result, 生成old-theme.js
8997 const oldObj = result . reduce ( ( pre , curr ) => {
9098 pre [ curr . key ] = curr . varsValue
9199 return pre
92100 } , { } )
93101
94- fs . writeFileSync ( $old ( name ) , `export const tiny${ kebab ( name ) } OldTheme = ${ JSON . stringify ( oldObj , null , 2 ) } ` )
102+ if ( ! isBase ) {
103+ fs . writeFileSync ( $old ( name ) , `export const tiny${ kebab ( name ) } OldTheme = ${ JSON . stringify ( oldObj , null , 2 ) } ` )
104+ } else {
105+ // old-theme.js 似乎不太能用
106+ fs . writeFileSync (
107+ path . join ( srcFold , 'base/old-theme.js' ) ,
108+ `export const tiny${ kebab ( name ) } OldTheme = ${ JSON . stringify ( oldObj , null , 2 ) } `
109+ )
110+ }
95111
96112 // 4、 删除smb-theme.js
97- fs . rmSync ( $smb ( name ) )
113+ if ( ! isBase ) {
114+ fs . rmSync ( $smb ( name ) )
115+ }
98116}
0 commit comments