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
Closed
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -27,3 +27,6 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# editor configuration
.vscode
33 changes: 31 additions & 2 deletions index.js
Expand Up @@ -42,8 +42,8 @@ function CleanWebpackPlugin(paths, options) {
// determine webpack root
options.root = options.root || path.dirname(module.parent.filename);

// allows for a single string entry
if (typeof paths == 'string' || paths instanceof String) {
// allows for a single string entry or a regular expression
if (typeof paths == 'string' || paths instanceof String || paths instanceof RegExp) {
paths = [paths];
}

Expand All @@ -52,6 +52,24 @@ function CleanWebpackPlugin(paths, options) {
this.options = options;
}

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

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 paths;
}, []);
}

var clean = function() {
var _this = this;
var results = [];
Expand Down Expand Up @@ -86,6 +104,17 @@ var clean = function() {
webpackDir = upperCaseWindowsRoot(webpackDir);
}

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



// preform an rm -rf on each path
_this.paths.forEach(function(rimrafPath) {
rimrafPath = path.resolve(_this.options.root, rimrafPath);
Expand Down