0.0.3 • Published 8 years ago

tree-match v0.0.3

Weekly downloads
5
License
ISC
Repository
github
Last release
8 years ago

tree-match

Match files using glob patterns

NPM Version Build Status Dependencies Status devDependencies Status

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-match

tree-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-get based distro)
apt-get install tree
  • openSUSE
zypper install tree
  • Fedora, CentOS, RHEL (and any other yum or dnf based distro):
## Fedora <= 21, CentOS, RHEL
yum install tree
## Fedora >= 22
dnf install tree
brew install tree
  • Windows
bin\tree.exe

Included 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:

  1. directory: the path of the folder that you want to search in, the current working directory is your app location;
  2. pattern: glob pattern, using minimatch, use only its features;
  3. options: minimatch's options with an extra field, maxDepth: int that translates to tree'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 in directory.

output is an Array containing the matched file names.

error is an Object with two fields:

  1. status: the exit code of tree;
  2. 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' }
});