-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(theme): create theme-swap.js, clean theme folder unused files #2037
Changes from all commits
8c5a1ac
713a191
2ef1d00
a907b86
4b44cfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
{ | ||
"name": "@opentiny/automate", | ||
"type": "module", | ||
"version": "1.0.0", | ||
"description": "", | ||
"author": "", | ||
"license": "ISC", | ||
"keywords": [], | ||
"main": "./src/index.ts", | ||
"type": "module", | ||
"scripts": { | ||
"start": "esno ./src/index.ts", | ||
"scan": "esno ./src/demos-scan.ts", | ||
"// ---------- 给文档组件加上Tiny前缀 ----------": "", | ||
"doc-prefix": "esno ./src/doc-prefix.ts", | ||
"diff": "esno ./src/aui-diff.ts", | ||
"add-dep": "esno ./src/add-dependencies.ts", | ||
"auto-build-pub": "esno ./src/publish/index.ts" | ||
"auto-build-pub": "esno ./src/publish/index.ts", | ||
"theme-swap": "esno ./src/theme-swap/index.ts" | ||
}, | ||
"dependencies": { | ||
"esno": "^4.7.0", | ||
"fast-glob": "^3.2.12", | ||
"fs-extra": "^11.2.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/generator": "^7.18.13", | ||
"@babel/parser": "^7.18.13", | ||
"@babel/template": "^7.18.13", | ||
"@babel/traverse": "^7.18.13", | ||
"@types/node": "^20.10.5", | ||
"@vue/compiler-sfc": "^3.4.21", | ||
"chalk": "2.4.2", | ||
"commander": "^10.0.0", | ||
"lodash-es": "^4.17.21", | ||
"sheetjs-style": "^0.15.8", | ||
"commander": "^10.0.0", | ||
"semver": "^7.3.8", | ||
"chalk": "2.4.2", | ||
"@babel/parser": "^7.18.13", | ||
"@babel/traverse": "^7.18.13", | ||
"@babel/generator": "^7.18.13", | ||
"@babel/template": "^7.18.13", | ||
"@vue/compiler-sfc": "^3.4.21" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
"semver": "^7.3.8" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# vue-theme 替换主题变量 | ||
|
||
- 将 `smb-theme.js` 中的变量值为默认值,smb-theme中有大量的px, 颜色值,都原样复制即可! | ||
- 将`vars.less`中要被替换的变量收集起来,另存到`old-theme.js`中去. | ||
|
||
- 不处理 `base/theme` 文件夹。 | ||
- 不处理 `images/mixins/svgs` 文件夹。 | ||
- 不处理 `transition` 文件夹。 | ||
|
||
## 例外处理 | ||
|
||
- 删除空样式的目录; 没有`smb-theme.js`的,忽略处理。 | ||
- `smb-theme.js`中有的, `vars.less`中没有-----------------打印警告 | ||
|
||
## 处理后待做 | ||
|
||
`base`中的变量与 `theme/smb-theme`的变量手工复制替换。 | ||
手工校对是否common变量有差异! | ||
|
||
## 命令 | ||
|
||
npm run theme-swap |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* eslint-disable no-console */ | ||
import fg from 'fast-glob' | ||
import path from 'node:path' | ||
import fs from 'node:fs' | ||
|
||
interface Exception { | ||
name: string | ||
reason: string | ||
} | ||
|
||
const ignoreFolds = ['base', 'theme', 'images', 'mixins', 'svgs', 'transitions', 'vars.less'] | ||
const srcFold = path.join(process.cwd(), '../../packages/theme/src') | ||
const exceptions = [] as Exception[] | ||
const $smb = (name: string) => path.join(srcFold, name, 'smb-theme.js') | ||
const $vars = (name: string) => path.join(srcFold, name, 'vars.less') | ||
const $old = (name: string) => path.join(srcFold, name, 'old-theme.js') | ||
const kebab = (name: string) => | ||
Comment on lines
+11
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential issue with hardcoded paths and names. The |
||
name | ||
.split('-') | ||
.map((n) => n[0].toUpperCase() + n.slice(1)) | ||
.join('') | ||
|
||
// vars.less的所有文件夹, 类似 [ 'action-menu/vars.less', 'alert/vars.less',.........] | ||
fg.sync(['**/vars.less'], { cwd: srcFold, ignore: ignoreFolds }) | ||
.map((file) => file.split('/')[0]) // 取出组件名 | ||
.filter((name) => fs.existsSync($smb(name))) // 过滤掉不存在在 smb-theme.js的 | ||
.forEach(async (name) => { | ||
Comment on lines
+23
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Efficient use of The use of |
||
const varsFile = fs.readFileSync($vars(name), 'utf8') | ||
|
||
const smbThemeJsObj = await import('file://' + $smb(name)) | ||
const smbJs = Object.values(smbThemeJsObj)[0] | ||
|
||
if (!smbJs) { | ||
exceptions.push({ name, reason: 'smb-theme.js 异常,未读取到变量值' }) | ||
return | ||
} | ||
|
||
try { | ||
processComponent(name, varsFile, smbJs) | ||
} catch (error: any) { | ||
exceptions.push({ name, reason: '替换组件变量,写入2个文件时出错!' + error.toString() }) | ||
} | ||
Comment on lines
+28
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Asynchronous operation within a synchronous loop. The use of - .forEach(async (name) => {
+ for (const name of names) {
|
||
}) | ||
console.log('/////执行完毕/////') | ||
console.table(exceptions) | ||
|
||
interface QueryInfo { | ||
key: string | ||
smbValue: string | ||
varsValue: string | ||
varsStart: number | ||
varsEnd: number | ||
varsLost: boolean | ||
} | ||
// 处理一个组件的交换 | ||
function processComponent(name: string, varsFile: string, smbJs: any) { | ||
const smbKeys = Object.keys(smbJs) | ||
// 记录所有替换信息 | ||
const result: QueryInfo[] = [] | ||
// 1、遍历 smbKeys 去 vars.less中查找。 生成信息记录在result中 | ||
smbKeys.forEach((key) => { | ||
const info: QueryInfo = { key, smbValue: smbJs[key], varsLost: false } as QueryInfo | ||
const query = `--${key}:` | ||
const start = varsFile.indexOf(query) | ||
|
||
if (start === -1) { | ||
info.varsLost = true | ||
exceptions.push({ name, reason: 'smb-theme中定义的变量错误, vars.less中找不到' }) | ||
return | ||
} | ||
|
||
info.varsStart = start + query.length | ||
info.varsEnd = varsFile.indexOf(';', info.varsStart) | ||
info.varsValue = varsFile.substring(info.varsStart, info.varsEnd).trim() | ||
result.push(info) | ||
}) | ||
|
||
// 2、根据result 倒排序后, 将smbValue 写入 varsValue的位置 | ||
result.sort((a, b) => (a.varsStart > b.varsStart ? -1 : 1)) | ||
|
||
result.forEach((info) => { | ||
const start = varsFile.substring(0, info.varsStart) | ||
const end = varsFile.substring(info.varsEnd) | ||
varsFile = `${start} ${info.smbValue}${end}` | ||
}) | ||
fs.writeFileSync($vars(name), varsFile) | ||
|
||
// 3、 根据result, 生成old-theme.js | ||
const oldObj = result.reduce((pre, curr) => { | ||
pre[curr.key] = curr.varsValue | ||
return pre | ||
}, {}) | ||
|
||
fs.writeFileSync($old(name), `export const tiny${kebab(name)}OldTheme = ${JSON.stringify(oldObj, null, 2)}`) | ||
|
||
// 4、 删除smb-theme.js | ||
fs.rmSync($smb(name)) | ||
} | ||
Comment on lines
+47
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complex function with multiple responsibilities. The
Comment on lines
+96
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider implications of deleting files programmatically. The script deletes |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider handling potential import errors.
The imports from
fast-glob
,node:path
, andnode:fs
are crucial for the functionality of this script. Consider adding error handling or checks to ensure these modules are available, which will prevent runtime errors in environments where these modules might be missing or versions are incompatible.