-
Notifications
You must be signed in to change notification settings - Fork 473
feat: add materials script #195
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5a0ce19
feat: add materials script
287e6fa
fix:优化命令行提示
9174c34
fix:修复创建表
af0ff35
fix:修复插入组件数据sql语句
4435955
fix:修复插入组件数据sql语句
b033871
fix:修复新增组件sql语句
9abb6a3
fix:创建表异步执行
af2452e
fix:补充注释
4866652
fix:修改默认数据库名称
1e81b8d
fix:提取物料保存文件夹名称
4ea2f67
fix:优化logger输出语句
40c4b70
fix:删除无用代码
14a4f1e
fix:提取物料资产包关联id
399f401
fix:sql配置提取env
33d8f34
fix:删除无用log
a091a83
fix:物料资产包关联表唯一性校验
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| SQL_HOST=localhost | ||
| SQL_PORT=3306 | ||
| SQL_USER=root | ||
| SQL_PASSWORD=admin | ||
| SQL_DATABASE=tiny_engine |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import fsExtra from 'fs-extra' | ||
| import path from 'node:path' | ||
| import chokidar from 'chokidar' | ||
| import fg from 'fast-glob' | ||
| import MysqlConnection from './connection.mjs' | ||
| import Logger from './logger.mjs' | ||
|
|
||
| const logger = new Logger('buildMaterials') | ||
| // 物料文件存放文件夹名称 | ||
| const materialsDir = 'materials' | ||
| // 物料资产包 | ||
| const bundlePath = path.join(process.cwd(), '/packages/design-core/public/mock/bundle.json') | ||
|
This conversation was marked as resolved.
|
||
| // mockServer应用数据 | ||
| const appInfoPath = path.join(process.cwd(), '/mockServer/src/services/appinfo.json') | ||
| const appInfo = fsExtra.readJSONSync(appInfoPath) | ||
| const bundle = { | ||
| data: { | ||
| framework: 'Vue', | ||
| materials: { | ||
| components: [], | ||
| blocks: [], | ||
| snippets: [] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const connection = new MysqlConnection() | ||
|
|
||
| /** | ||
| * 更新物料资产包和应用mock数据 | ||
| */ | ||
| const write = () => { | ||
| fsExtra.outputJSONSync(bundlePath, bundle, { spaces: 2 }) | ||
| fsExtra.outputJSONSync(appInfoPath, appInfo, { spaces: 2 }) | ||
| } | ||
|
|
||
| /** | ||
| * 校验组件文件数据 | ||
| * @param {string} file 组件文件路径 | ||
| * @param {object} component 组件数据 | ||
| * @returns | ||
| */ | ||
| const validateComponent = (file, component) => { | ||
| const requiredFields = ['component'] | ||
| const fields = Object.keys(component) | ||
| const requiredList = requiredFields.filter((field) => !fields.includes(field)) | ||
|
|
||
| if (requiredList.length) { | ||
| logger.error(`组件文件 ${file} 缺少必要字段:${requiredList.join('、')}。`) | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| if (!component.npm) { | ||
| logger.warn(`组件文件 ${file} 缺少 npm 字段,出码时将不能通过import语句导入组件。`) | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * 校验区块文件数据 | ||
| * @param {string} file 区块文件路径 | ||
| * @param {object} block 区块数据 | ||
| * @returns | ||
| */ | ||
| const validateBlock = (file, block) => { | ||
| const requiredFields = ['label', 'assets'] | ||
| const fields = Object.keys(block) | ||
| const requiredList = requiredFields.filter((field) => !fields.includes(field)) | ||
|
|
||
| if (requiredList.length) { | ||
| logger.error(`区块文件 ${file} 缺少必要字段:${requiredList.join('、')}。`) | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * 读取materials目录下的json文件,执行下列操作 | ||
| * 1. 合并生成物料资产包 | ||
| * 2. 更新应用的组件数据componentsMap | ||
| * 3. 连接上数据库后,将组件数据写入数据库(新增或更新) | ||
| */ | ||
| const generateComponents = () => { | ||
| try { | ||
| fg([`${materialsDir}/**/*.json`]).then((files) => { | ||
| if(!files.length) { | ||
| logger.warn('物料文件夹为空,请先执行`pnpm splitMaterials`命令拆分物料资产包') | ||
| } | ||
|
|
||
| const { components = [], snippets = [], blocks = [] } = bundle.data.materials | ||
| const componentsMap = [] | ||
| const appInfoBlocksLabels = appInfo.blockHistories.map((item) => item.label) | ||
|
|
||
| files.forEach((file) => { | ||
| const material = fsExtra.readJsonSync(file, { throws: false }) | ||
|
|
||
| if (!material) { | ||
| logger.error(`读取物料文件 ${file} 失败`) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if (file.includes('/blocks/')) { | ||
| const valid = validateBlock(file, material) | ||
|
|
||
| if (!valid) return | ||
|
|
||
| blocks.push(material) | ||
|
|
||
| if (!appInfoBlocksLabels.includes(material.label)) { | ||
| appInfo.blockHistories.push(material) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| const valid = validateComponent(file, material) | ||
|
|
||
| if (!valid) return | ||
|
|
||
| const { snippets: componentSnippets, category, ...componentInfo } = material | ||
|
|
||
| components.push(componentInfo) | ||
|
|
||
| const snippet = snippets.find((item) => item.group === category) | ||
|
|
||
| if (snippet) { | ||
| componentSnippets && snippet.children.push(componentSnippets[0]) | ||
|
chilingling marked this conversation as resolved.
|
||
| } else if (category && componentInfo) { | ||
| snippets.push({ | ||
| group: category, | ||
| children: componentSnippets || [] | ||
| }) | ||
| } | ||
|
|
||
| const { component, npm = {} } = componentInfo | ||
|
|
||
| componentsMap.push({ component, npm }) | ||
|
|
||
| if (connection.connected) { | ||
| connection.initDB(material) | ||
| } | ||
| }) | ||
|
|
||
| appInfo.materialHistory.components = componentsMap | ||
|
|
||
| write() | ||
| }) | ||
|
|
||
| logger.success('构建物料资产包成功') | ||
| } catch (error) { | ||
| logger.error(`构建物料资产包失败:${error}`) | ||
| } | ||
| } | ||
|
|
||
| // 监听materials下json文件的变化 | ||
| const watcher = chokidar.watch(`${materialsDir}/**/*.json`, { ignoreInitial: true }) | ||
|
|
||
| watcher.on('all', (event, file) => { | ||
| const eventMap = { | ||
| add: '新增', | ||
| change: '更新', | ||
| unlink: '删除' | ||
| } | ||
|
|
||
| logger.info(`${eventMap[event]}组件文件 ${file}`) | ||
|
|
||
| // 监听物料文件变化,更新物料资产包 | ||
| generateComponents() | ||
|
|
||
| if (!connection.connected || event === 'unlink') return | ||
|
|
||
| const component = fsExtra.readJsonSync(path.join(process.cwd(), file)) | ||
|
|
||
| if (event === 'change') { | ||
| connection.updateComponent(component) | ||
| } else if (event === 'add') { | ||
| connection.insertComponent(component) | ||
| } | ||
| }) | ||
|
|
||
| // 连接数据库 | ||
| connection | ||
| .connect() | ||
| .then(() => { | ||
| connection.initUserComponentsTable().finally(() => { | ||
| generateComponents() | ||
| }) | ||
| }) | ||
| .catch(() => { | ||
| // 未能连接数据库也可以执行更新本地mock数据 | ||
| generateComponents() | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.