Permalink
Browse files

Filelist exclusions test now passing.

  • Loading branch information...
1 parent 5393d2e commit f452a1db1877d44780530e1284a272403ab9cd15 mosen committed May 19, 2012
Showing with 92 additions and 47 deletions.
  1. +84 −47 lib/buildy/filelist.js
  2. +8 −0 test/filelist-test.js
View
@@ -8,6 +8,9 @@ var path = require('path');
*
* @class Filelist
* @constructor
+ * @param items {Array} Items to process (globs, filenames, directories)
+ * @param callback {Function} Callback function
+ * @param options {Object} Filelist options
*/
function Filelist(items, callback, options) {
@@ -30,7 +33,7 @@ function Filelist(items, callback, options) {
* @type {Object}
* @private
*/
- this._options = options || {};
+ this._options = options || { "exclude" : [] };
/**
* File listing results
@@ -77,54 +80,59 @@ Filelist.prototype.add = function(item, callback) {
console.log('Not implemented: globbing');
//self._addGlob(item, self.handleGlobDone);
} else {
- fs.stat(item, function(err, stats) {
- if (err) {
- callback('Failed to stat: ' + err);
- return;
- }
-
- if (stats.isDirectory()) {
- fs.readdir(item, function(err, files) {
- if (err) {
- callback('Failed to read directory: ' + item + ', reason: ' + err);
- return;
- } else {
- var _mapParentDirectory = function _mapParentDirectory(filename) {
- return path.join(item, filename);
- };
-
- if (files.length > 0) {
- files.map(_mapParentDirectory).forEach(function(subItem) {
- // Each subdirectory item starts a new item for processing
- this._items.push(subItem);
- this.add(subItem, function _itemDoneCallback(err, results) {
- if (err) {
- self._callback(err);
- } else {
- self._addResults(results);
- self.handleItemFinished(item);
- }
- });
- }, self);
- // TODO: Note that callback gets called before the entire directory is processed
- // Consider a different design where the callback is only called upon completion of all
- // subdirectory items.
- callback(null, []);
+ if (this.isExcluded(item)) {
+ // TODO: add to excluded list, for debugging purposes
+ callback(null, []);
+ } else {
+ fs.stat(item, function(err, stats) {
+ if (err) {
+ callback('Failed to stat: ' + err);
+ return;
+ }
+
+ if (stats.isDirectory()) {
+ fs.readdir(item, function(err, files) {
+ if (err) {
+ callback('Failed to read directory: ' + item + ', reason: ' + err);
+ return;
} else {
- callback(null, []);
+ var _mapParentDirectory = function _mapParentDirectory(filename) {
+ return path.join(item, filename);
+ };
+
+ if (files.length > 0) {
+ files.map(_mapParentDirectory).forEach(function(subItem) {
+ // Each subdirectory item starts a new item for processing
+ this._items.push(subItem);
+ this.add(subItem, function _itemDoneCallback(err, results) {
+ if (err) {
+ self._callback(err);
+ } else {
+ self._addResults(results);
+ self.handleItemFinished(item);
+ }
+ });
+ }, self);
+ // TODO: Note that callback gets called before the entire directory is processed
+ // Consider a different design where the callback is only called upon completion of all
+ // subdirectory items.
+ callback(null, []);
+ } else {
+ callback(null, []);
+ }
}
- }
- });
- return;
- }
-
- if (stats.isFile()) {
- callback(null, [item]);
- return;
- }
-
- // TODO: warning, specified item does not exist
- });
+ });
+ return;
+ }
+
+ if (stats.isFile()) {
+ callback(null, [item]);
+ return;
+ }
+
+ // TODO: warning, specified item does not exist
+ });
+ }
}
};
@@ -155,8 +163,37 @@ Filelist.prototype.handleItemFinished = function(item) {
};
/**
+ * Determine whether a given path has been specified as an exclusion.
+ *
+ * @param item {String} Path
+ * @public
+ */
+Filelist.prototype.isExcluded = function(item) {
+
+ var isExcluded = false;
+
+ this._options.exclude.forEach(function _itemMatchesExclusion(excl) {
+ if (excl instanceof RegExp) {
+ if (excl.test(item)) {
+ isExcluded = true;
+ }
+ } else {
+ if (excl === item) {
+ isExcluded = true;
+ }
+ }
+ }, this);
+
+ return isExcluded;
+};
+
+/**
* Generate an expanded file listing from a list of files, directories and glob patterns.
*
+ * Options is an object with the following (optional) properties:
+ *
+ * "exclude" : ["exact filename or directory name", /regexp matched against each item/]
+ *
* @type {Function}
* @param {Array} items Items that will be expanded to a complete file listing including filenames, directories and globs
* @param {Function} callback Callback function
@@ -46,4 +46,12 @@ vows.describe('Generating file lists').addBatch({
assert.ok(err);
}
}
+ , 'when called with one file that is also excluded': {
+ topic: function() {
+ filelist([fixtures.file], this.callback, { exclude: [fixtures.file] });
+ },
+ 'the file is excluded from the results': function(err, data) {
+ assert.equal(-1, data.indexOf(fixtures.file));
+ }
+ }
}).export(module);

0 comments on commit f452a1d

Please sign in to comment.