适用于包括隐藏文件在内的所有内容的全局模式



我试图获得一个glob模式,它包括每个子目录中的每个文件,但我不知道如何包括隐藏文件。

例如,所有这些都应该匹配:

.git
.github/workflow.yml
index.js
src/index.js
src/components/index.js

这适用于所有具有名称和扩展名的文件,但忽略了隐藏文件:

**/**

更具体的背景:我想使用archiver库,用除node_modules之外的所有文件(可能还有其他一些文件(创建一个归档。

archive.directory("???", {
ignore: ["node_modules/", ...some other files],
});

最好的方法可能是使用两个独立的模式,一个匹配隐藏文件,另一个匹配非隐藏文件。一种方法是.* *。但是,这与目录本身.和父目录..相匹配,这通常不是您想要的。

避免这个问题的模式是.??* *。假设您的目录中有以下文件。

file1  file2  .hidden

正如您在下面的示例中看到的,这个glob模式匹配隐藏文件和非隐藏文件,但不匹配当前目录或其父目录。

$ ls -l .??* *
-rw-r--r-- 1 amy users 0 Jul 30 18:00 file1
-rw-r--r-- 1 amy users 0 Jul 30 18:00 file2
-rw-r--r-- 1 amy users 0 Jul 30 18:00 .hidden

我在使用archiver库时也遇到了同样的问题。我在文档中读到它用于glob模式的minmatch库。为特定问题提供选项的图书馆。这是文档中的位置。获取所有文件、目录(递归(和隐藏文件如";。npmrc";您需要使用";archive.glob而不是";archive.directory";。

我的代码如下:

archive.glob('**/*', {
cwd: __dirname,
dot: true,
ignore: ['node_modules/**', '<name of your zipfile>.zip']
});

我已经通过了";点:真";现在它还包括隐藏的文件。

最后的代码是这样的:

const fs = require('fs');
const archiver = require('archiver');
const zipname = "the name of your zipfile"
const output = fs.createWriteStream(__dirname + `/${zipname}.zip`);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function (err) {
throw err;
});
archive.pipe(output);
archive.glob('**/*', {
cwd: __dirname,
dot: true,
ignore: ['node_modules/**', `${zipname}.zip`]
}
);
archive.finalize();

最新更新