Coder Social home page Coder Social logo

micromatch / glob-fs Goto Github PK

View Code? Open in Web Editor NEW
55.0 11.0 17.0 107 KB

file globbing for node.js. speedy and powerful alternative to node-glob. This library is experimental and does not work on windows!

Home Page: http://jonschlinkert.github.io/glob-fs

License: MIT License

JavaScript 100.00%
glob-pattern glob fs files patterns match minimatch micromatch multimatch node-glob

glob-fs's Introduction

glob-fs NPM version

file globbing for node.js. speedy and powerful alternative to node-glob.

Usage

var glob = require('glob-fs')({ gitignore: true });
var files = glob.readdirSync('**/*.js');

Run actual examples:

Jump to docs sections:

Table of contents

(Table of contents generated by verb)

Install

Install with npm

$ npm i glob-fs --save

Usage

Params

All "read" methods take a glob pattern and an options object.

  • pattern {String}: Glob pattern to use for matching. (multiple pattern support is planned)
  • options {Object}: Options for glob-fs or middleware.

Examples:

// sync
var files = glob.readdirSync('*.js', {});

// async
glob.readdir('*.js', function(err, files) {
  console.log(files);
});

// stream
glob.readdirStream('*.js', {})
  .on('data', function(file) {
    console.log(file);
  });

// promise
glob.readdirPromise('*.js')
  .then(function(files) {
    console.log(file);
  });

API

Asynchronously glob files or directories that match the given pattern.

Params

  • pattern {String}: Glob pattern
  • options {Object}
  • cb {Function}: Callback

Example

var glob = require('glob-fs')({ gitignore: true });

glob.readdir('*.js', function (err, files) {
  //=> do stuff with `files`
});

Synchronously glob files or directories that match the given pattern.

Params

  • pattern {String}: Glob pattern
  • options {Object}
  • returns {Array}: Returns an array of files.

Example

var glob = require('glob-fs')({ gitignore: true });

var files = glob.readdirSync('*.js');
//=> do stuff with `files`

Stream files or directories that match the given glob pattern.

Params

  • pattern {String}: Glob pattern
  • options {Object}
  • returns {Stream}

Example

var glob = require('glob-fs')({ gitignore: true });

glob.readdirStream('*.js')
  .on('data', function (file) {
    console.log(file.path);
  })
  .on('error', console.error)
  .on('end', function () {
    console.log('end');
  });

Optionally create an instance of Glob with the given options.

Params

  • options {Object}

Example

var Glob = require('glob-fs').Glob;
var glob = new Glob();

Add a middleware to be called in the order defined.

Params

  • fn {Function}
  • returns {Object}: Returns the Glob instance, for chaining.

Example

var gitignore = require('glob-fs-gitignore');
var dotfiles = require('glob-fs-dotfiles');
var glob = require('glob-fs')({ foo: true })
  .use(gitignore())
  .use(dotfiles());

var files = glob.readdirSync('**');

Thin wrapper around .use() for easily excluding files or directories that match the given pattern.

Params

  • pattern {String}
  • options {Object}

Example

var gitignore = require('glob-fs-gitignore');
var dotfiles = require('glob-fs-dotfiles');
var glob = require('glob-fs')()
  .exclude(/\.foo$/)
  .exclude('*.bar')
  .exclude('*.baz');

var files = glob.readdirSync('**');
//=> ['index.js', 'README.md', ...]

Middleware

glob-fs uses middleware to add file matching and exclusion capabilities, or other features that may or may not eventually become core functionality.

What is a middleware?

A middleware is a function that "processes" files as they're read from the file system by glob-fs.

Additionally, middleware can:

  • be chained
  • include or exclude a file based on some condition, like whether or not one of its properties matches a regex or glob pattern.
  • determine whether or not to continue recursing in a specific directory
  • modifying an existing property to the file object
  • add a new property to the file object

Middleware examples

Ignoring files

In the following example, notemp is a complete and functional middleware for excluding any filepath that has the substring temp:

var glob = require('glob-fs')();

function notemp(file) {
  if (/temp/.test(file.path)) {
    file.exclude = true;
  }
  return file;
}

glob.use(notemp)
  .readdirStream('**/*.js')
  .on('data', function(file) {
    console.log(file.relative);
  });

Matching

Pattern matching is done by default in glob-fs, but you get disable the built-in matchers or get more specific by adding a middleware that uses [micromatch][] or minimatch for matching files.

var glob = require('glob-fs')({ gitignore: true });
var mm = require('micromatch');

