-
Notifications
You must be signed in to change notification settings - Fork 318
chore(chat): docs generate css vars #4077
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/tdesign-miniprogram-chat/site/scripts/generate-css-vars.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import fs from 'fs'; | ||
| import resolveCwd from './utils.mjs'; | ||
|
|
||
| const COMPONENT_NAME = process.argv[process.argv.indexOf('--NAME') + 1]; // 在 --NAME 后面 | ||
| const ROOT_DIR = resolveCwd('../../../packages/pro-components/chat'); | ||
|
|
||
| const combine = { | ||
| 'chat-markdown': [ | ||
| 'chat-markdown', | ||
| 'chat-markdown/components/chat-markdown-code', | ||
| 'chat-markdown/components/chat-markdown-node', | ||
| 'chat-markdown/components/chat-markdown-table', | ||
| ], | ||
| }; | ||
|
|
||
| const findFilePath = (componentPath, componentName) => `${ROOT_DIR}/${componentPath}/${componentName}.less`; | ||
|
|
||
| const getAllComponentName = async (dirPath) => { | ||
| const items = await fs.promises.readdir(dirPath, { withFileTypes: true }); | ||
| return items.filter((item) => item.isDirectory()).map((item) => item.name); | ||
| }; | ||
|
|
||
| const generateCssVariables = async (componentName) => { | ||
|
Collaborator
Author
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. 解析脚本后面挪到common 仓库跟 mobile-vue 和 mobile-react公用 |
||
| const lessPath = []; | ||
|
|
||
| const parsedKeys = []; | ||
| let cssVariableBodyContent = ''; | ||
|
|
||
| if (combine[componentName]) { | ||
| combine[componentName].forEach((item) => { | ||
| lessPath.push(findFilePath(item, item.includes('/') ? item.split('/').pop() : item)); | ||
| }); | ||
| } else { | ||
| lessPath.push(findFilePath(componentName, componentName)); | ||
| } | ||
|
|
||
| const validPaths = lessPath.filter((item) => fs.existsSync(item)); | ||
|
|
||
| // 使用 fs.promises.readFile 并行读取文件 | ||
| const fileContents = await Promise.all(validPaths.map((item) => fs.promises.readFile(item, 'utf8'))); | ||
|
|
||
| fileContents.forEach((file) => { | ||
| const matchReg = /(?<=var)\([\s\S]*?(?=;)/g; | ||
|
|
||
| const list = file.match(matchReg)?.sort(); | ||
|
|
||
| list?.forEach((item) => { | ||
| const key = item.slice(1, item.indexOf(',')).trim(); | ||
| const value = item.slice(item.indexOf(',') + 2, item.length - 1).trim(); | ||
| if (!key || !value) { | ||
| throw new Error('⚠️ 解析失败,请检查 less 文件'); | ||
| } | ||
| if (!parsedKeys.includes(key)) { | ||
| parsedKeys.push(key); | ||
| cssVariableBodyContent += `${key} | ${value} | -${'\n'}`; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| return cssVariableBodyContent.trimEnd(); | ||
| }; | ||
|
|
||
| /** | ||
| * 替换文档中的 CSS 变量部分 | ||
| * @param {string} filePath - 文档路径 | ||
| * @param {string} headContent - 变量表头部内容 | ||
| * @param {string} variables - 生成的变量内容 | ||
| */ | ||
| const updateDocVariables = (filePath, headContent, variables) => { | ||
| if (!fs.existsSync(filePath)) return; | ||
|
|
||
| const content = fs.readFileSync(filePath, 'utf8'); | ||
| const cssVariablesSection = `\n${headContent}${variables}\n`; | ||
|
|
||
| // 检查是否存在 ### CSS Variables 部分 | ||
| if (content.includes('### CSS Variables')) { | ||
| // 替换现有部分 | ||
| const newContent = content.replace(/(^|\n+)### CSS Variables[\s\S]*?(?=###|$)/, cssVariablesSection); | ||
| fs.writeFileSync(filePath, newContent, 'utf8'); | ||
| } else { | ||
| // 追加到文件末尾 | ||
| const trimmedContent = content.trimEnd(); | ||
| const newContent = `${trimmedContent}${cssVariablesSection}\n`; | ||
| fs.writeFileSync(filePath, newContent, 'utf8'); | ||
| } | ||
| }; | ||
|
|
||
| // 批量处理所有组件 | ||
| const processAllComponents = async () => { | ||
| const cssVariableHeadContent = `\n### CSS Variables\n\n组件提供了下列 CSS 变量,可用于自定义样式。\n名称 | 默认值 | 描述 \n-- | -- | --\n`; | ||
| const cssVariableHeadContentEn = `\n### CSS Variables\n\nThe component provides the following CSS variables, which can be used to customize styles.\nName | Default Value | Description \n-- | -- | --\n`; | ||
|
|
||
| let COMPONENT_NAMES = []; | ||
| if (COMPONENT_NAME === 'all') { | ||
| COMPONENT_NAMES = await getAllComponentName(ROOT_DIR); | ||
| } else { | ||
| COMPONENT_NAMES = [COMPONENT_NAME]; | ||
| } | ||
|
|
||
| // 并行处理所有组件 | ||
| await Promise.all( | ||
| COMPONENT_NAMES.map(async (name) => { | ||
| const variables = await generateCssVariables(name); | ||
| if (variables) { | ||
| updateDocVariables(`${ROOT_DIR}/${name}/README.md`, cssVariableHeadContent, variables); | ||
| updateDocVariables(`${ROOT_DIR}/${name}/README.en-US.md`, cssVariableHeadContentEn, variables); | ||
| console.log(`✅ 组件 "${name}" 文档更新完成`); | ||
| } else { | ||
| console.log(`${name}: 没有找到 CSS 变量`); | ||
| } | ||
| }), | ||
| ); | ||
| }; | ||
|
|
||
| // 执行入口 | ||
| processAllComponents().catch((err) => | ||
| console.error(`${COMPONENT_NAME === 'all' ? '❌ 批量处理失败:' : `${COMPONENT_NAME}处理失败`}`, err), | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import path from 'path'; | ||
|
|
||
| function resolveCwd(...args) { | ||
| args.unshift(process.cwd()); | ||
| return path.join(...args); | ||
| } | ||
|
|
||
| export default resolveCwd; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
chat 系列暂时不对外暴露 css vars,后面会统一支持