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

Feature/ Support regex path names #74

Closed
31 changes: 14 additions & 17 deletions index.js
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written with .filter as

return rootChildren.filter(pattern.test)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aulisius Enhanced! :D

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
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