Introduction to Deglob
deglob
is a powerful Node.js utility that helps you manage and clean up global ignores for your coding projects. It provides a suite of useful APIs to improve your code quality and ensure consistency across your project files.
API Examples
deglob(patterns, [opts], callback)
This API is used to deglob specified patterns. It accepts an array of patterns, an optional options object, and a callback function.
const deglob = require('deglob');
deglob(['**/*.js', '!node_modules'], (err, files) => {
if (err) return console.error(err);
console.log('Files:', files);
});
Options Object
The options object can be used to customize deglob behavior. Below are some of the options available:
cwd
: Set the current working directory for the patterns.ignore
: Array of patterns to ignore.gitIgnoreFile
: Custom .gitignore file path.
const options = {
cwd: 'src',
ignore: ['test/**', '**/*.spec.js'],
gitIgnoreFile: '.custom-gitignore'
};
deglob(['**/*.js', '!node_modules'], options, (err, files) => {
if (err) return console.error(err);
console.log('Files with options:', files);
});
deglob.sync(patterns, [opts])
This is the synchronous version of the deglob API.
const deglob = require('deglob');
const files = deglob.sync(['**/*.js', '!node_modules']);
console.log('Sync Files:', files);
App Example with Deglob
Let’s look at an example where we use deglob in a Node.js application to find all JavaScript files excluding those in the node_modules and test directories.
const deglob = require('deglob');
function findFiles() {
const options = {
ignore: ['node_modules', 'test/**']
};
deglob(['**/*.js'], options, (err, files) => {
if (err) {
return console.error('Error:', err);
}
console.log('Discovered files:', files);
});
}
// Call the function to see the results
findFiles();
This example demonstrates how deglob can be integrated into a project to help manage file discovery and exclusion patterns effectively.
Hash: c0338a0ede65dbada3ed7f823c3279c0674f38b9750c8a543c8ca6476d4ffcf9