-
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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 | ||
| } | ||
coderabbitai[bot] 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)) | ||
| } | ||
|
Comment on lines
43
to
64
Contributor
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. Validate HTTP response status before parsing. The script doesn't check if the request succeeded before parsing JSON. Non-2xx responses may cause parsing failures or silent errors. Apply this diff: 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()
- logger.success('File uploaded successfully:', data)
+ logger.success(`File uploaded successfully: ${JSON.stringify(data)}`)
} catch (error) {🤖 Prompt for AI Agents
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); | ||
| }) | ||
| 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 |
| 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 was deleted.
Uh oh!
There was an error while loading. Please reload this page.