Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex Support Fixes #1

Merged
merged 6 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 14 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,20 @@ function CleanWebpackPlugin(paths, options) {

/**
* Gives back file paths that match the given pattern
*
* @param {string} root
* @param {RegExp} pattern
*
* @param {string} root
* @param {RegExp} pattern
* @returns [string]
*/
function resolveRegexPaths(root, pattern) {
const rootChildren = fs.readdir(root);

const resolvedPaths = [];
var rootChildren = fs.readdirSync(root);

rootChildren.forEach(function (child) {
if (pattern.test(child) === true) {
resolvedPaths.push(child);
return rootChildren.reduce(function(paths, child) {
if(pattern.test(child) === true) {
paths.push(child);
}
});

return resolvedPaths;
return paths;
}, []);
}

var clean = function() {
Expand Down Expand Up @@ -108,15 +105,15 @@ var clean = function() {
}

// Resolve RegExp paths.
_this.paths = _this.paths.reduce(function (acc, currentPath) {
_this.paths = _this.paths.reduce(function (paths, currentPath) {
if (currentPath instanceof RegExp) {
const resolvedPaths = resolveRegexPaths(_this.options.root, currentPath);
return acc.concat(resolvedPaths);
var resolvedPaths = resolveRegexPaths(_this.options.root, currentPath);
return paths.concat(resolvedPaths);
}
return acc.concat(currentPath);
return paths.concat(currentPath);
}, []);



// preform an rm -rf on each path
_this.paths.forEach(function(rimrafPath) {
Expand Down
24 changes: 17 additions & 7 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ var run = function (setup) {
expect(result[0].output).to.equal('removed');
});

// it('remove one regex match', function () {
// createDir(dirOne);
// cleanWebpackPlugin = new CleanWebpackPlugin(/_.+/i, { root: projectRoot });
// result = cleanWebpackPlugin.apply();
it('remove one regex match', function () {
createDir(dirOne);
cleanWebpackPlugin = new CleanWebpackPlugin(/_.+/i, { root: projectRoot });
result = cleanWebpackPlugin.apply();

// expect(result[0].output).to.equal('removed');
// });
expect(result[0].output).to.equal('removed');
});

it('remove multiple regex matches', function () {
createDir(dirOne);
Expand All @@ -154,7 +154,17 @@ var run = function (setup) {
expect(result[0].output).to.equal('removed');
expect(result[1].output).to.equal('removed');
});


it('removes mix of regex and string matches', function () {
createDir(dirOne);
createDir(dirTwo);
cleanWebpackPlugin = new CleanWebpackPlugin([/_one/i, dirTwo], { root: projectRoot });
result = cleanWebpackPlugin.apply();

expect(result[0].output).to.equal('removed');
expect(result[1].output).to.equal('removed');
});

it('context backwards compatibility ', function () {
createDir(dirOne);
cleanWebpackPlugin = new CleanWebpackPlugin(dirOne, projectRoot);
Expand Down