Skip to content
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

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions internals/automate/package.json
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"
}
}
22 changes: 22 additions & 0 deletions internals/automate/src/theme-swap/README.md
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
98 changes: 98 additions & 0 deletions internals/automate/src/theme-swap/index.ts
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'
Comment on lines +1 to +4
Copy link

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, and node: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.


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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue with hardcoded paths and names.

The ignoreFolds array and the srcFold constant are hardcoded, which could limit the flexibility of this script in different environments or projects. Consider making these configurable through environment variables or command-line arguments to enhance the script's usability across different setups.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Efficient use of fast-glob but lacks error handling.

The use of fast-glob to filter directories and files is efficient. However, there's no error handling if the glob pattern fails or if the directories are inaccessible. Adding error handling here would improve the robustness of the script.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asynchronous operation within a synchronous loop.

The use of async within a forEach loop is not effective because forEach cannot handle asynchronous operations properly. This could lead to unhandled promises or race conditions. Consider using for...of loop or Promise.all with map to handle asynchronous operations correctly.

- .forEach(async (name) => {
+ for (const name of names) {

Committable suggestion was skipped due to low confidence.

})
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complex function with multiple responsibilities.

The processComponent function is doing too much: reading files, processing data, writing files, and even deleting files. This violates the Single Responsibility Principle (SRP). Consider breaking this function into smaller, more manageable functions that each handle one specific task. Additionally, the function lacks comprehensive error handling, which could lead to partial updates or data corruption in case of failures.

Comment on lines +96 to +98
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider implications of deleting files programmatically.

The script deletes smb-theme.js files after processing. Ensure that there is a backup mechanism or a way to recover these files if needed, as this operation is irreversible and could lead to data loss.

15 changes: 0 additions & 15 deletions packages/theme/src/area/index.js

This file was deleted.

31 changes: 0 additions & 31 deletions packages/theme/src/bulletin-board/index.js

This file was deleted.

38 changes: 0 additions & 38 deletions packages/theme/src/calendar/index.js

This file was deleted.

15 changes: 0 additions & 15 deletions packages/theme/src/card-container/index.js

This file was deleted.

27 changes: 0 additions & 27 deletions packages/theme/src/card-item/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions packages/theme/src/card-layout/index.js

This file was deleted.

30 changes: 0 additions & 30 deletions packages/theme/src/card-template/index.js

This file was deleted.

19 changes: 0 additions & 19 deletions packages/theme/src/carousel-item/index.js

This file was deleted.

17 changes: 0 additions & 17 deletions packages/theme/src/chart-core/index.js

This file was deleted.

Loading
Loading