[Pkg-javascript-commits] [node-fileset] 01/02: Imported Upstream version 0.1.5
Leo Iannacone
l3on-guest at moszumanska.debian.org
Wed Jun 4 12:07:45 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-fileset.
commit 63e201f3cbd3e277f57673d1d5993add29aa2100
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Wed Jun 4 12:20:52 2014 +0200
Imported Upstream version 0.1.5
---
.travis.yml | 5 ++
LICENSE-MIT | 22 ++++++
README.md | 87 +++++++++++++++++++++++
lib/fileset.js | 64 +++++++++++++++++
package.json | 23 +++++++
tests/fixtures/an (odd) filename.js | 1 +
tests/helper.js | 61 +++++++++++++++++
tests/test.js | 133 ++++++++++++++++++++++++++++++++++++
8 files changed, 396 insertions(+)
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..b740293
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+
+node_js:
+ - 0.4
+ - 0.6
\ No newline at end of file
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..e6f8599
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Mickael Daniel
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c6ec211
--- /dev/null
+++ b/README.md
@@ -0,0 +1,87 @@
+# node-fileset
+
+Exposes a basic wrapper on top of
+[Glob](https://github.com/isaacs/node-glob) /
+[minimatch](https://github.com/isaacs/minimatch) combo both written by
+ at isaacs. Glob now uses JavaScript instead of C++ bindings which makes it
+usable in Node.js 0.6.x and Windows platforms.
+
+[![Build Status](https://secure.travis-ci.org/mklabs/node-fileset.png)](http://travis-ci.org/mklabs/node-fileset)
+
+Adds multiples patterns matching and exlude ability. This is
+basically just a sugar API syntax where you can specify a list of includes
+and optional exclude patterns. It works by setting up the necessary
+miniglob "fileset" and filtering out the results using minimatch.
+
+## Install
+
+ npm install fileset
+
+## Usage
+
+Can be used with callback or emitter style.
+
+* **include**: list of glob patterns `foo/**/*.js *.md src/lib/**/*`
+* **exclude**: *optional* list of glob patterns to filter include
+ results `foo/**/*.js *.md`
+* **callback**: *optional* function that gets called with an error if
+ something wrong happend, otherwise null with an array of results
+
+The callback is optional since the fileset method return an instance of
+EventEmitter which emit different events you might use:
+
+* *match*: Every time a match is found, miniglob emits this event with
+ the pattern.
+* *include*: Emitted each time an include match is found.
+* *exclude*: Emitted each time an exclude match is found and filtered
+ out from the fileset.
+* *end*: Emitted when the matching is finished with all the matches
+ found, optionally filtered by the exclude patterns.
+
+#### Callback
+
+```js
+var fileset = require('fileset');
+
+fileset('**/*.js', '**.min.js', function(err, files) {
+ if (err) return console.error(err);
+
+ console.log('Files: ', files.length);
+ console.log(files);
+});
+```
+
+#### Event emitter
+
+```js
+var fileset = require('fileset');
+
+fileset('**.coffee README.md *.json Cakefile **.js', 'node_modules/**')
+ .on('match', console.log.bind(console, 'error'))
+ .on('include', console.log.bind(console, 'includes'))
+ .on('exclude', console.log.bind(console, 'excludes'))
+ .on('end', console.log.bind(console, 'end'));
+```
+
+`fileset` returns an instance of EventEmitter, with an `includes` property
+which is the array of Fileset objects (inheriting from
+`miniglob.Miniglob`) that were used during the mathing process, should
+you want to use them individually.
+
+Check out the
+[tests](https://github.com/mklabs/node-fileset/tree/master/tests) for
+more examples.
+
+## Tests
+
+Run `npm test`
+
+## Why
+
+Mainly for a build tool with cake files, to provide me an easy way to get
+a list of files by either using glob or path patterns, optionally
+allowing exclude patterns to filter out the results.
+
+All the magic is happening in
+[Glob](https://github.com/isaacs/node-glob) and
+[minimatch](https://github.com/isaacs/minimatch). Check them out!
diff --git a/lib/fileset.js b/lib/fileset.js
new file mode 100644
index 0000000..a74077c
--- /dev/null
+++ b/lib/fileset.js
@@ -0,0 +1,64 @@
+var util = require('util'),
+ minimatch = require('minimatch'),
+ Glob = require('glob').Glob,
+ EventEmitter = require('events').EventEmitter;
+
+module.exports = fileset;
+
+function fileset(include, exclude, options, cb) {
+ if (typeof exclude === 'function') cb = exclude, exclude = '';
+ else if (typeof options === 'function') cb = options, options = {};
+
+ var includes = (typeof include === 'string') ? include.split(' ') : include;
+ var excludes = (typeof exclude === 'string') ? exclude.split(' ') : exclude;
+
+ var em = new EventEmitter,
+ remaining = includes.length,
+ results = [];
+
+ if(!includes.length) return cb(new Error('Must provide an include pattern'));
+
+ em.includes = includes.map(function(pattern) {
+ return new fileset.Fileset(pattern, options)
+ .on('error', cb ? cb : em.emit.bind(em, 'error'))
+ .on('match', em.emit.bind(em, 'match'))
+ .on('match', em.emit.bind(em, 'include'))
+ .on('end', next.bind({}, pattern))
+ });
+
+ function next(pattern, matches) {
+ results = results.concat(matches);
+
+ if(!(--remaining)) {
+ results = results.filter(function(file) {
+ return !excludes.filter(function(glob) {
+ var match = minimatch(file, glob, { matchBase: true });
+ if(match) em.emit('exclude', file);
+ return match;
+ }).length;
+ });
+
+ if(cb) cb(null, results);
+ em.emit('end', results);
+ }
+ }
+
+ return em;
+}
+
+fileset.Fileset = function Fileset(pattern, options, cb) {
+
+ if (typeof options === 'function') cb = options, options = {};
+ if (!options) options = {};
+
+ Glob.call(this, pattern, options);
+
+ if(typeof cb === 'function') {
+ this.on('error', cb);
+ this.on('end', function(matches) { cb(null, matches); });
+ }
+};
+
+util.inherits(fileset.Fileset, Glob);
+
+
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..8c69ed3
--- /dev/null
+++ b/package.json
@@ -0,0 +1,23 @@
+{
+ "author": "mklabs",
+ "name": "fileset",
+ "description": "Wrapper around miniglob / minimatch combo to allow multiple patterns matching and include-exclude ability",
+ "version": "0.1.5",
+ "homepage": "https://github.com/mklabs/node-fileset",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mklabs/node-fileset.git"
+ },
+ "main": "./lib/fileset",
+ "scripts": {
+ "test": "node tests/test.js"
+ },
+ "dependencies": {
+ "minimatch": "0.x",
+ "glob": "3.x"
+ },
+ "licenses": [{
+ "type": "MIT",
+ "url": "https://github.com/mklabs/node-fileset/blob/master/LICENSE-MIT"
+ }]
+}
diff --git a/tests/fixtures/an (odd) filename.js b/tests/fixtures/an (odd) filename.js
new file mode 100644
index 0000000..fbf9f2b
--- /dev/null
+++ b/tests/fixtures/an (odd) filename.js
@@ -0,0 +1 @@
+var odd = true;
\ No newline at end of file
diff --git a/tests/helper.js b/tests/helper.js
new file mode 100644
index 0000000..d76735e
--- /dev/null
+++ b/tests/helper.js
@@ -0,0 +1,61 @@
+
+var EventEmitter = require('events').EventEmitter,
+ assert = require('assert'),
+ tests = {};
+
+module.exports = test;
+test.run = run;
+
+// ## Test helpers
+
+function test(msg, handler) {
+ tests[msg] = handler;
+}
+
+function run() {
+ var specs = Object.keys(tests),
+ specsRemaining = specs.length;
+
+ specs.forEach(function(spec) {
+ var handler = tests[spec];
+
+ // grab the set of asserts for this spec
+ var shoulds = handler(),
+ keys = Object.keys(shoulds),
+ remaining = keys.length;
+
+ keys.forEach(function(should) {
+ var em = new EventEmitter(),
+ to = setTimeout(function() {
+ assert.fail('never ended');
+ }, 5000);
+
+ em
+ .on('error', function assertFail(err) { assert.fail(err) })
+ .on('end', function assertOk() {
+ clearTimeout(to);
+ shoulds[should].status = true;
+
+ // till we get to 0
+ if(!(--remaining)) {
+ console.log([
+ '',
+ '» ' + spec,
+ keys.map(function(k) { return ' » ' + k; }).join('\n'),
+ '',
+ ' Total: ' + keys.length,
+ ' Failed: ' + keys.map(function(item) { return shoulds[should].status; }).filter(function(status) { return !status; }).length,
+ ''
+ ].join('\n'));
+
+ if(!(--specsRemaining)) {
+ console.log('All done');
+ }
+
+ }
+ });
+
+ shoulds[should](em);
+ });
+ });
+}
diff --git a/tests/test.js b/tests/test.js
new file mode 100644
index 0000000..cb0ceb1
--- /dev/null
+++ b/tests/test.js
@@ -0,0 +1,133 @@
+
+var EventEmitter = require('events').EventEmitter,
+ fileset = require('../'),
+ assert = require('assert'),
+ test = require('./helper');
+
+// Given a **.coffee pattern
+test('Given a **.md pattern', function() {
+
+ return {
+ 'should return the list of matching file in this repo': function(em) {
+ fileset('*.md', function(err, results) {
+ if(err) return em.emit('error', err);
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.ok(results.length, 'should return at least one element');
+ assert.equal(results.length, 1, 'actually, should return only one');
+ em.emit('end');
+ });
+ }
+ }
+});
+
+test('Say we want the **.js files, but not those in node_modules', function() {
+
+ return {
+ 'Should recursively walk the dir and return the matching list': function(em) {
+ fileset('**/*.js', 'node_modules/**', function(err, results) {
+ if(err) return em.emit('error', err);
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.equal(results.length, 4);
+ em.emit('end');
+ });
+ },
+
+ 'Should support multiple paths at once': function(em) {
+ fileset('**/*.js *.md', 'node_modules/**', function(err, results) {
+ if(err) return em.emit('error', err);
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.equal(results.length, 5);
+
+ assert.deepEqual(results, [
+ 'README.md',
+ 'lib/fileset.js',
+ 'tests/fixtures/an (odd) filename.js',
+ 'tests/helper.js',
+ 'tests/test.js'
+ ]);
+
+ em.emit('end');
+ });
+ },
+
+ 'Should support multiple paths for excludes as well': function(em) {
+ fileset('**/*.js *.md', 'node_modules/** **.md tests/*.js', function(err, results) {
+ if(err) return em.emit('error', err);
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.equal(results.length, 2);
+
+ assert.deepEqual(results, [
+ 'lib/fileset.js',
+ 'tests/fixtures/an (odd) filename.js',
+ ]);
+
+ em.emit('end');
+ });
+ }
+ }
+});
+
+
+test('Testing out emmited events', function() {
+
+ // todos: the tests for match, include, exclude events, but seems like it's ok
+ return {
+ 'Should recursively walk the dir and return the matching list': function(em) {
+ fileset('**/*.js', 'node_modules/**')
+ .on('error', em.emit.bind(em, 'error'))
+ .on('end', function(results) {
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.equal(results.length, 4);
+ em.emit('end');
+ });
+ },
+
+ 'Should support multiple paths at once': function(em) {
+ fileset('**/*.js *.md', 'node_modules/**')
+ .on('error', em.emit.bind(em, 'error'))
+ .on('end', function(results) {
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.equal(results.length, 5);
+
+ assert.deepEqual(results, [
+ 'README.md',
+ 'lib/fileset.js',
+ 'tests/fixtures/an (odd) filename.js',
+ 'tests/helper.js',
+ 'tests/test.js'
+ ]);
+
+ em.emit('end');
+ });
+ }
+ }
+});
+
+
+test('Testing patterns passed as arrays', function() {
+
+ return {
+ 'Should match files passed as an array with odd filenames': function(em) {
+ fileset(['lib/*.js', 'tests/fixtures/an (odd) filename.js'], ['node_modules/**'])
+ .on('error', em.emit.bind(em, 'error'))
+ .on('end', function(results) {
+ assert.ok(Array.isArray(results), 'should be an array');
+ assert.equal(results.length, 2);
+
+ assert.deepEqual(results, [
+ 'lib/fileset.js',
+ 'tests/fixtures/an (odd) filename.js',
+ ]);
+
+ em.emit('end');
+ });
+ }
+ }
+
+});
+
+
+
+test.run();
+
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-fileset.git
More information about the Pkg-javascript-commits
mailing list