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)
相关推荐
小领航2 小时前
构建 MySQL MCP Server
人工智能·node.js
Patrick_Wilson4 小时前
Vite+ vs nvm:一次「全局 CLI 失踪」事故引出的 Node 工具链选型
node.js·vite·前端工程化
用户1558319968141 天前
用Node写一个文件同步CLI工具
node.js
李白的天不白1 天前
webpack 压缩文件
前端·webpack·node.js
zzzzzz3101 天前
AI Agent 开发实战:从零构建智能代码助手
react.js·node.js
donecoding1 天前
用了多年 nvm,我终于找到 Python 的版本管理「答案」:uv
python·node.js·前端工程化
南城雨落1 天前
uni-app开发经验分享-跨端开发经验总结
javascript·vue.js·node.js
子兮曰3 天前
Node.js v26.1.0 深度解读:FFI、后量子密码与调试器的进化
前端·后端·node.js
大家的林语冰3 天前
Node 2026 发布,JS 三大新功能上线,最后一个奇偶版本
前端·javascript·node.js
Aolith3 天前
从裸奔到加固:我的校园论坛网络安全实战
node.js·全栈