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

Conversation

shenjunjian
Copy link
Collaborator

@shenjunjian shenjunjian commented Sep 3, 2024

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

  1. theme-swap 脚本 ,详见 readme.md
  2. 删除原来theme 文件夹中多余的文件和目录 :
  • gantt/ menubar/pbi/poplist/selector/time/tip

Summary by CodeRabbit

  • New Features

    • Introduced a new README.md file with detailed instructions for replacing theme variables in Vue.js projects.
    • Added a new script for theme swapping functionality to enhance theme management capabilities.
  • Bug Fixes

    • Removed unnecessary import statements in the theme package, optimizing styles and improving clarity.
  • Chores

    • Updated the package.json file for better organization, including new fields for author, license, and keywords.

Copy link

coderabbitai bot commented Sep 3, 2024

Walkthrough

The changes involve updates to the package.json file for the @opentiny/automate package, including the addition of new fields and a reorganization of dependencies. A new README.md file was introduced to guide theme variable replacement in a Vue.js project, along with a new index.ts file that implements functionality for managing theme variables. Several import statements were removed or modified in the index.less and vars.less files, indicating a potential refactoring of styles.

Changes

File Change Summary
internals/automate/package.json Updated structure with new fields (author, license, keywords), added theme-swap script, and reorganized devDependencies.
internals/automate/src/theme-swap/README.md New file added with instructions for replacing theme variables in a Vue.js project.
internals/automate/src/theme-swap/index.ts New file added for managing theme variables and processing vars.less files.
packages/theme/src/index.less Removed multiple import statements related to UI components, indicating potential refactoring.
packages/theme/src/vars.less Removed import for menubar/vars.less and modified import for popeditor/vars.less (formatting adjustment).

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
Loading

🐇
In the garden, changes bloom,
New fields sprout, dispelling gloom.
With themes to swap and styles anew,
A joyful hop, a vibrant view!
Let's dance and play, oh what a sight,
For every change brings pure delight! 🌼✨


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between 56e8819 and 8c5a1ac.

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 the theme-swap script in the package.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-swap

Length 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, and tip) are no longer needed or have been relocated.

Please verify the following:

  1. Are these components no longer used in the application? If they are still in use, removing the imports may break the existing functionality.

  2. 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.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between 8c5a1ac and 713a191.

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.

@shenjunjian shenjunjian added the developing 正在开发中 label Sep 4, 2024
Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between 713a191 and a907b86.

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 for name and reason. This is good practice for error handling within the script.

Comment on lines +1 to +4
/* eslint-disable no-console */
import fg from 'fast-glob'
import path from 'node:path'
import fs from 'node:fs'
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.

Comment on lines +11 to +17
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) =>
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.

Comment on lines +23 to +27
// 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) => {
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.

Comment on lines +47 to +98
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))
}
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
// 4、 删除smb-theme.js
fs.rmSync($smb(name))
}
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.

Comment on lines +28 to +42
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() })
}
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.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between a907b86 and 4b44cfd.

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

@shenjunjian shenjunjian changed the title [WIP] fix(theme): create theme-swap.js, clean theme folder unused files fix(theme): create theme-swap.js, clean theme folder unused files Sep 9, 2024
Copy link

github-actions bot commented Sep 9, 2024

[e2e-test-warn]
The component to be tested is missing.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developing 正在开发中
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants