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
35 changes: 33 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,27 @@ 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) {
const rootChildren = fs.readdir(root);

const resolvedPaths = [];

rootChildren.forEach(child => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The codebase is very ES5 right now. I believe we can maintain one style for now. We can later on rewrite the entire plugin in one go with ES6.

Copy link
Author

Choose a reason for hiding this comment

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

Okay :D

if (pattern.test(child) === true) {
resolvedPaths.push(child);
}
});

return resolvedPaths;
}

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

// Resolve RegExp paths.
_this.paths.reduce((acc, currentPath) => {
Copy link
Contributor

@aulisius aulisius Mar 5, 2018

Choose a reason for hiding this comment

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

You should be assigning the result of this .reduce back again to _this.paths. Better else, You can instead map the regex to resolved paths and assign it to _this.paths

Copy link
Author

Choose a reason for hiding this comment

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

Hey, okay I'll look into it!

if (path instanceof RegExp) {
const resolvedPaths = resolveRegexPaths(_this.options.root, currentPath);
return [...acc, ...resolvedPaths];
}
return [...acc, currentPath]
}, []);


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