glob.use(function(file) {
    if (mm.isMatch(file.relative, 'vendor/**')) file.exclude = true;
    return file;
  })
  .readdirStream('**/*.js')
  .on('data', function(file) {
    console.log(file.relative);
  });

recursion

Here is how a middleware might determine whether or not to recurse based on a certain pattern:

var glob = require('glob-fs')();

// this specific check is already done by glob-fs, it's just used here as an example 
function recurse(file) {
  // `file.pattern` is an object with a `glob` (string) property
  file.recurse = file.pattern.glob.indexOf('**') !== -1;
  return file;
}

// use the middleware
glob.use(recurse)
  .readdir('**/*.js', function(err, files) {
    console.log(files);
  });

Built-in middleware

Currently glob-fs includes and runs the following middleware automatically:

  • glob-fs-dotfiles: glob-fs middleware for automatically ignoring dotfiles.
  • glob-fs-gitignore: glob-fs middleware for automatically ignoring files specified in .gitignore

Disabling built-ins

To disable built-in middleware and prevent them from running, pass builtins: false on the global options. This will disable all built-in middleware.

Example:

var glob = require('glob-fs')({builtins: false});

To disable a specific middleware from running, you can usually pass the name of the middleware on the options, like dotfiles: false, but it's best to check the readme of that middleware for specifics.

Middleware conventions

  • Naming: any middleware published to npm should be prefixed with glob-fs-, as in: glob-fs-dotfiles.
  • Keywords: please add glob-fs to the keywords array in package.json
  • Options: all middleware should return a function that takes an options object, as in the Middleware Example
  • Return file: all middleware should return the file object after processing.

Advice for middleware authors

  • A middleware should only do one specific thing.
  • Multiple middleware libs can be bundled together to create a single middleware.
  • Pattern matching should be extremely specific. Don't force downstream middleware to reverse your mistakes.
  • As mentioned in the middleware conventions section, always return the file object.
  • A single conditional should only set file.exclude to true, or file.include to true, never both.
  • It's completely okay to check this.options
  • Middleware modules should be fully documented.

Globbing examples

Note that the gitignore option is already true by default, it's just shown here as a placeholder for how options may be defined.

async

var glob = require('glob-fs')({ gitignore: true });

glob.readdir('**/*.js', function(err, files) {
  console.log(files);
});

promise

var glob = require('glob-fs')({ gitignore: true });

glob.readdirPromise('**/*')
  .then(function (files) {
    console.log(files);
  });

stream

var glob = require('glob-fs')({ gitignore: true });

glob.readdirStream('**/*')
  .on('data', function (file) {
    console.log(file.path);
  })

sync

var glob = require('glob-fs')({ gitignore: true });

var files = glob.readdirSync('**/*.js');
console.log(files);

Events

(WIP)

The following events are emitted with all "read" methods:

  • read: emitted immediately before an iterator calls the first middleware.
  • include: emits a file object when it's matched
  • exclude: emits a file object when it's ignored/excluded
  • file: emits a file object when the iterator pushes it into the results array. Only applies to sync, async and promise.
  • dir: emits a file object when the iterator finds a directory
  • end when the iterator is finished reading
  • error on errors

Event examples

async

var glob = require('..')({ gitignore: true });

glob.on('dir', function (file) {
  console.log(file);
});

glob.readdir('**/*.js', function (err, files) {
  if (err) return console.error(err);
  console.log(files.length);
});

promise

var glob = require('glob-fs')({ gitignore: true });

glob.on('include', function (file) {
  console.log('including:', file.path);
});

glob.on('exclude', function (file) {
  console.log('excluding:', file.path);
});

glob.readdirPromise('**/*');

sync

Also has an example of a custom event, emitted from a middleware:

var glob = require('glob-fs')({ gitignore: true })
  .use(function (file) {
    if (/\.js$/.test(file.path)) {
      // custom event
      this.emit('js', file);
    }
    return file;
  });

glob.on('js', function (file) {
  console.log('js file:', file.path);
});

glob.on('exclude', function (file) {
  console.log('excluded:', i.excludes++);
});

glob.on('include', function (file) {
  console.log('included:', i.includes++)
});

glob.on('end', function () {
  console.log('total files:', this.files.length);
});

glob.readdirSync('**/*.js');

stream

var glob = require('glob-fs')({ gitignore: true })

glob.readdirStream('**/*')
  .on('data', function (file) {
    console.log(file.path)
  })
  .on('error', console.error)
  .on('end', function () {
    console.log('end');
  });

