forked from johnagan/clean-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
184 lines (155 loc) · 5.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
var fs = require('fs');
var os = require('os');
var path = require('path');
var rimraf = require('rimraf');
// added node .10
// http://stackoverflow.com/questions/21698906/how-to-check-if-a-path-is-absolute-or-relative/30714706#30714706
function isAbsolute(dir) {
return path.normalize(dir + path.sep) === path.normalize(path.resolve(dir) + path.sep);
}
function upperCaseWindowsRoot(dir) {
var splitPath = dir.split(path.sep);
splitPath[0] = splitPath[0].toUpperCase();
return splitPath.join(path.sep);
}
function Plugin(paths, options) {
//backwards compatibility
if (typeof options === 'string') {
options = {
root: options
}
}
options = options || {};
if (options.verbose === undefined) {
if (process.env.NODE_ENV === 'test') {
options.verbose = false;
} else {
options.verbose = true;
}
}
if (options.dry === undefined) {
options.dry = false;
}
// 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) {
paths = [paths];
}
// store paths and options
this.paths = paths;
this.options = options;
}
Plugin.prototype.apply = function () {
var _this = this;
var results = [];
var workingDir;
var dirName;
var projectRootDir;
var webpackDir;
// exit if no paths passed in
if (_this.paths === void 0) {
results.push({ path: _this.paths, output: 'nothing to clean' });
return results;
}
if (!isAbsolute(_this.options.root)) {
_this.options.verbose && console.warn(
'clean-webpack-plugin: ' + _this.options.root +
' project root must be an absolute path. Skipping all...');
results.push({ path: _this.options.root, output: 'project root must be an absolute path' });
return results;
}
workingDir = process.cwd();
dirName = __dirname;
projectRootDir = path.resolve(_this.options.root);
webpackDir = path.dirname(module.parent.filename);
if (os.platform() === 'win32') {
workingDir = upperCaseWindowsRoot(workingDir);
dirName = upperCaseWindowsRoot(dirName);
projectRootDir = upperCaseWindowsRoot(projectRootDir);
webpackDir = upperCaseWindowsRoot(webpackDir);
}
// preform an rm -rf on each path
_this.paths.forEach(function (rimrafPath) {
rimrafPath = path.resolve(_this.options.root, rimrafPath);
if (os.platform() === 'win32') {
rimrafPath = upperCaseWindowsRoot(rimrafPath);
}
// disallow deletion any directories outside of root path.
if (rimrafPath.indexOf(projectRootDir) < 0) {
_this.options.verbose && console.warn(
'clean-webpack-plugin: ' + rimrafPath + ' is outside of the project root. Skipping...');
results.push({ path: rimrafPath, output: 'must be inside the project root' });
return;
}
if (rimrafPath === projectRootDir) {
_this.options.verbose &&
console.warn(
'clean-webpack-plugin: ' + rimrafPath + ' is equal to project root. Skipping...');
results.push({ path: rimrafPath, output: 'is equal to project root' });
return;
}
if (rimrafPath === webpackDir) {
_this.options.verbose &&
console.warn('clean-webpack-plugin: ' + rimrafPath + ' would delete webpack. Skipping...');
results.push({ path: rimrafPath, output: 'would delete webpack' });
return;
}
if (rimrafPath === dirName || rimrafPath === workingDir) {
_this.options.verbose &&
console.log('clean-webpack-plugin: ' + rimrafPath + ' is working directory. Skipping...');
results.push({ path: rimrafPath, output: 'is working directory' });
return;
}
var childrenAfterExcluding = [];
var excludedChildren = [];
if(_this.options.exclude && _this.options.exclude.length) {
try {
var pathStat = fs.statSync(rimrafPath);
if (pathStat.isDirectory()) {
childrenAfterExcluding = fs.readdirSync(rimrafPath)
.filter(function (childFile) {
var include = _this.options.exclude.indexOf(childFile) < 0;
if (!include) {
excludedChildren.push(childFile);
}
return include;
})
.map(function (file) {
var fullPath = path.join(rimrafPath, file);
if (os.platform() === 'win32') {
fullPath = upperCaseWindowsRoot(fullPath);
}
return fullPath;
});
}
if (_this.options.exclude.indexOf('.') >= 0) {
excludedChildren.push('.');
}
} catch (e) {
childrenAfterExcluding = [];
}
}
if (_this.options.dry !== true) {
if (_this.options.exclude && excludedChildren.length) {
childrenAfterExcluding.forEach(function (child) {
rimraf.sync(child);
});
} else {
rimraf.sync(rimrafPath);
}
}
this.applyPlugin('before-clean', rimrafPath);
_this.options.verbose &&
console.warn('clean-webpack-plugin: ' + rimrafPath + ' has been removed.');
_this.options.verbose && excludedChildren.length &&
console.warn('clean-webpack-plugin: ' + excludedChildren.length + ' file(s) excluded - ' + excludedChildren.join(', '));
this.applyPlugin('after-clean', rimrafPath);
excludedChildren.length ?
results.push({ path: rimrafPath, output: 'removed with exclusions (' + excludedChildren.length + ')'}) :
results.push({ path: rimrafPath, output: 'removed' });
});
return results;
};
module.exports = Plugin;