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
10 changes: 4 additions & 6 deletions README.md
Expand Up @@ -32,10 +32,7 @@ const webpack = require('webpack'); //to access built-in plugins
const path = require('path');

// the path(s) that should be cleaned
let pathsToClean = [
'dist',
'build'
]
let pathsToClean = /dist|build/;

// the clean options to use
let cleanOptions = {
Expand Down Expand Up @@ -71,12 +68,13 @@ const webpackConfig = {


### Paths (Required)
An [array] of string paths to clean
An [array] of string or regex paths to clean
```js
[
'dist', // removes 'dist' folder
'build/*.*', // removes all files in 'build' folder
'web/*.js' // removes all JavaScript files in 'web' folder
'web/*.js', // removes all JavaScript files in 'web' folder
/_.+/i, // removes all the files / folders matching the regex
]
```

Expand Down
28 changes: 26 additions & 2 deletions index.js
Expand Up @@ -44,8 +44,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 @@ -54,6 +54,19 @@ 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.filter(pattern.test.bind(pattern));
}

var clean = function() {
var _this = this;
var results = [];
Expand Down Expand Up @@ -88,6 +101,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