-
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
Conversation
WalkthroughThe changes involve updates to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ThemeSwap
participant ThemeFiles
User->>ThemeSwap: Initiate theme swap
ThemeSwap->>ThemeFiles: Extract variables from smb-theme.js
ThemeSwap->>ThemeFiles: Collect variables from vars.less
ThemeSwap->>User: Provide updated theme variables
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
Outside diff range, codebase verification and nitpick comments (2)
internals/automate/package.json (2)
6-6
: Fill the author field.The
"author"
field is currently empty. Please add the author's name.
8-8
: Add relevant keywords.The
"keywords"
array is currently empty. Please add relevant keywords to improve the discoverability of the package.
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (5)
- internals/automate/package.json (1 hunks)
- internals/automate/src/theme-swap/README.md (1 hunks)
- internals/automate/src/theme-swap/index.ts (1 hunks)
- packages/theme/src/index.less (6 hunks)
- packages/theme/src/vars.less (2 hunks)
Files skipped from review due to trivial changes (1)
- internals/automate/src/theme-swap/index.ts
Additional comments not posted (9)
internals/automate/src/theme-swap/README.md (4)
1-1
: LGTM!The title is appropriate for the content of the file.
3-4
: LGTM!The instructions are clear and provide a good overview of the steps involved in replacing theme variables.
6-8
: LGTM!The exclusions are clear and provide a good overview of the folders that should not be processed.
10-18
: LGTM!The exceptions and post-processing instructions are clear and provide a good overview of the additional steps involved in replacing theme variables.
internals/automate/package.json (4)
7-7
: LGTM!The code changes are approved.
26-29
: LGTM!The code changes are approved.
31-33
: Also applies to: 36-37
18-18
: Verify the theme-swap script.Please ensure that the
./src/theme-swap/index.ts
file exists and the theme-swapping functionality works as expected.Run the following script to verify the existence of the theme-swap script:
Verification successful
Theme-swap script file verified.
The
./internals/automate/src/theme-swap/index.ts
file exists, confirming the validity of thetheme-swap
script in thepackage.json
. No further action is required.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the existence of the theme-swap script. # Test: Check if the theme-swap script file exists. Expect: The file should exist. fd -e ts -x echo {} theme-swapLength of output: 219820
Script:
#!/bin/bash # Description: Verify the existence of the specific theme-swap script file. # Test: Check if the specific theme-swap script file exists. Expect: The file should exist. fd -e ts -p 'index.ts' -x echo {} | grep 'theme-swap'Length of output: 98
packages/theme/src/index.less (1)
Line range hint
1-170
: Verify the removal of UI component imports.The following import statements have been removed:
@import './gantt/index.less'; @import './menubar/index.less'; @import './pbi/index.less'; @import './poplist/index.less'; @import './selector/index.less'; @import './time/index.less'; @import './tip/index.less';This suggests that the corresponding UI components (
gantt
,menubar
,pbi
,poplist
,selector
,time
, andtip
) are no longer needed or have been relocated.Please verify the following:
Are these components no longer used in the application? If they are still in use, removing the imports may break the existing functionality.
If the components have been relocated, ensure that the new location is properly referenced in the code.
You can use the following script to search for the usage of these components in the codebase:
If the script returns no results, it confirms that the components are no longer used in the codebase, and removing the imports is safe. Otherwise, please ensure that the removal does not introduce any breaking changes.
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- internals/automate/src/theme-swap/README.md (1 hunks)
- internals/automate/src/theme-swap/index.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- internals/automate/src/theme-swap/README.md
Additional comments not posted (1)
internals/automate/src/theme-swap/index.ts (1)
1-11
: LGTM!The code segment is well-structured, follows best practices, and efficiently finds the required files using
fast-glob
. The ignored folders are clearly defined, and the code is properly formatted according to TypeScript conventions.
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.
Actionable comments posted: 6
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- internals/automate/src/theme-swap/index.ts (1 hunks)
Additional comments not posted (1)
internals/automate/src/theme-swap/index.ts (1)
6-9
: Interface definition is clear and concise.The
Exception
interface is well-defined with appropriate fields forname
andreason
. This is good practice for error handling within the script.
/* eslint-disable no-console */ | ||
import fg from 'fast-glob' | ||
import path from 'node:path' | ||
import fs from 'node:fs' |
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
, 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.
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) => |
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.
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.
// 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) => { |
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.
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.
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)) | ||
} |
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.
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.
// 4、 删除smb-theme.js | ||
fs.rmSync($smb(name)) | ||
} |
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 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.
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() }) | ||
} |
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.
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.
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- internals/automate/src/theme-swap/README.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- internals/automate/src/theme-swap/README.md
[e2e-test-warn] The title of the Pull request should look like "fix(vue-renderless): [action-menu, alert] fix xxx bug". Please make sure you've read our contributing guide |
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
README.md
file with detailed instructions for replacing theme variables in Vue.js projects.Bug Fixes
Chores
package.json
file for better organization, including new fields for author, license, and keywords.