-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
50 lines (40 loc) · 1.12 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
'use strict';
var through = require('through2');
var rimraf = require('rimraf');
var path = require('path');
module.exports = function(options){
if (!options) {
options = {};
}
if (options.force && typeof options.force !== 'boolean') {
options.force = false;
}
function del(file, encoding, cb){
//jshint validthis:true
var cwd = file.cwd || process.cwd();
// For safety always resolve paths
var filepath = path.resolve(cwd, file.path);
var relativeFromCwd = path.relative(cwd, filepath);
if (relativeFromCwd === '') {
this.emit('error', new Error('Cannot delete the current working directory: ' + filepath));
this.push(file);
return cb();
}
if (!options.force && relativeFromCwd.substr(0, 2) === '..') {
this.emit('error', new Error('Cannot delete files or folders outside the current working directory: ' + filepath));
this.push(file);
return cb();
}
if (options.verbose) {
console.log('gulp-rimraf: removed '+filepath);
}
rimraf(filepath, function (err) {
if (err) {
this.emit('error', err);
}
this.push(file);
cb();
}.bind(this));
}
return through.obj(del);
};