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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

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;
            }
        }
    });
}