FAQ

  • when files are read from the file system, an object is created to keep a record of the file's path, dirname, and fs stat object and other pertinent information that makes it easier to make decisions about inclusion and exclusion later on.
  • file objects are decorated with a parse method that is used to calculate the file.relative and file.absolute properties.
  • the file.parse() method is called in the iterator, right after the call to fs.stats and just before the call to the middleware handler (.handle()). This ensures that all middleware have access to necessary path information.
  • file.relative is the file path that's actually pushed into the files array that is ultimately returned.
  • file.relative is calculated using path.relative(file.path, cwd), where cwd is passed on the options (globally, or on a middleware), and file.path is typically the absolute, actual file path to the file being globbed.

TODO

middleware

  • middleware
  • middleware handler
  • externalize middleware to modules (started, prs welcome!)

events

  • events

tests

  • unit tests (need to be moved)

iterators

  • sync iterator
  • async iterator
  • stream iterator
  • promise iterator

read methods

  • glob.readdir (async)
  • glob.readdirSync
  • glob.readdirStream
  • glob.readdirPromise

patterns

  • Multiple pattern support. will need to change pattern handling, middleware handling. this is POC currently
  • Negation patterns (might not do this, since it can be handled in middleware)
  • matching method, memoized/cached/bound to a glob pattern or patterns, so it can be reused without having to recompile the regex.

other

  • clean up ./lib
  • comparsion to [node-glob][]

Community middleware

(Add your project to the .verb.md template do a PR!)

  • glob-fs-dotfiles: glob-fs middleware for automatically ignoring dotfiles.
  • glob-fs-gitignore: glob-fs middleware for automatically ignoring files specified in .gitignore

Related projects

  • braces: Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 bracesโ€ฆ more
  • fill-range: Fill in a range of numbers or letters, optionally passing an increment or multiplier toโ€ฆ more
  • is-glob: Returns true if the given string looks like a glob pattern.
  • micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. Justโ€ฆ more

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright ยฉ 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on July 11, 2015.

glob-fs's People

Contributors

doowb avatar jonschlinkert avatar seggev319 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

glob-fs's Issues

TypeError: Cannot read property 'realpath' of undefined

I get the below when using readdirPromise:
/Users/scott/Dropbox/Docs/Scripts/mail-restore-js/node_modules/glob-fs/lib/symlinks.js:38
self.realpath(file, cb);
^

TypeError: Cannot read property 'realpath' of undefined
    at /Users/scott/Dropbox/Docs/Scripts/mail-restore-js/node_modules/glob-fs/lib/symlinks.js:38:15
    at LOOP (fs.js:1609:14)
    at _combinedTickCallback (node.js:376:9)
    at process._tickCallback (node.js:407:11)

cannot install with npm v5: No matching version found for [email protected]

$ node -v
v4.8.3
$ npm -v
5.0.1
$ npm i -g glob-fs
npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected]
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! notarget 
npm ERR! notarget It was specified as a dependency of 'glob-fs'
npm ERR! notarget 

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2017-06-02T20_41_06_057Z-debug.log

Node.js was installed via nvm, and I upgraded npm to v5.

readdirSync is not a function

error: /Users/craigcosmo/Desktop/electron skeleton/webpack.config.babel.js:49
var files = _globFs2.default.readdirSync('*.js');
^

TypeError: _globFs2.default.readdirSync is not a function

Reproduce

import fs from 'fs'
import glob from 'glob-fs'
let files = glob.readdirSync('*.js');

console.log(files)

README should be clear that this requires git to be on the PATH

It would be really great if the README made it clear that this package requires git to be on the path as well as carrying the "experimental" and "doesn't work on Windows" warnings which are shown on github. This would help make sure that devs who discover the package on www.npmjs.org are aware of these limitations before they spend time trying to try out the package.

Thanks.

middlewares to be async

Im working on tunnckoCore/benz, which i think is awesome.
Because lies on top of great libs - async-done, now-and-later and co. Allows you to pass async, callback, generator functions, passing context through them; returning and yielding promises, thunks streams; handles errors and completion of all of them.

You can look at the tests for now. Also want to mention that it is almost like bach, but bach dont have options and flexibility that i need and want. You cant pass context to all of the functions with it. With benz you can pass context and can pass result of one middleware to next if you want. Support running on parallel and on series. Just look the tests, please :)

I will try to implement also to accept sync functions. It is little bit more work cuz promises and streams looks like sync functions, so it is lil' bit tricky to determine which is stream, which is sync, which is promise. But I believe it is possible.

You can try benz now (v0.2.0) here in glob-fs instead of custom calling middlewares - its ready, just need more tests and docs that I will add today.

With the title i mean like this

var glob = require('glob-fs')()

