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],
});
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With