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)
相关推荐
星空下的曙光3 小时前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法
青灬河9 小时前
实现企业级全栈应用服务框架-Elpis(一)
vue.js·node.js
星空下的曙光11 小时前
Node.js events模块所有 API 详解 + 常用 API + 使用场景
node.js
无责任此方_修行中11 小时前
我的两次 Vibe Coding 经历,一次天堂,一次地狱
后端·node.js·vibecoding
程序铺子16 小时前
如何使用 npm 安装 sqlite3 和 canvas 这些包
javascript·npm·node.js
星空下的曙光17 小时前
Node.js 事件循环(Event Loop)
node.js
勤奋菲菲1 天前
Egg.js 完全指南:企业级 Node.js 应用框架
开发语言·javascript·node.js
aesthetician1 天前
Node.js 24.10.0: 拥抱现代 JavaScript 与增强性能
开发语言·javascript·node.js
APItesterCris2 天前
Node.js/Python 实战:编写一个淘宝商品数据采集器
大数据·开发语言·数据库·node.js
勤奋菲菲2 天前
Koa.js 完全指南:下一代 Node.js Web 框架
前端·javascript·node.js