Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion designer-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build:alpha": "cross-env NODE_OPTIONS=--max-old-space-size=10240 vite build --mode alpha",
"build": "cross-env NODE_OPTIONS=--max-old-space-size=10240 vite build",
"test": "vitest run",
"test:watch": "vitest"
"test:watch": "vitest",
"uploadMaterials": "node ./scripts/uploadMaterials.mjs"
},
"dependencies": {
"@opentiny/tiny-engine": "workspace:^",
Expand All @@ -28,6 +29,9 @@
"@opentiny/tiny-engine-vite-config": "workspace:^",
"@vitejs/plugin-vue": "^5.1.2",
"cross-env": "^7.0.3",
"dotenv": "^16.6.1",
"fs-extra": "^11.3.2",
"picocolors": "^1.1.1",
"vite": "^5.4.2",
"vitest": "3.0.9"
}
Expand Down
67 changes: 67 additions & 0 deletions designer-demo/scripts/logger.mjs
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')

Check failure on line 9 in designer-demo/scripts/logger.mjs

View workflow job for this annotation

GitHub Actions / push-check

A `require()` style import is forbidden
return true
} catch (e) {
console.warn('colors package not found, using basic logging')

Check failure on line 12 in designer-demo/scripts/logger.mjs

View workflow job for this annotation

GitHub Actions / push-check

Unexpected console statement
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')

Check failure on line 30 in designer-demo/scripts/logger.mjs

View workflow job for this annotation

GitHub Actions / push-check

A `require()` style import is forbidden
const colorMap = {
info: colors.cyan,
warn: colors.yellow,
error: colors.red,
success: colors.green
}

const coloredType = colorMap[type] ? colorMap[type](type.toUpperCase()) : type.toUpperCase()
console.log(`${prefix} ${coloredType} ${message}`)

Check failure on line 39 in designer-demo/scripts/logger.mjs

View workflow job for this annotation

GitHub Actions / push-check

Unexpected console statement
} else {
const emojiMap = {
info: 'ℹ️',
warn: '⚠️',
error: '❌',
success: '✅'
}
console.log(`${prefix} ${emojiMap[type] || ''} ${message}`)

Check failure on line 47 in designer-demo/scripts/logger.mjs

View workflow job for this annotation

GitHub Actions / push-check

Unexpected console statement
}
}

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
75 changes: 75 additions & 0 deletions designer-demo/scripts/uploadMaterials.mjs
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))
}
}

main()
.catch((e) => {
const logger = new Logger('uploadMaterials')
logger.error('Error uploading file:', e instanceof Error ? e.message : String(e));
process.exit(1);
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"splitMaterials": "node ./scripts/splitMaterials.mjs",
"buildMaterials": "node ./scripts/buildMaterials.mjs",
"updateTemplate": "node ./scripts/updateTemplate.mjs",
"uploadMaterials": "node scripts/uploadMaterials.mjs"
"uploadMaterials": "pnpm --filter designer-demo uploadMaterials"
},
"devDependencies": {
"@eslint/js": "^8.57.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/engine-cli/template/designer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "concurrently 'pnpm:serve:mock' 'pnpm:serve:frontend'",
"build:alpha": "cross-env NODE_OPTIONS=--max-old-space-size=10240 vite build --mode alpha",
"build": "cross-env NODE_OPTIONS=--max-old-space-size=10240 vite build",
"uploadMaterials": "node ./scripts/uploadMaterials.mjs",
"serve:frontend": "cross-env vite",
"serve:mock": "node node_modules/@opentiny/tiny-engine-mock/dist/app.js"
},
Expand All @@ -28,6 +29,9 @@
"@opentiny/tiny-engine-vite-config": "^2.8.0",
"@vitejs/plugin-vue": "^5.1.2",
"cross-env": "^7.0.3",
"dotenv": "^16.6.1",
"fs-extra": "^11.3.2",
"picocolors": "^1.1.1",
"vite": "^5.4.2",
"concurrently": "^8.2.0"
}
Expand Down
43 changes: 43 additions & 0 deletions packages/engine-cli/template/designer/scripts/logger.mjs
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 packages/engine-cli/template/designer/scripts/uploadMaterials.mjs
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
}

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))
}
}

main().catch((e) => {
const logger = new Logger('uploadMaterials')
logger.error('Error uploading file:', e instanceof Error ? e.message : String(e))
process.exit(1)
})
2 changes: 1 addition & 1 deletion scripts/updateTemplate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs-extra'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import Logger from './logger.mjs'
import pkg from '../packages/design-core/package.json' assert { type: 'json' }
import pkg from '../packages/design-core/package.json' with { type: 'json' }

const logger = new Logger('updateTemplate')

Expand Down
40 changes: 0 additions & 40 deletions scripts/uploadMaterials.mjs

This file was deleted.

Loading