0.0.3 • Published 10 years ago
tree-match v0.0.3
tree-match
Match files using glob patterns
You don't have to use the File System API to find files, just use this module!
Under the hood it spawns a tree command, matches files using glob patterns and then you can handle the result with a Promise API :wink:
Installation
$ npm install tree-matchtree-match depends on the tree command, if it's not installed run one of these commands based on your OS:
- Debian, Ubuntu (and any other
apt-getbased distro)
apt-get install tree- openSUSE
zypper install tree- Fedora, CentOS, RHEL (and any other
yumordnfbased distro):
## Fedora <= 21, CentOS, RHEL
yum install tree
## Fedora >= 22
dnf install tree- Mac OS, with Homebrew
brew install tree- Windows
bin\tree.exeIncluded in the package, provided by GnuWin32.
Usage
var treeMatch = require('tree-match');
treeMatch(directory, pattern, options)
.then(function(output){
}).catch(function(error){
});As you can see there are three arguments:
directory: the path of the folder that you want to search in, the current working directory is your app location;pattern: glob pattern, using minimatch, use only its features;options: minimatch's options with an extra field,maxDepth: intthat translates totree's option-L level-Descend only level directories deep. I recommend you to always set this field when you don't know how many files may be contained indirectory.
output is an Array containing the matched file names.
error is an Object with two fields:
status: the exit code oftree;stderr: the error message.
There's also a function that controls if the tree command is installed:
var treeMatch = require('tree-match');
treeMatch.treeIsInstalled()
.then(function(){
// OK, tree is installed, do something!
}).catch(function(){
// Well, tree isn't installed...
});It has no arguments and returns nothing.
Examples
var treeMatch = require('tree-match');
treeMatch('./test', '**/*.js').then(function(output){
console.log(output);
// => [ 'test.js' ]
}).catch(function(error){
console.error(error);
});var treeMatch = require('tree-match');
treeMatch('./test', '**/*.js', { maxDepth: 0 }).then(function(output){
console.log(output);
}).catch(function(error){
console.error(error);
// => { status: 1, stderr: 'tree: Invalid level, must be greater than 0.\n' }
});