惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

Lovestu

WordPress 发布7.0:AI功能多,实则依托 - Lovestu AeroCore 自带小组件展示 - Lovestu Vite 8 性能实测:底层换上 Rust 后,前端构建到底有多狂? 使用WPOPT高级缓存功能为WordPress加速 - Lovestu 【WordPress优化插件】WPOPT v2.5.7 - Lovestu AeroCore 主题发布 - Lovestu CoreNext 正式版 1.7.9 发布 - Lovestu 粒子导航 - 一款独立的导航源码 1.4.95 - Lovestu Core AI Power 1.1.0 - WordPress AI 增强插件
NodeJS 使用copy-dir 快速复制内容 - Lovestu
applek · 2024-05-13 · via Lovestu

Node虽然自带有文件处理库,但是用起来不是特别方便。找了一下,发现一个第三方库,非常好用,现在就记录一下。

copy-dir

copy-dir是一款第三方的功能库,支持使用过滤器对复制文件的内容,进行过滤。将文件或目录复制到另一个路径,当目标路径或父目标路径不存在时,它还可以自动创建目录。

https://www.npmjs.com/package/copy-dir

使用方法

安装库

npm install copy-dir

 同步方法

这个很好用

copydir.sync(from, to[, options]);

参数说明

  • utimes: 默认为 false,如果设置为 true,则保留文件的访问和修改时间戳。
  • mode: 默认为 false,如果设置为 true,则保留文件的权限模式。
  • cover: 默认为 true,如果设置为 false,则在目标路径已存在同名文件时不会覆盖。
  • filter: 一个函数,用于过滤路径。返回 true 表示复制,返回 false 表示不复制。

示例

var copydir = require('copy-dir');
copydir.sync('/my/from/path', '/my/target/path', {
  utimes: true,
  mode: true,
  cover: true
});

这是简单的直接复制内容到指定文件夹了

可以添加过滤器,指定文件夹或者文件可以不要。在方法里面返回false就表示不复制

var path = require('path');
var copydir = require('copy-dir');
copydir.sync('/my/from/path', '/my/target/path', {
  filter: function(stat, filepath, filename) {
    if (stat === 'file' && path.extname(filepath) === '.html') {
      return false;
    }
    if (stat === 'directory' && filename === '.svn') {
      return false;
    }
    if (stat === 'symbolicLink') {
      return false;
    }
    return true;
  }
});
console.log('done');

 异步方法

var copydir = require('copy-dir');
copydir(from, to, [options, ]callback);

其中,call为回调函数

改造函数

果核改造一个方法出来,让函数更好用

dir:源文件

targetDir:目标文件

eliminate_dir:排除的文件夹,数组

eliminate_suffix:排除指定后缀名的文件,数组,例如:['.log','.txt']

eliminate_files:排除指定文件,完整的文件名,数组

function copy(dir, targetDir, eliminate_dir, eliminate_suffix, eliminate_files) {
    copy_dir.sync(dir, targetDir, {
        filter: (stat, filepath, filename) => {
            if (stat === 'directory') {
                return !eliminate_dir.includes(filename);
            }
            if (stat === 'file') {
                let suffix = path.extname(filepath);
                if (eliminate_suffix.includes(suffix)) {
                    return false;
                } else if (eliminate_files.includes(filename)) {
                    return false;
                }
                return true;
            }
        }
    });
}