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

发布于 2025-01-14 08:01:33 字数 541 浏览 2 评论 0原文

我尝试获取一个包含每个子目录中的每个文件的全局模式,但我不知道如何包含隐藏文件。

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

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

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

**/**

更具体的背景:我想用除 node_modules 之外的所有文件(以及可能的其他文件)制作一个存档),使用 archiver 库。

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

I try to get a glob pattern which includes every file in every subdirectory, but I can't figure out how to include hidden files.

Example, all those should match:

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

This works for all files with name and extension, but leaves out hidden files:

**/**

More specific background: I want so make an archive with all files except node_modules (and potentially some others), using the archiver library.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2025-01-21 08:01:33

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

我的代码如下所示:

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

我传递了选项“dot: true”现在它还包含隐藏文件,

最终代码如下所示:

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

I was facing the same problem when using the archiver library. I read in the docs that it is using for the glob pattern the minmatch library. The library offering an option for the specific issue. Here is the location in the docs. To get all files, directories (recursive) and hidden files like ".npmrc" you need to use "archive.glob instead of "archive.directory".

My code looks like this:

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

I've passed the option "dot: true" and now it includes hidden files as well.

The final code would look like this:

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();
最舍不得你 2025-01-21 08:01:33

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

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

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

The best way is probably to use two separate patterns, one of which matches the hidden files, and one which matches the non-hidden files.
One way to do this is .* *. However, this matches the directory itself . and parent .. as well, which is usually not what you want.

A pattern which avoids this problem is .??* *. Suppose you have the following files in your directory.

file1  file2  .hidden

As you can see in the example below, this glob pattern matches both hidden files and non-hidden files, but not the current directory or its parent.

$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文