What is fstream-ignore?
The fstream-ignore npm package is a specialized file stream filter that allows you to include or exclude files based on .gitignore-style rules. It is particularly useful for creating tarballs, zips, or other file archives where you need to exclude certain files or directories.
What are fstream-ignore's main functionalities?
Filtering files based on .gitignore rules
This feature allows you to filter files in a directory based on rules specified in .gitignore files. The code sample demonstrates how to create an Ignore stream that reads from a directory and applies the ignore rules.
const Ignore = require('fstream-ignore');
const ignore = new Ignore({ path: 'path/to/dir', ignoreFiles: ['.gitignore'] });
ignore.on('child', function (c) {
console.error(c.path);
});
ignore.on('end', function () {
console.error('done');
});
Custom ignore rules
This feature allows you to specify custom ignore rules directly in the code, without relying on an external .gitignore file. The code sample shows how to create an Ignore stream with custom rules to exclude .log files and the node_modules directory.
const Ignore = require('fstream-ignore');
const ignore = new Ignore({ path: 'path/to/dir', ignoreRules: ['*.log', 'node_modules/'] });
ignore.on('child', function (c) {
console.error(c.path);
});
ignore.on('end', function () {
console.error('done');
});
Other packages similar to fstream-ignore
ignore
The ignore package is a standalone utility for handling .gitignore-style rules. It provides a more flexible and powerful API for managing ignore rules, but it does not integrate directly with file streams like fstream-ignore.
minimatch
The minimatch package is a glob matching library that can be used to match file paths against patterns. While it does not specifically handle .gitignore rules, it can be used to implement similar functionality with custom logic.
glob
The glob package is a file matching library that allows you to find files based on glob patterns. It can be used in conjunction with ignore rules to filter files, but it does not natively support .gitignore-style rules.
fstream-ignore
A fstream DirReader that filters out files that match globs in .ignore
files throughout the tree, like how git ignores files based on a
.gitignore
file.
Here's an example:
var Ignore = require("fstream-ignore")
Ignore({ path: __dirname
, ignoreFiles: [".ignore", ".gitignore"]
})
.on("child", function (c) {
console.error(c.path.substr(c.root.path.length + 1))
})
.pipe(tar.Pack())
.pipe(fs.createWriteStream("foo.tar"))
This will tar up the files in __dirname into foo.tar
, ignoring
anything matched by the globs in any .iginore or .gitignore file.