-
Notifications
You must be signed in to change notification settings - Fork 437
chore: move upload script to template #1685
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
Open
chilingling
wants to merge
5
commits into
opentiny:develop
Choose a base branch
from
chilingling:feat/move-upload-script-to-template
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| class Logger { | ||
| constructor(command = 'default') { | ||
| this.command = command | ||
| this.hasColors = this.checkColorSupport() | ||
| } | ||
|
|
||
| checkColorSupport() { | ||
| try { | ||
| require('colors') | ||
| return true | ||
| } catch (e) { | ||
| console.warn('colors package not found, using basic logging') | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| output(type, ...args) { // 支持多个参数 | ||
| const time = new Date().toLocaleTimeString() | ||
| const prefix = `[${this.command}] [${time}]` | ||
|
|
||
| // 将所有参数合并为一个字符串 | ||
| const message = args.map(arg => { | ||
| if (typeof arg === 'object') { | ||
| return JSON.stringify(arg, null, 2) | ||
| } | ||
| return String(arg) | ||
| }).join(' ') | ||
|
|
||
| if (this.hasColors) { | ||
| const colors = require('colors') | ||
| const colorMap = { | ||
| info: colors.cyan, | ||
| warn: colors.yellow, | ||
| error: colors.red, | ||
| success: colors.green | ||
| } | ||
lu-yg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const coloredType = colorMap[type] ? colorMap[type](type.toUpperCase()) : type.toUpperCase() | ||
| console.log(`${prefix} ${coloredType} ${message}`) | ||
| } else { | ||
| const emojiMap = { | ||
| info: 'ℹ️', | ||
| warn: '⚠️', | ||
| error: '❌', | ||
| success: '✅' | ||
| } | ||
| console.log(`${prefix} ${emojiMap[type] || ''} ${message}`) | ||
| } | ||
| } | ||
|
|
||
| success(...args) { | ||
| this.output('success', ...args) | ||
| } | ||
|
|
||
| info(...args) { | ||
| this.output('info', ...args) | ||
| } | ||
|
|
||
| warn(...args) { | ||
| this.output('warn', ...args) | ||
| } | ||
|
|
||
| error(...args) { | ||
| this.output('error', ...args) | ||
| } | ||
| } | ||
| export default Logger | ||
lu-yg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,75 @@ | ||
| import { Buffer } from 'node:buffer' | ||
| import path from 'node:path' | ||
| import dotenv from 'dotenv' | ||
| import fs from 'fs-extra' | ||
| import Logger from './logger.mjs' | ||
|
|
||
|
|
||
| /** | ||
| * 同步物料资产包到后端数据库 | ||
| * 1. 读取 env/.env.local 文件,获取后端地址。需要设置地址如:backend_url=http://localhost:9090 | ||
| * 2. 读取 public/mock/bundle.json 文件,获取物料资产包数据 | ||
| * 3. 将物料资产包数据通过 POST 请求上传到后端接口 /material-center/api/component/bundle/create | ||
| * 4. 检查数据库t_component表中数据是否更新成功 | ||
| * | ||
| * 使用场景: | ||
| * 1. 本地已经将 bundle.json 文件进行修改,但是数据需要同步到后端数据库中。 | ||
| * 2. 本地已经将 bundle.json 文件进行修改,但是出码仍然不正确。 | ||
| * @returns | ||
| */ | ||
| async function main() { | ||
| const logger = new Logger('uploadMaterials') | ||
|
|
||
| // 先构造出.env*文件的绝对路径 | ||
| const appDirectory = fs.realpathSync(process.cwd()) | ||
| const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath) | ||
| const pathsDotenv = resolveApp('env') | ||
| logger.info(`Start to load .env.local file from ${pathsDotenv}/.env.local`) | ||
| dotenv.config({ path: `${pathsDotenv}/.env.local` }) | ||
| const { backend_url } = process.env | ||
|
|
||
| if (!backend_url) { | ||
| logger.error('backend_url is not set in .env.local file') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| const bundlePath = path.join(process.cwd(), './public/mock/bundle.json') | ||
| logger.info(`Start to read bundle.json file from ${bundlePath}`) | ||
| const bundle = fs.readJSONSync(bundlePath) | ||
| const jsonBuffer = Buffer.from(JSON.stringify(bundle)) | ||
|
|
||
| const requestUrl = (backend_url.endsWith('/') ? backend_url.slice(0, -1) : backend_url) + '/material-center/api/component/bundle/create' | ||
| logger.info(`Start to upload bundle.json file to ${requestUrl}`) | ||
| try { | ||
| const formData = new FormData() | ||
| formData.append('file', new Blob([jsonBuffer], { type: 'application/json'}), 'bundle.json') | ||
| const response = await fetch(requestUrl, { | ||
| method: 'POST', | ||
| body: formData | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text() | ||
| throw new Error(`Upload failed with status ${response.status}: ${errorText}`) | ||
| } | ||
| const data = await response.json() | ||
| if (data && data.success) { | ||
| logger.success('File uploaded successfully!') | ||
| logger.success('Inserted records:', data.data?.insertNum || 0) | ||
| logger.success('Updated records:', data.data?.updateNum || 0) | ||
| logger.success('Message:', data.message) | ||
| } else { | ||
| logger.warn('Upload completed but success flag is false:', data) | ||
| logger.warn('Upload completed with warnings:', data.message) | ||
| } | ||
| } catch (error) { | ||
| logger.error('Error uploading file:', error instanceof Error ? error.message : String(error)) | ||
| } | ||
lu-yg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| main() | ||
| .catch((e) => { | ||
| const logger = new Logger('uploadMaterials') | ||
| logger.error('Error uploading file:', e instanceof Error ? e.message : String(e)); | ||
| process.exit(1); | ||
| }) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import colors from 'picocolors' | ||
|
|
||
| class Logger { | ||
| constructor(command) { | ||
| this.command = command | ||
| } | ||
|
|
||
| output(type, msg) { | ||
| const format = () => { | ||
| const colorMap = { | ||
| info: 'cyan', | ||
| warn: 'yellow', | ||
| error: 'red', | ||
| success: 'green' | ||
| } | ||
| const time = new Date().toLocaleTimeString() | ||
| const colorMsg = colors[colorMap[type]](type) | ||
|
|
||
| return `[${this.command}] [${colors.dim(time)}] ${colorMsg} ${msg}` | ||
| } | ||
| const _logger = console | ||
|
|
||
| return _logger.log(format()) | ||
| } | ||
|
|
||
| info(msg) { | ||
| this.output('info', msg) | ||
| } | ||
|
|
||
| warn(msg) { | ||
| this.output('warn', msg) | ||
| } | ||
|
|
||
| error(msg) { | ||
| this.output('error', msg) | ||
| } | ||
|
|
||
| success(msg) { | ||
| this.output('success', msg) | ||
| } | ||
| } | ||
|
|
||
| export default Logger |
62 changes: 62 additions & 0 deletions
62
packages/engine-cli/template/designer/scripts/uploadMaterials.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,62 @@ | ||
| import { Buffer } from 'node:buffer' | ||
| import path from 'node:path' | ||
| import dotenv from 'dotenv' | ||
| import fs from 'fs-extra' | ||
| import Logger from './logger.mjs' | ||
|
|
||
| /** | ||
| * 同步物料资产包到后端数据库 | ||
| * 1. 读取 env/.env.local 文件,获取后端地址。需要设置地址如:backend_url=http://localhost:9090 | ||
| * 2. 读取 public/mock/bundle.json 文件,获取物料资产包数据 | ||
| * 3. 将物料资产包数据通过 POST 请求上传到后端接口 /material-center/api/component/bundle/create | ||
| * 4. 检查数据库t_component表中数据是否更新成功 | ||
| * | ||
| * 使用场景: | ||
| * 1. 本地已经将 bundle.json 文件进行修改,但是数据需要同步到后端数据库中。 | ||
| * 2. 本地已经将 bundle.json 文件进行修改,但是出码仍然不正确。 | ||
| * @returns | ||
| */ | ||
| async function main() { | ||
| const logger = new Logger('uploadMaterials') | ||
|
|
||
| // 先构造出.env*文件的绝对路径 | ||
| const appDirectory = fs.realpathSync(process.cwd()) | ||
| const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath) | ||
| const pathsDotenv = resolveApp('env') | ||
| logger.info(`Start to load .env.local file from ${pathsDotenv}/.env.local`) | ||
| dotenv.config({ path: `${pathsDotenv}/.env.local` }) | ||
| const { backend_url } = process.env | ||
|
|
||
| if (!backend_url) { | ||
| logger.error('backend_url is not set in .env.local file') | ||
| return | ||
| } | ||
lu-yg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const bundlePath = path.join(process.cwd(), './public/mock/bundle.json') | ||
| logger.info(`Start to read bundle.json file from ${bundlePath}`) | ||
| const bundle = fs.readJSONSync(bundlePath) | ||
| const jsonBuffer = Buffer.from(JSON.stringify(bundle)) | ||
|
|
||
| const requestUrl = | ||
| (backend_url.endsWith('/') ? backend_url.slice(0, -1) : backend_url) + | ||
| '/material-center/api/component/bundle/create' | ||
| logger.info(`Start to upload bundle.json file to ${requestUrl}`) | ||
| try { | ||
| const formData = new FormData() | ||
| formData.append('file', new Blob([jsonBuffer], { type: 'application/json' }), 'bundle.json') | ||
| const response = await fetch(requestUrl, { | ||
| method: 'POST', | ||
| body: formData | ||
| }) | ||
| const data = await response.json() | ||
| logger.success('File uploaded successfully:', data) | ||
| } catch (error) { | ||
| logger.error('Error uploading file:', error instanceof Error ? error.message : String(error)) | ||
| } | ||
lu-yg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| main().catch((e) => { | ||
| const logger = new Logger('uploadMaterials') | ||
| logger.error('Error uploading file:', e instanceof Error ? e.message : String(e)) | ||
| process.exit(1) | ||
| }) | ||
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 was deleted.
Oops, something went wrong.
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.