glob.use(function notemp(file, next) {
  if (/temp/.test(file.path)) {
    file.exclude = true
  }
  next(null, file)
})

or to be possible through option like async: true?

middleware ideas

Anyone is free to create these. Middleware should be extremely specific. this keeps them very fast, more composable, and more likely to be interesting to more users

Please add a link to your middleware in the readme if you create one!

  • glob-fs-tilde: expand a leading tilde to an absolute file path (using expand-tilde)
  • glob-fs-bower-ignore: ignore files or directories specified in bower.json
  • glob-fs-ignore-tests: ignore test files/fixtures automatically.

Any more ideas?

Glob pattern is not filtering files as expected

Hi.
I was expecting glob to recursively list all files and (internally) reject all that don't match provided pattern. However, given following actions taken:

git clone https://github.com/ama-team/voxengine-sdk.git
cd voxengine-sdk
git checkout f2a0af
npm i glob-fs -D # installed version: 0.1.7
cat <<EOF > index.js   
var glob = require('glob-fs')
glob().readdirPromise('test/**/*.spec.js')
  .then(console.log.bind(console));
EOF
node index.js

I have following output:

[
  '/tmp/voxengine-sdk/test/mocha.opts', // unexpected
  '/tmp/voxengine-sdk/test/spec/http/_common.spec.js',
  '/tmp/voxengine-sdk/test/spec/http/basic.spec.js',
  '/tmp/voxengine-sdk/test/spec/http/rest.spec.js',
  '/tmp/voxengine-sdk/test/spec/logger/_common.spec.js',
  '/tmp/voxengine-sdk/test/spec/logger/slf4j.spec.js',
  '/tmp/voxengine-sdk/test/support/mocha-multi-reporters.json', // unexpected
  '/tmp/voxengine-sdk/test/support/setup.js' // unexpected
]

Looks like it's either me misinterpreting how this package is designed to work (and all of the above is valid output) or a bug

readdirSync is returning files from wrong path

given

``` debug(processing files at path ${process.cwd()} );

let fileProcessor = new FileProcessor(this._config);
fileProcessor.rootPath = process.cwd();
let files = glob.readdirSync(this._config.globPattern);
files.forEach( (file) => {```

where the process.cwd() is /Users/georgecook/Documents/h7ci/hope/opensource/navSpike/build/.roku-deploy-staging

and a globPattern of **/*.brs

I find the returned files have a path of .roku-deploy-staging/components/Framework/Core/BaseAggregateView.brs - this is wrong! that folder doesn't even exist - it's as if readdirSync is running the glob against build instead of build.roku-deploy-staging

I'll have to stop using this package if I can't get around this, as it's breaking my tools.

npm audit give high status vulnerability

running npm audit with latest glob-fs gives:

High Prototype Pollution
Package set-value
Patched in >=2.0.1 <3.0.0 || >=3.0.1

Would you be updating package.json to use latest set-value (currently it is ^0.2.0).

this.pattern.re vs: this.pattern.regex

When trying to use glob-fs 0.1.6 just right out of the box I had to do the following modification in index.js, to get it running:

    // if middleware are registered, use the glob, otherwise regex
    var glob = this.fns.length
      ? this.pattern.glob
      //: this.pattern.re; //<-- error
      : this.pattern.regex; //<-- fix

npm ERR! Does not contain a package.json file

Facing issue on package installation.

npm ERR! code ENOLOCAL
npm ERR! Could not install from "node_modules/glob-fs/micromatch@github:jonschlinkert/micromatch#5017fd78202e04c684cc31d3c2fb1f469ea222ff" as it does not contain a package.json file.

Getting this Error on npm install
pacakge.json contains
"glob-fs": "^0.1.7",

node - 8.10.0
npm - 6.14.9

Please help with this issue.
@jonschlinkert

readdirAsync doesn't filter

When I try this

require('glob-fs')().readdirPromise('**/*.yaml').then((_)=>console.log(_))

I see just just all files in dir

I suppose that error is somewhere here:

    readdirPromise: function(pattern, options) {
      this.emit('read');
      this.setPattern(pattern, options);
      var res = this.iteratorPromise(this.pattern.base);  
      this.emit('end', this.files);
      return res;
    }

because it's strange to me that end is emitted before promise is resolved.

but change from this:

      this.emit('end', this.files);

to this:

      res.then(() => this.emit('end', this.files));

didn't help

Did I used this method properly? Or am I missing somethig? Or is there some error?

TypeError: Cannot read property 'split' of undefined

When trying to run the sync example from the website, I get the following error:

    return fp.split('\\').join('/');
             ^

