build.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { execaCommand } from 'execa'
  2. import path from 'path'
  3. import fsExtra from 'fs-extra'
  4. const { existsSync, remove, copy } = fsExtra
  5. const cwd = process.cwd()
  6. //打包发布路径,谨慎改动
  7. const releaseRelativePath = '../public/admin'
  8. const distPath = path.resolve(cwd, 'dist')
  9. const releasePath = path.resolve(cwd, releaseRelativePath)
  10. async function build() {
  11. await execaCommand('vite build', { stdio: 'inherit', encoding: 'utf-8', cwd })
  12. if (existsSync(releasePath)) {
  13. await remove(releasePath)
  14. }
  15. console.log(`文件正在复制 ==> ${releaseRelativePath}`)
  16. try {
  17. await copyFile(distPath, releasePath)
  18. } catch (error) {
  19. console.log(`\n ${error}`)
  20. }
  21. console.log(`文件已复制 ==> ${releaseRelativePath}`)
  22. }
  23. function copyFile(sourceDir, targetDir) {
  24. return new Promise((resolve, reject) => {
  25. copy(sourceDir, targetDir, (err) => {
  26. if (err) {
  27. reject(err)
  28. } else {
  29. resolve()
  30. }
  31. })
  32. })
  33. }
  34. build()