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)
相关推荐
集成显卡10 小时前
基于 Node.js 的 API 方式接入深度求索Deepseek、字节跳动豆包大模型
前端·人工智能·node.js
HexCIer11 小时前
cbT.js: 一个让模板继承变得优雅的 Node.js 模板引擎
javascript·node.js
Q_Q51100828514 小时前
python的小学课外综合管理系统
开发语言·spring boot·python·django·flask·node.js
ChongYu重玉17 小时前
【node/vue】css制作可3D旋转倾斜的图片,朝向鼠标
javascript·css·vue.js·经验分享·笔记·node.js·vue
前端双越老师17 小时前
使用 langChain.js 实现 RAG 知识库语义搜索
人工智能·langchain·node.js
植物昂光17 小时前
基于Node.js的微博热榜抓取与展示开发记录
前端·node.js
今天也在写bug19 小时前
输入npm install后发生了什么
前端·npm·node.js
popoxf1 天前
在新版本的微信开发者工具中使用npm包
前端·npm·node.js
Q_Q19632884751 天前
python的平安驾校管理系统
开发语言·spring boot·python·django·flask·node.js·php
GISer_Jing1 天前
LLM对话框项目总结II
前端·javascript·node.js