node快速复制文件或文件夹,排除部分文件(node_modules)

javascript 复制代码
const fs = require('fs')
const path = require('path')

/**
 * @description: 获取完整的文件路径
 * @param {*} url 路径
 * @return {*} 返回完整的文件路径
 */
const getPath = (url) => {
  return path.join(__dirname, url)
}

/**
 * @description: 获取参数
 * @return {*} target【目标文件夹】 source【源文件夹】
 */
function parseArgv() {
  let arguments = process.argv.splice(2)
  const target = arguments[0] || './copy'
  const source = arguments[1] || './'
  return { target, source }
}

/**
 * @description: 是否是文件夹
 * @param {*} pathDir 路径
 * @return {*} true 是,false 否
 */
const isDir = (pathDir) => {
  const path = getPath(pathDir)
  let data = false
  try {
    data = fs.statSync(path).isDirectory()
    return data
  } catch (error) {
    return data
  }
}

/**
 * @description: 获取文件夹列表
 * @param {*} pathDir 路径
 * @return {*} 文件列表
 */
const getDirList = (pathDir) => {
  const path = getPath(pathDir)
  return new Promise((resolve, reject) => {
    fs.readdir(path, (err, data) => {
      if (err) {
        rejects(err)
      }
      resolve(data)
    })
  })
}

const exclude = ['node_modules', '.gitignore']

const main = async (target, source) => {
  let list = await getDirList(source)
  const except = [path.basename(target), path.parse(__filename).base]
  list = list.filter((item) => {
    return !except.includes(item)
  })

  isDir(target) ? '' : fs.mkdirSync(target, { recursive: true })
  const copyFile = (list, target, source) => {
    const result = list.filter((item) => {
      return !exclude.includes(item)
    })
    result.forEach(async (item) => {
      const path =
        source.endsWith('./') || source.endsWith('/')
          ? './' + item
          : source + '/' + item // 原始路径

      const copyPath = target + '/' + item //复制的路径

      if (isDir(path)) {
        if (!fs.existsSync(copyPath)) {
          await fs.mkdirSync(copyPath)
          console.log(
            `正在复制:文件夹从${getPath(path)}------->复制到${getPath(
              copyPath
            )} ======成功`
          )
        }
        let needCopyList = await getDirList(path)

        await copyFile(needCopyList, copyPath, path)
      } else {
        if (!fs.existsSync(copyPath)) {
          fs.copyFileSync(path, copyPath)
          console.log(
            `正在复制:文件从${getPath(path)}------->复制到${getPath(
              copyPath
            )} ======成功`
          )
        }
      }
    })
  }

  copyFile(list, target, source)
}
const { target, source } = parseArgv()
main(target, source)
相关推荐
xd0000212 小时前
11. vue pinia 和react redux、jotai对比
node.js
程序猿小D13 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
前端老六喔21 小时前
🎉 开源项目推荐 | 让你的 TypeScript/React 项目瘦身更简单!
node.js·前端工程化
醉书生ꦿ℘゜এ1 天前
npm error Cannot read properties of null (reading ‘matches‘)
前端·npm·node.js
超级土豆粉1 天前
从0到1写一个适用于Node.js的User Agent生成库
linux·ubuntu·node.js
空中湖1 天前
‘pnpm‘ 不是内部或外部命令,也不是可运行的程序
npm·node.js
SailingCoder1 天前
grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
运维·人工智能·typescript·node.js·grafana
又又呢1 天前
前端面试题总结——webpack篇
前端·webpack·node.js
avoidaily2 天前
使用Node.js分片上传大文件到阿里云OSS
阿里云·node.js·云计算
xd000022 天前
8.axios Http网络请求库(1)
node.js