0.0.2 • Published 10 years ago
tree-match-sync v0.0.2
tree-match-sync
Match files using glob patterns synchronously
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 synchronously, matches files using glob patterns and then you can handle the result stored in a var :wink:
Installation
$ npm install tree-match-synctree-match-sync 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-sync');
var tree = treeMatch(directory, pattern, options);
if(Object.prototype.toString.call(tree) === '[object Object]'){
console.error(tree);
}else{
console.log(tree);
}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.
The output may be an Array (all went good) or an Object (an error occured).
The Array contains the matched file names.
The Object has 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');
if(treeMatch.treeIsInstalled()){
// OK, tree is installed, do something!
}else{
// Well, tree isn't installed...
}It has no arguments and returns true/false.
Examples
var treeMatch = require('tree-match-sync');
var tree = treeMatch('./test', '**/*.js');
if(Object.prototype.toString.call(tree) === '[object Object]'){
console.error(tree);
}else{
console.log(tree);
// => [ 'test.js' ]
}var treeMatch = require('tree-match-sync');
var tree = treeMatch('./test', '**/*.js', { maxDepth: 0 });
if(Object.prototype.toString.call(tree) === '[object Object]'){
console.error(tree);
// => { status: 1, stderr: 'tree: Invalid level, must be greater than 0.\n' }
}else{
console.log(tree);
}