|
| 1 | +/* eslint-disable no-console */ |
1 | 2 | import fg from 'fast-glob'
|
2 | 3 | import path from 'node:path'
|
| 4 | +import fs from 'node:fs' |
| 5 | + |
| 6 | +interface Exception { |
| 7 | + name: string |
| 8 | + reason: string |
| 9 | +} |
3 | 10 |
|
4 | 11 | const ignoreFolds = ['base', 'theme', 'images', 'mixins', 'svgs', 'transitions', 'vars.less']
|
5 | 12 | const srcFold = path.join(process.cwd(), '../../packages/theme/src')
|
| 13 | +const exceptions = [] as Exception[] |
| 14 | +const $smb = (name: string) => path.join(srcFold, name, 'smb-theme.js') |
| 15 | +const $vars = (name: string) => path.join(srcFold, name, 'vars.less') |
| 16 | +const $old = (name: string) => path.join(srcFold, name, 'old-theme.js') |
| 17 | +const kebab = (name: string) => |
| 18 | + name |
| 19 | + .split('-') |
| 20 | + .map((n) => n[0].toUpperCase() + n.slice(1)) |
| 21 | + .join('') |
6 | 22 |
|
7 | 23 | // vars.less的所有文件夹, 类似 [ 'action-menu/vars.less', 'alert/vars.less',.........]
|
8 |
| -const varsFiles = fg.sync(['**/vars.less'], { cwd: srcFold, ignore: ignoreFolds }) |
9 |
| -// console.log(varsFiles) |
| 24 | +fg.sync(['**/vars.less'], { cwd: srcFold, ignore: ignoreFolds }) |
| 25 | + .map((file) => file.split('/')[0]) // 取出组件名 |
| 26 | + .filter((name) => fs.existsSync($smb(name))) // 过滤掉不存在在 smb-theme.js的 |
| 27 | + .forEach(async (name) => { |
| 28 | + const varsFile = fs.readFileSync($vars(name), 'utf8') |
| 29 | + |
| 30 | + const smbThemeJsObj = await import('file://' + $smb(name)) |
| 31 | + const smbJs = Object.values(smbThemeJsObj)[0] |
| 32 | + |
| 33 | + if (!smbJs) { |
| 34 | + exceptions.push({ name, reason: 'smb-theme.js 异常,未读取到变量值' }) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + processComponent(name, varsFile, smbJs) |
| 40 | + } catch (error: any) { |
| 41 | + exceptions.push({ name, reason: '替换组件变量,写入2个文件时出错!' + error.toString() }) |
| 42 | + } |
| 43 | + }) |
| 44 | +console.log('/////执行完毕/////') |
| 45 | +console.table(exceptions) |
| 46 | + |
| 47 | +interface QueryInfo { |
| 48 | + key: string |
| 49 | + smbValue: string |
| 50 | + varsValue: string |
| 51 | + varsStart: number |
| 52 | + varsEnd: number |
| 53 | + varsLost: boolean |
| 54 | +} |
| 55 | +// 处理一个组件的交换 |
| 56 | +function processComponent(name: string, varsFile: string, smbJs: any) { |
| 57 | + const smbKeys = Object.keys(smbJs) |
| 58 | + // 记录所有替换信息 |
| 59 | + const result: QueryInfo[] = [] |
| 60 | + // 1、遍历 smbKeys 去 vars.less中查找。 生成信息记录在result中 |
| 61 | + smbKeys.forEach((key) => { |
| 62 | + const info: QueryInfo = { key, smbValue: smbJs[key], varsLost: false } as QueryInfo |
| 63 | + const query = `--${key}:` |
| 64 | + const start = varsFile.indexOf(query) |
| 65 | + |
| 66 | + if (start === -1) { |
| 67 | + info.varsLost = true |
| 68 | + exceptions.push({ name, reason: 'smb-theme中定义的变量错误, vars.less中找不到' }) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + info.varsStart = start + query.length |
| 73 | + info.varsEnd = varsFile.indexOf(';', info.varsStart) |
| 74 | + info.varsValue = varsFile.substring(info.varsStart, info.varsEnd).trim() |
| 75 | + result.push(info) |
| 76 | + }) |
| 77 | + |
| 78 | + // 2、根据result 倒排序后, 将smbValue 写入 varsValue的位置 |
| 79 | + result.sort((a, b) => (a.varsStart > b.varsStart ? -1 : 1)) |
| 80 | + |
| 81 | + result.forEach((info) => { |
| 82 | + const start = varsFile.substring(0, info.varsStart) |
| 83 | + const end = varsFile.substring(info.varsEnd) |
| 84 | + varsFile = `${start} ${info.smbValue}${end}` |
| 85 | + }) |
| 86 | + fs.writeFileSync($vars(name), varsFile) |
| 87 | + |
| 88 | + // 3、 根据result, 生成old-theme.js |
| 89 | + const oldObj = result.reduce((pre, curr) => { |
| 90 | + pre[curr.key] = curr.varsValue |
| 91 | + return pre |
| 92 | + }, {}) |
| 93 | + |
| 94 | + fs.writeFileSync($old(name), `export const tiny${kebab(name)}OldTheme = ${JSON.stringify(oldObj, null, 2)}`) |
10 | 95 |
|
11 |
| -const _folds = varsFiles.map((file) => file.split('/')[0]) |
| 96 | + // 4、 删除smb-theme.js |
| 97 | + fs.rmSync($smb(name)) |
| 98 | +} |
0 commit comments