1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var isPathCwd = require('is-path-cwd');
|
11 | var isPathInCwd = require('is-path-in-cwd');
|
12 | var rimraf = require('rimraf');
|
13 |
|
14 | function safeCheck(filepath) {
|
15 | if (isPathCwd(filepath)) {
|
16 | throw new Error('Cannot delete the current working directory. Can be overriden with the `force` option.');
|
17 | }
|
18 |
|
19 | if (!isPathInCwd(filepath)) {
|
20 | throw new Error('Cannot delete files/folders outside the current working directory. Can be overriden with the `force` option.');
|
21 | }
|
22 | }
|
23 |
|
24 | module.exports = function (filepath, options, next) {
|
25 | if (typeof options === 'function') {
|
26 | next = options;
|
27 | options = {};
|
28 | }
|
29 |
|
30 | if (!(options && options.force)) {
|
31 | safeCheck(filepath);
|
32 | }
|
33 | return rimraf(filepath, next);
|
34 | };
|
35 |
|
36 |
|
37 | module.exports.sync = function (filepath, options) {
|
38 | if (!(options && options.force)) {
|
39 | safeCheck(filepath);
|
40 | }
|
41 | return rimraf.sync(filepath);
|
42 | };
|