tree-match-sync

0.0.2 • Public • Published

tree-match-sync

Match files using glob patterns synchronously

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 synchronously, matches files using glob patterns and then you can handle the result stored in a var 😉

Installation

$ npm install tree-match-sync

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

  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.

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:

  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');

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);
}

Readme

Keywords

Package Sidebar

Install

npm i tree-match-sync

Weekly Downloads

1

Version

0.0.2

License

ISC

Last publish

Collaborators

  • bored