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)
相关推荐
samonyu2 小时前
fnm 简介及使用
前端·node.js
李游Leo13 小时前
Node.js 多版本管理与 nvm/nvs 使用全流程(含国内镜像加速与常见坑)
node.js
Q_Q196328847514 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
陈随易20 小时前
适合中国宝宝的AI编程神器,文心快码
前端·后端·node.js
Q_Q19632884751 天前
python+springboot大学生心理测评与分析系统 心理问卷测试 自动评分分析 可视化反馈系统
开发语言·spring boot·python·django·flask·node.js·php
EndingCoder1 天前
Electron 新特性:2025 版本更新解读
前端·javascript·缓存·electron·前端框架·node.js·桌面端
machinecat1 天前
node,小程序合成音频的方式
前端·node.js
草木红1 天前
express 框架基础和 EJS 模板
arcgis·node.js·express
亮子AI2 天前
【npm】npm 包更新工具 npm-check-updates (ncu)
前端·npm·node.js
Yvonne爱编码2 天前
构建高效协作的桥梁:前后端衔接实践与接口文档规范详解
前端·git·ajax·webpack·node.js