forked from fluffynuts/gulp-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge.js
68 lines (62 loc) · 1.73 KB
/
purge.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
'use strict'
const gulp = requireModule('gulp-with-help'),
path = require('path'),
del = require('del'),
rimraf = require('rimraf'),
findDirs = requireModule('find-dirs'),
debug = require('debug')('purge');
function isNotInRootDir(dir) {
// where 'root dir' refers to the gulp context current dir
var inRootDir = path.resolve(path.basename(dir));
return inRootDir !== dir;
}
function doRegularRm(dir, inRootToo) {
return new Promise((resolve, reject) => {
try {
debug(`searching for folders matching: ${dir}`);
var matches = findDirs('.', dir);
debug(`got: ${matches}`);
if (!inRootToo) {
matches = matches.filter(isNotInRootDir);
}
if (matches.length === 0) {
debug(`-> nothing to do for ${dir}`);
return resolve();
}
matches.forEach(f => {
debug(`should purge: ${f}`);
rimraf.sync(f);
debug(`purge complete: ${f}`);
});
resolve();
} catch (e) {
debug(`whoops! ${e}`);
reject(e);
}
});
}
function doPurge(includeRootFolders) {
return Promise.all([
del([
'./source/**/bin/**',
'./src/**/bin/**',
'./source/**/obj/**',
'./src/**/obj/**'
]),
doRegularRm('node_modules', includeRootFolders),
doRegularRm('bower_components', includeRootFolders),
doRegularRm('packages', includeRootFolders)
]).then(() => {
debug('-- PURGE COMPLETE! ---');
});
}
gulp.task('purge',
'Purges all bins, objs, node_modules, bower_components and packages not in the root',
function () {
return doPurge(false);
});
gulp.task('mega-purge',
'Performs regular purge and in the root (you\'ll have to `npm install` afterwards!',
function() {
return doPurge(true);
});