TypeError: Cannot read property 'split' of undefined

For reference, here is the code:

var glob = require('glob-fs')({ gitignore: true });

var files = glob.readdirSync('**/*.js');
console.log(files);

My hunch is it involves line 74 of glob-fs/lib/pattern.js: this.base = path.join(this.cwd, this.parent);. Running the debugger, the path.join returns a nonsense path (the c:/ drive is in both this.cwd and this.parent and path.join just smashes them together).

Even if I fix the issue of this.pattern.re vs this.pattern.regex, I still get the same 'fp is undefined' error.

Full absolute path

Why does the module prepend it's own path to the passed path in readdirSync?

Surely this is an oversight?

How does one scan a different folder?

All the examples show scanning the default folder. The docs vaguely indicate that I can modify glob.options.cwd, but it doesn't seem to do the trick. Specifying an absolute folder like /users/Kristian/** doesn't work either. Am I supposed to run process.chdir() to scan a specific folder?

first pass

@doowb / @tunnckoCore

I just pushed up a first pass at this. there is still a lot of work to be done but I think the basic conventions are a good start. I'd love feedback on the following:

  • multiple pattern support. suggestions for approach - obviously there are a few existing solutions that do this: globby, micromatch, multimatch etc. but I wanted to see if you guys had any ideas for how we could make it more dynamic. I decided to use a file object in glob-fs, since it really simplifies options handling, etc. I'm thinking that we might be able to implement some interesting ignore/unignore features that leverage this. maybe even something like the stash in jade/css/stylus.
  • iterators: what can we do to make iterators smarter without making them more complicated? e.g. iterators should be able to passively include, passively exclude, actively include and actively exclude files based on configuration options, pattern matching, etc.

any other thoughts would be great!

micromatch dependancy pulls from github not npm

The dependency for: "micromatch": "jonschlinkert/micromatch#2.2.0", pulls from github repo instead of npm. This breaks on firewalled networks that do not have access to public github. Can this be changed to pull from npm instead?

Multiple calls to readdirSync adds to the returned array

If I call globFs.readdirSync(path, {cwd: '/'}) multiple times I get a different array each time. The file list is being added to the last response each time.

Note I'm using the cwd param because I'm passing in absolute paths.

wishlist

Wishlist

Some user features that would be nice to have in a node.js file globbing lib:

  • define patterns as a string or array
  • allow ignore patterns to be passed on the options
  • ignore node_modules by default. IMO it makes more sense for a user to explicitly define node_modules to include it.
  • 
    

API

  • streams
  • async
  • sync

Code

  • how much flexibility to we want to provide for negation patterns?
  • is it important to allow users to un-negate previously negated patterns? If not, things become simpler, but I don't think we'll need to compromise on speed either way

readdir case-insensitive wildcard

I work in Windows.

How would I go about looking for files like, for example, both:
*.txt and *.TXT

glob.readdir returns either the one or the other, but not both.

For example I have 2 files:
file1.txt
file2.TXT

Now, I want to find all files with extension txt (Upper and Lower case). How would I do that without calling 2 functions (*.txt and *.TXT) ?

lacking in glob pattern recognition

I have used glob-fs in my program for a faster glob experience. The full pattern recognition was not available. I ran many attempts with patterns similar to *!(.js) and every result included .js. This is unfortunate as this looks like a promising library.

Replace micromatch github dependency

Could you please replace the micromatch github- by a npm-dependency?
This leads to trouble by firewall restrictions or deployments through inhouse npm caches. Thx!

async iterator example?

The TODO section of the readme says that async iterator support is done, but I don't see any examples in the readme...is it possible to for await with the current API?

Problem using brace matcher

I hope this is just me not understanding glob syntax. I seem to be able to read test and src separately, but not together, joined in braces.

> var glob = require('glob-fs')();
undefined

> glob.readdirStream('../foopath/test/**').on('data', function(file) { console.log(file.path); }), null;
null
> /Users/steveluscher/.../foopath/test/testOneFile.js
/Users/steveluscher/.../foopath/test/testTwoFile.js

> glob.readdirStream('../foopath/src/**').on('data', function(file) { console.log(file.path); }), null;
null
> /Users/steveluscher/.../foopath/src/srcOneFile.js
/Users/steveluscher/.../foopath/src/srcTwoFile.js

> glob.readdirStream('{../foopath/src,../foopath/test}/**').on('data', function(file) { console.log(file.path); }), null;
null

Note that the same glob pattern used with ls lists all four files:

ls -al {../foopath/src,../foopath/test}/**

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.