plugin_backup.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // import { logger, type RsbuildPlugin } from '@rsbuild/core'
  2. // import path from 'path'
  3. // import fs from 'fs'
  4. // import readline from 'readline'
  5. // import * as tar from 'tar'
  6. // const __dirname = process.cwd()
  7. // // 是否打的是正式环境的包
  8. // const isProd = process.env.npm_lifecycle_event === 'build'
  9. // /**
  10. // * @name backupDist
  11. // * @description 打包完成后备份dist目录,只会备份production环境的打包文件
  12. // */
  13. // export const pluginBackup = (): RsbuildPlugin => ({
  14. // name: 'plugin-backup-dist',
  15. // setup(api) {
  16. // const { version } = require(path.resolve(process.cwd(), 'package.json'))
  17. // api.onCloseBuild(() => {
  18. // if (!isProd) {
  19. // logger.warn('非正式环境打包,不执行备份操作')
  20. // return
  21. // }
  22. // // 判断是否存在dist-backup目录,不存在则创建
  23. // const distDir = path.resolve(__dirname, 'dist')
  24. // const distBackupDir = path.resolve(__dirname, 'dist-backup')
  25. // if (!fs.existsSync(distDir)) {
  26. // logger.error('dist目录不存在,备份失败!')
  27. // return
  28. // }
  29. // fs.mkdirSync(path.resolve(distBackupDir, version), { recursive: true })
  30. // logger.warn('打包完成,开始备份dist目录')
  31. // // 备份dist目录
  32. // fileToGz(
  33. // distDir,
  34. // path.resolve(
  35. // distBackupDir,
  36. // version,
  37. // `dist-${dateFormat(new Date())}.gz`,
  38. // ),
  39. // ).then(() => {
  40. // logger.success('dist备份完成')
  41. // copyFile(version)
  42. // })
  43. // })
  44. // api.onAfterBuild(() => {
  45. // outputLog()
  46. // })
  47. // },
  48. // })
  49. // function fileToGz(fromPath: string, toPath: string) {
  50. // return new Promise((resolve, reject) => {
  51. // const output = fs.createWriteStream(toPath)
  52. // let totalSize = 0
  53. // let processedSize = 0
  54. // const calculateSize = (dir: string): number => {
  55. // let size = 0
  56. // const files = fs.readdirSync(dir)
  57. // for (const file of files) {
  58. // const filePath = path.join(dir, file)
  59. // const stat = fs.statSync(filePath)
  60. // if (stat.isDirectory()) {
  61. // size += calculateSize(filePath)
  62. // } else {
  63. // size += stat.size
  64. // }
  65. // }
  66. // return size
  67. // }
  68. // totalSize = calculateSize(fromPath)
  69. // tar
  70. // .create(
  71. // {
  72. // gzip: true,
  73. // cwd: fromPath,
  74. // onWriteEntry(entry) {
  75. // processedSize += entry.stat?.size || 0
  76. // const progress = Math.ceil((processedSize / totalSize) * 100)
  77. // readline.cursorTo(process.stdout, 0)
  78. // readline.clearLine(process.stdout, 0)
  79. // process.stdout.write(`压缩进度:${progress}%\r`)
  80. // },
  81. // },
  82. // ['.'],
  83. // )
  84. // .pipe(output)
  85. // .on('close', () => {
  86. // resolve(true)
  87. // })
  88. // .on('error', (err: Error) => {
  89. // logger.error(`打包失败: ${err.message}`)
  90. // reject(err)
  91. // })
  92. // })
  93. // }
  94. // function copyFile(version) {
  95. // // 判断是否存在dist-file目录,不存在则创建
  96. // const distBackupDir = path.resolve(__dirname, 'dist-file')
  97. // // 新建版本文件夹及备份文件夹
  98. // fs.mkdirSync(path.resolve(distBackupDir, 'dist-backup'), { recursive: true })
  99. // fs.mkdirSync(path.resolve(distBackupDir, version), { recursive: true })
  100. // //复制 dist 文件夹内容
  101. // getFiles(
  102. // path.resolve(__dirname, 'dist'),
  103. // path.resolve(__dirname, `dist-file/${version}`),
  104. // )
  105. // //复制 dist-backup 文件夹内容
  106. // getFiles(
  107. // path.resolve(__dirname, 'dist-backup'),
  108. // path.resolve(__dirname, `dist-file/dist-backup`),
  109. // )
  110. // logger.success('服务器打包文件夹创建成功')
  111. // }
  112. // function getFiles(OriginFilePath, CopyFilePath) {
  113. // fs.readdir(OriginFilePath, { withFileTypes: true }, (err, files) => {
  114. // for (const file of files) {
  115. // //判断是否是文件夹,不是则直接复制文件到newFile中
  116. // if (!file.isDirectory()) {
  117. // const OriginFile = path.resolve(OriginFilePath, file.name)
  118. // const CopyFile = path.resolve(CopyFilePath, file.name)
  119. // fs.copyFileSync(OriginFile, CopyFile)
  120. // } else {
  121. // //如果是文件夹就递归变量把最新的文件夹路径传过去
  122. // const CopyDirPath = path.resolve(CopyFilePath, file.name)
  123. // const OriginDirPath = path.resolve(OriginFilePath, file.name)
  124. // fs.mkdir(CopyDirPath, (err) => {})
  125. // getFiles(OriginDirPath, CopyDirPath)
  126. // }
  127. // }
  128. // })
  129. // }
  130. // /* 日期格式化 */
  131. // function dateFormat(dateInput: Date, format: string = 'yyyy-MM-dd-HH:mm:ss') {
  132. // if (!dateInput) return ''
  133. // const components: Record<string, any> = {
  134. // yyyy: dateInput.getFullYear(),
  135. // MM: dateInput.getMonth() + 1,
  136. // dd: dateInput.getDate(),
  137. // HH: dateInput.getHours(),
  138. // mm: dateInput.getMinutes(),
  139. // ss: dateInput.getSeconds(),
  140. // }
  141. // format = format.replace(/(yyyy|MM|dd|HH|mm|ss)/g, (match, p1) => {
  142. // return components[p1].toString().padStart(2, '0')
  143. // })
  144. // return format
  145. // }
  146. // function outputLog() {
  147. // const buildType = {
  148. // build: 'APP正式包',
  149. // 'build:web': 'WEB正式环境包',
  150. // 'build:test': 'APP测试环境包',
  151. // 'build:webTest': 'WEB测试环境包',
  152. // }
  153. // const npmEvent = process.env.npm_lifecycle_event
  154. // logger.info(`打包类型: ${npmEvent ? buildType[npmEvent] : npmEvent}`)
  155. // logger.info(`打包时间:${new Date().toLocaleString()}`)
  156. // console.log(`\r`)
  157. // }