Skip to content

Commit

Permalink
Do not descend ignored directories
Browse files Browse the repository at this point in the history
Paths may be ignored because they contain a large number of files. The
getDirs method should not actually descend into those ignored
directories.

Additionally this commit flattens the recursion in getDirs.
  • Loading branch information
mixonic committed Aug 8, 2015
1 parent 8156baa commit 7797d0a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
68 changes: 35 additions & 33 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,27 +336,44 @@ YUI.add('utils', function (Y) {

Y.getProjectData = getProjectData;


/**
* Walks the tree from this dir and returns all the subdirs
* @method getDirs
* @param {String} dir The dir to begin at
* @return {Array} The array of directories..
*/
var getDirs = function (dir) {
var dirs = fs.readdirSync(dir),
paths = [];

dirs.forEach(function (d) {
var _dir = path.join(dir, d),
stat = fs.lstatSync(_dir);
var getDirs = function (baseDir, ignore) {
var inputPaths = [baseDir],
paths = [],
i, d, isIgnored, fullPath, subpath,
stat, possibleDirs;

var inputPath = inputPaths.pop();
while (inputPath) {
fullPath = path.join(baseDir, inputPath);
if (inputPath !== baseDir) {
paths.push(fullPath);
}

stat = fs.lstatSync(fullPath);
if (stat.isDirectory()) {
if (_dir.indexOf('.') !== 0) {
paths = [].concat(paths, _dir, getDirs(_dir));
possibleDirs = fs.readdirSync(fullPath);
for (d=0;d<possibleDirs.length;d++) {
subpath = path.join(inputPath, possibleDirs[d]);
isIgnored = false;
for (i=0;i<ignore.length;i++) {
if (subpath.indexOf(ignore[i]) === 0) {
isIgnored = true;
break;
}
}
if (!isIgnored) {
inputPaths.push(subpath);
}
}
}
});
inputPath = inputPaths.pop();
}

return paths;
};
Expand All @@ -370,6 +387,12 @@ YUI.add('utils', function (Y) {
* @param {String} [ignore=false] A string to call `.indexOf` on a path to determine if it should be ignored
*/
var validatePaths = function (paths, ignore) {
if (!ignore) {
ignore = [];
} else if (!(ignore instanceof Array)) {
ignore = [ignore];
}

var newpaths = [];
//Shortcut the *, . & ./ shortcuts that shall globbing fixes for us
if (paths === '*' || paths === '.' || paths === './') {
Expand All @@ -388,7 +411,7 @@ YUI.add('utils', function (Y) {
glob = validatePath.replace(/\//g, '\\\\');
}

var glob_paths = getDirs('.'),
var glob_paths = getDirs('.', ignore),
is_globbed = false;

glob_paths.forEach(function (dir) {
Expand Down Expand Up @@ -426,27 +449,6 @@ YUI.add('utils', function (Y) {
throw ('Paths should be an array of paths');
}

if (ignore) {
if (!(ignore instanceof Array)) {
ignore = [ignore];
}
var p = [],
shouldIgnore = false;

paths.forEach(function (v) {
shouldIgnore = false;
ignore.forEach(function (i) {
if (!shouldIgnore && v.indexOf(i) !== -1) {
shouldIgnore = true;
}
});
if (!shouldIgnore) {
p.push(v);
}
});
paths = p;
}

paths = paths.sort();
return paths;
};
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/input/with-symlink/a/b
Empty file.
Empty file.
Empty file.
Empty file.
20 changes: 20 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ suite.add(new YUITest.TestCase({

Assert.isArray(options.paths, 'Failed to set path');
Assert.areSame(3, options.paths.length, 'Failed to retrieve all path options');
},
'test: ignore paths': function () {
var options;

process.chdir(path.join(__dirname, 'input/with-symlink'));

// Simulate a path provided by a configuration...
// first with a String path
options = {
paths: 'a',
ignorePaths: [
'a/b',
'a/d',
'c'
]
};
options = Y.Project.init(options);

Assert.isArray(options.paths, 'Failed to set path');
Assert.areSame(1, options.paths.length, 'Failed to retrieve all path options');
}
}));

Expand Down

0 comments on commit 7797d0a

Please sign in to comment.