admission-admin/scripts/build.mjs

47 lines
1.4 KiB
JavaScript

import { execaCommand } from 'execa'
import path from 'path'
import fsExtra from 'fs-extra'
const { existsSync, remove, copy } = fsExtra
const cwd = process.cwd()
//打包发布路径,谨慎改动
const releaseRelativePath = './public/admin'
const distPath = path.resolve(cwd, 'dist')
const releasePath = path.resolve(cwd, releaseRelativePath)
async function build() {
try {
console.log(`开始执行 Vite 打包...`)
await execaCommand('vite build', { stdio: 'inherit', encoding: 'utf-8', cwd })
console.log(`Vite 打包完成.`)
// 删除旧的发布目录
if (existsSync(releasePath)) {
console.log(`正在删除旧文件: ${releasePath}`)
await remove(releasePath)
console.log(`旧文件已删除: ${releasePath}`)
}
// 复制新文件到发布目录
console.log(`文件正在复制 ==> ${releaseRelativePath}`)
await copyFile(distPath, releasePath)
console.log(`文件已复制 ==> ${releaseRelativePath}`)
} catch (error) {
console.error(`构建失败: ${error.message}`)
throw error
}
}
function copyFile(sourceDir, targetDir) {
return new Promise((resolve, reject) => {
copy(sourceDir, targetDir, err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
build()