| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- // import { logger, type RsbuildPlugin } from '@rsbuild/core'
- // import path from 'path'
- // import fs from 'fs'
- // import readline from 'readline'
- // import * as tar from 'tar'
- // const __dirname = process.cwd()
- // // 是否打的是正式环境的包
- // const isProd = process.env.npm_lifecycle_event === 'build'
- // /**
- // * @name backupDist
- // * @description 打包完成后备份dist目录,只会备份production环境的打包文件
- // */
- // export const pluginBackup = (): RsbuildPlugin => ({
- // name: 'plugin-backup-dist',
- // setup(api) {
- // const { version } = require(path.resolve(process.cwd(), 'package.json'))
- // api.onCloseBuild(() => {
- // if (!isProd) {
- // logger.warn('非正式环境打包,不执行备份操作')
- // return
- // }
- // // 判断是否存在dist-backup目录,不存在则创建
- // const distDir = path.resolve(__dirname, 'dist')
- // const distBackupDir = path.resolve(__dirname, 'dist-backup')
- // if (!fs.existsSync(distDir)) {
- // logger.error('dist目录不存在,备份失败!')
- // return
- // }
- // fs.mkdirSync(path.resolve(distBackupDir, version), { recursive: true })
- // logger.warn('打包完成,开始备份dist目录')
- // // 备份dist目录
- // fileToGz(
- // distDir,
- // path.resolve(
- // distBackupDir,
- // version,
- // `dist-${dateFormat(new Date())}.gz`,
- // ),
- // ).then(() => {
- // logger.success('dist备份完成')
- // copyFile(version)
- // })
- // })
- // api.onAfterBuild(() => {
- // outputLog()
- // })
- // },
- // })
- // function fileToGz(fromPath: string, toPath: string) {
- // return new Promise((resolve, reject) => {
- // const output = fs.createWriteStream(toPath)
- // let totalSize = 0
- // let processedSize = 0
- // const calculateSize = (dir: string): number => {
- // let size = 0
- // const files = fs.readdirSync(dir)
- // for (const file of files) {
- // const filePath = path.join(dir, file)
- // const stat = fs.statSync(filePath)
- // if (stat.isDirectory()) {
- // size += calculateSize(filePath)
- // } else {
- // size += stat.size
- // }
- // }
- // return size
- // }
- // totalSize = calculateSize(fromPath)
- // tar
- // .create(
- // {
- // gzip: true,
- // cwd: fromPath,
- // onWriteEntry(entry) {
- // processedSize += entry.stat?.size || 0
- // const progress = Math.ceil((processedSize / totalSize) * 100)
- // readline.cursorTo(process.stdout, 0)
- // readline.clearLine(process.stdout, 0)
- // process.stdout.write(`压缩进度:${progress}%\r`)
- // },
- // },
- // ['.'],
- // )
- // .pipe(output)
- // .on('close', () => {
- // resolve(true)
- // })
- // .on('error', (err: Error) => {
- // logger.error(`打包失败: ${err.message}`)
- // reject(err)
- // })
- // })
- // }
- // function copyFile(version) {
- // // 判断是否存在dist-file目录,不存在则创建
- // const distBackupDir = path.resolve(__dirname, 'dist-file')
- // // 新建版本文件夹及备份文件夹
- // fs.mkdirSync(path.resolve(distBackupDir, 'dist-backup'), { recursive: true })
- // fs.mkdirSync(path.resolve(distBackupDir, version), { recursive: true })
- // //复制 dist 文件夹内容
- // getFiles(
- // path.resolve(__dirname, 'dist'),
- // path.resolve(__dirname, `dist-file/${version}`),
- // )
- // //复制 dist-backup 文件夹内容
- // getFiles(
- // path.resolve(__dirname, 'dist-backup'),
- // path.resolve(__dirname, `dist-file/dist-backup`),
- // )
- // logger.success('服务器打包文件夹创建成功')
- // }
- // function getFiles(OriginFilePath, CopyFilePath) {
- // fs.readdir(OriginFilePath, { withFileTypes: true }, (err, files) => {
- // for (const file of files) {
- // //判断是否是文件夹,不是则直接复制文件到newFile中
- // if (!file.isDirectory()) {
- // const OriginFile = path.resolve(OriginFilePath, file.name)
- // const CopyFile = path.resolve(CopyFilePath, file.name)
- // fs.copyFileSync(OriginFile, CopyFile)
- // } else {
- // //如果是文件夹就递归变量把最新的文件夹路径传过去
- // const CopyDirPath = path.resolve(CopyFilePath, file.name)
- // const OriginDirPath = path.resolve(OriginFilePath, file.name)
- // fs.mkdir(CopyDirPath, (err) => {})
- // getFiles(OriginDirPath, CopyDirPath)
- // }
- // }
- // })
- // }
- // /* 日期格式化 */
- // function dateFormat(dateInput: Date, format: string = 'yyyy-MM-dd-HH:mm:ss') {
- // if (!dateInput) return ''
- // const components: Record<string, any> = {
- // yyyy: dateInput.getFullYear(),
- // MM: dateInput.getMonth() + 1,
- // dd: dateInput.getDate(),
- // HH: dateInput.getHours(),
- // mm: dateInput.getMinutes(),
- // ss: dateInput.getSeconds(),
- // }
- // format = format.replace(/(yyyy|MM|dd|HH|mm|ss)/g, (match, p1) => {
- // return components[p1].toString().padStart(2, '0')
- // })
- // return format
- // }
- // function outputLog() {
- // const buildType = {
- // build: 'APP正式包',
- // 'build:web': 'WEB正式环境包',
- // 'build:test': 'APP测试环境包',
- // 'build:webTest': 'WEB测试环境包',
- // }
- // const npmEvent = process.env.npm_lifecycle_event
- // logger.info(`打包类型: ${npmEvent ? buildType[npmEvent] : npmEvent}`)
- // logger.info(`打包时间:${new Date().toLocaleString()}`)
- // console.log(`\r`)
- // }
|