What is findit?
The 'findit' npm package is a simple and efficient tool for recursively searching through directories to find files and directories. It emits events for each file and directory it encounters, making it easy to handle file system traversal in a Node.js application.
What are findit's main functionalities?
Finding Files
This feature allows you to search for files within a specified directory. The 'file' event is emitted for each file found, and you can handle it by logging the file path or performing other operations.
const findit = require('findit');
const finder = findit('/path/to/search');
finder.on('file', (file, stat) => {
console.log('File: ' + file);
});
Finding Directories
This feature allows you to search for directories within a specified directory. The 'directory' event is emitted for each directory found, and you can handle it by logging the directory path or performing other operations.
const findit = require('findit');
const finder = findit('/path/to/search');
finder.on('directory', (dir, stat, stop) => {
console.log('Directory: ' + dir);
});
Handling Errors
This feature allows you to handle errors that occur during the search process. The 'error' event is emitted when an error is encountered, and you can handle it by logging the error or performing other error-handling operations.
const findit = require('findit');
const finder = findit('/path/to/search');
finder.on('error', (err) => {
console.error('Error: ' + err);
});
Stopping the Search
This feature allows you to stop the search process based on certain conditions. The 'stop' function can be called within the 'directory' event handler to halt the search when a specific directory is encountered.
const findit = require('findit');
const finder = findit('/path/to/search');
finder.on('directory', (dir, stat, stop) => {
if (dir === '/path/to/stop') stop();
});
Other packages similar to findit
glob
The 'glob' package allows for pattern matching and file searching using glob patterns. It is more flexible in terms of specifying patterns for file matching compared to 'findit', but it may be less efficient for large directory trees.
readdirp
The 'readdirp' package provides a recursive version of 'fs.readdir'. It offers a more modern API with promises and streams, making it easier to integrate with async/await syntax. It is similar to 'findit' in terms of functionality but provides more modern features.
walk
The 'walk' package is another alternative for recursively walking through directories. It provides a similar event-based API to 'findit' but includes additional features like filtering and limiting the depth of the search.
findit
Recursively walk directory trees. Think /usr/bin/find
.
example time!
callback style
require('findit').find(__dirname, function (file) {
console.log(file);
})
emitter style
var finder = require('findit').find(__dirname);
finder.on('directory', function (dir, stat) {
console.log(dir + '/');
});
finder.on('file', function (file, stat) {
console.log(file);
});
finder.on('link', function (link, stat) {
console.log(link);
});
synchronous
var files = require('findit').sync(__dirname);
console.dir(files);
methods
find(basedir, options, cb)
Do an asynchronous recursive walk starting at basedir
.
Optionally supply an options object. Setting the property 'follow_symlinks'
will follow symlinks.
Optionally supply a callback that will get the same arguments as the path event
documented below in "events".
If basedir
is actually a non-directory regular file, findit emits a single
"file" event for it then emits "end".
Findit uses fs.lstat()
so symlinks are not traversed automatically. To have it
follow symlinks, supply the options argument with 'follow_symlinks' set to true.
Findit won't traverse an inode that it has seen before so directories can have
symlink cycles and findit won't blow up.
Returns an EventEmitter. See "events".
sync(basedir, options, cb)
Return an array of files and directories from a synchronous recursive walk
starting at basedir
.
Optionally supply an options object. Setting the property 'follow_symlinks'
will follow symlinks.
An optional callback cb
will get called with cb(file, stat)
if specified.
events
file: [ file, stat ]
Emitted for just files which are not directories.
directory : [ directory, stat ]
Emitted for directories.
path : [ file, stat ]
Emitted for both files and directories.
end
Emitted when the recursive walk is done.