copyDir.js 3.61 KB
const path = require('path')

class copyDir {
  apply(compiler) {
    // done是webpack 得编译完成周期
    compiler.hooks.afterEmit.tap('DonePlugins', function(stats) {
      // console.log('编译完成!!')
      // 因为写在文件夹里面 要找外面的文件要加../

      // 删除element
      removeDir(`${__dirname}/../dist/static/css`)
      // cpSync(
      //   path.join(__dirname, '../copyDirTarget'),
      //   path.join(__dirname, '../dist')
      // )
      // cpSync(
      //   path.join(__dirname, '../copyDirTarget'),
      //   path.join(__dirname, '../dist//dj-carlife-web')
      // )
      // removeDir(`${__dirname}/../copyDirTarget`)
    })
  }
}
/** 主要版本 */
const major = process.version.match(/v([0-9]*).([0-9]*)/)[1]
/** 特性版本 */
const minor = process.version.match(/v([0-9]*).([0-9]*)/)[2]
/**
 * 文件夹复制
 * @param {string} source 源文件夹
 * @param {string} destination 目标文件夹
 */
function cpSync(source, destination) {
  const fs = require('fs')
  // console.log('source', source)
  // 判断node版本不是16.7.0以上的版本
  // 则进入兼容处理
  // 这样处理是因为16.7.0的版本支持了直接复制文件夹的操作
  if (Number(major) < 16 || (Number(major) == 16 && Number(minor) < 7)) {
    // 如果存在文件夹 先递归删除该文件夹
    if (fs.existsSync(destination)) fs.rmSync(destination, { recursive: true })
    // 新建文件夹 递归新建
    fs.mkdirSync(destination, { recursive: true })
    // 读取源文件夹
    const rd = fs.readdirSync(source)
    // console.log('rd', rd)
    for (const fd of rd) {
      // 循环拼接源文件夹/文件全名称
      const sourceFullName = source + '/' + fd
      // 循环拼接目标文件夹/文件全名称
      const destFullName = destination + '/' + fd
      // 读取文件信息
      const lstatRes = fs.lstatSync(sourceFullName)
      // 是否是文件
      if (lstatRes.isFile()) fs.copyFileSync(sourceFullName, destFullName)
      // 是否是文件夹
      if (lstatRes.isDirectory()) cpSync(sourceFullName, destFullName)
    }
  } else fs.cpSync(source, destination, { force: true, recursive: true })
}

/**
 * @caption 删除本地文件夹/文件
 * @param {String} directoryPath 例: d://x/x/x.json 或者 d://x/x/x
 */
function removeDir(directoryPath) {
  const fs = require('fs')
  fs.readdir(directoryPath, (err, files) => {
    if (err) {
      console.error('读取目录失败:', err)
      return
    }

    if (files.length === 0) {
      console.log('目录为空,没有文件可删除。')
      return
    }

    const lastFile = files[files.length - 1]
    const filePath = path.join(directoryPath, lastFile)

    fs.unlink(filePath, err => {
      if (err) {
        console.error('删除文件失败:', err)
      } else {
        console.log('最后一个文件已被成功删除。')
      }
    })
  })
  // 判断文件是否存在
  // require('fs').existsSync(directoryPath) && dleteDir()
  // function dleteDir() {
  //   const files = fs.readdirSync(directoryPath)
  //   for (var i = 0; i < files.length; i++) {
  //     const newPath = path.join(directoryPath, files[i])
  //     const stat = fs.statSync(newPath)
  //     if (stat.isDirectory()) {
  //       // 如果是文件夹就递归下去
  //       removeDir(newPath)
  //     } else {
  //       // 删除文件
  //       fs.unlinkSync(newPath)
  //     }
  //   }
  //   fs.rmdirSync(directoryPath) // 如果文件夹是空的,就将自己删除掉
  // }
}

module.exports = copyDir