-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
144 lines (120 loc) · 3.11 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
/*!
* delete <https://github.com/jonschlinkert/delete>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var each = require('async-each');
var extend = require('extend-shallow');
var rimraf = require('rimraf');
var glob = require('matched');
function del(patterns, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
if (typeof cb !== 'function') {
return del.promise.apply(del, arguments);
}
var opts = extend({cwd: process.cwd()}, options);
var deleted = [];
if (patterns === '' && isCurrentDir(opts.cwd)) {
cb(null, deleted);
return;
}
glob(patterns, opts, function(err, files) {
if (err) {
cb(err);
return;
}
each(files, function(file, next) {
var fp = path.resolve(opts.cwd, file);
fs.stat(fp, function(err, stat) {
if (err) {
next();
return;
}
try {
assertDirectory(fp, opts);
} catch (err) {
next(err);
return;
}
rimraf(fp, {disableGlob: true}, function(err) {
if (err) return next(err);
deleted.push(fp);
next();
});
});
}, function(err) {
cb(err, deleted.sort());
});
});
}
del.sync = function delSync(patterns, options) {
var opts = extend({cwd: process.cwd()}, options);
var deleted = [];
if (patterns === '' && isCurrentDir(opts.cwd)) {
return [];
}
glob.sync(patterns, opts).forEach(function(file) {
var fp = path.resolve(opts.cwd, file);
assertDirectory(fp, opts);
rimraf.sync(fp, {disableGlob: true});
deleted.push(fp);
});
deleted.sort();
return deleted;
};
del.promise = function delPromise(patterns, options) {
var opts = extend({cwd: process.cwd()}, options);
var deleted = [];
if (patterns === '' && isCurrentDir(opts.cwd)) {
return Promise.resolve(deleted);
}
return glob.promise(patterns, opts)
.then(function(files) {
// sync is actually faster than async most of the time
files.forEach(function(fp) {
del.sync(fp, opts);
deleted.push(fp);
});
return deleted;
});
};
function assertDirectory(fp, options) {
if (options && options.force === true) {
return;
}
if (isCurrentDir(fp) === true) {
throw new Error('CAUTION! to delete the current working directory "options.force" must be set to true');
}
if (insideCurrentDir(fp) === false) {
throw new Error('CAUTION! to delete files or folders outside the current working directory "options.force" must be set to true');
}
}
function insideCurrentDir(fp) {
var cwd = stripSlash(process.cwd());
fp = stripSlash(path.normalize(path.resolve(fp)));
if (path.sep === '\\') {
cwd = cwd.toLowerCase();
fp = fp.toLowerCase();
}
return fp.slice(0, cwd.length) === cwd;
}
function stripSlash(fp) {
if (fp.slice(-1) === '/') {
return fp.slice(0, -1);
}
return fp;
}
function isCurrentDir(fp) {
return process.cwd() === path.resolve(fp);
}
/**
* Expose `del`
*/
module.exports = del;