Skip to content

Commit

Permalink
Close #26 PR: Expose generateGlobTasks
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil authored and UltCombo committed Apr 3, 2016
1 parent 01e99e7 commit 88bcb6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
71 changes: 31 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,54 @@ var glob = require('glob');
var arrify = require('arrify');
var pify = require('pify');

function sortPatterns(patterns) {
patterns = arrify(patterns);
var globP = pify(glob, Promise).bind(glob);

var positives = [];
var negatives = [];
function isNegative(pattern) {
return pattern[0] === '!';
}

patterns.forEach(function (pattern, index) {
var isNegative = pattern[0] === '!';
(isNegative ? negatives : positives).push({
index: index,
pattern: isNegative ? pattern.slice(1) : pattern
});
});
function generateGlobTasks(patterns, opts) {
var globTasks = [];

return {
positives: positives,
negatives: negatives
};
}
patterns = arrify(patterns);
opts = objectAssign({ignore: []}, opts);

function setIgnore(opts, negatives, positiveIndex) {
opts = objectAssign({}, opts);
patterns.forEach(function (pattern, i) {
if (isNegative(pattern)) {
return;
}

var negativePatterns = negatives.filter(function (negative) {
return negative.index > positiveIndex;
}).map(function (negative) {
return negative.pattern;
var ignore = patterns.slice(i).filter(isNegative).map(function (pattern) {
return pattern.slice(1);
});

globTasks.push({
pattern: pattern,
opts: objectAssign({}, opts, {
ignore: opts.ignore.concat(ignore)
})
});
});

opts.ignore = (opts.ignore || []).concat(negativePatterns);
return opts;
return globTasks;
}

module.exports = function (patterns, opts) {
var sortedPatterns = sortPatterns(patterns);
opts = opts || {};
var globTasks = generateGlobTasks(patterns, opts);

if (sortedPatterns.positives.length === 0) {
return Promise.resolve([]);
}

return Promise.all(sortedPatterns.positives.map(function (positive) {
var globOpts = setIgnore(opts, sortedPatterns.negatives, positive.index);
return pify(glob, Promise)(positive.pattern, globOpts);
return Promise.all(globTasks.map(function (task) {
return globP(task.pattern, task.opts);
})).then(function (paths) {
return arrayUnion.apply(null, paths);
});
};

module.exports.sync = function (patterns, opts) {
var sortedPatterns = sortPatterns(patterns);

if (sortedPatterns.positives.length === 0) {
return [];
}
var globTasks = generateGlobTasks(patterns, opts);

return sortedPatterns.positives.reduce(function (ret, positive) {
return arrayUnion(ret, glob.sync(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index)));
return globTasks.reduce(function (matches, task) {
return arrayUnion(matches, glob.sync(task.pattern, task.opts));
}, []);
};

module.exports.generateGlobTasks = generateGlobTasks;
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ it('should not mutate the options object - async', function () {
it('should not mutate the options object - sync', function () {
globby.sync(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
});

it('should expose generateGlobTasks', function () {
var tasks = globby.generateGlobTasks(['*.tmp', '!b.tmp'], {ignore: ['c.tmp']});

assert.strictEqual(tasks.length, 1);
assert.strictEqual(tasks[0].pattern, '*.tmp');
assert.deepEqual(tasks[0].opts.ignore, ['c.tmp', 'b.tmp']);
});

0 comments on commit 88bcb6b

Please sign in to comment.