UNPKG

1.23 kBJavaScriptView Raw
1'use strict'
2
3const fse = require('fs-extra')
4const isPathInside = require('is-path-inside')
5const log = require('./log')
6
7/**
8 * Delete an entire directory with all its files and folders.
9 * Will skip the deletion without an error when folder does not exist.
10 * @public
11 * @param {String} distPath - Path to the destination folder.
12 * @param {String} cwdPath - Current working directory.
13 * @param {Object} opts - Additional optional options.
14 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
15 * @returns {?*}
16 */
17module.exports = function(distPath, cwdPath, opts, next) {
18
19 // Check if distPath is inside cwdPath. This prevents us from deleting ourselves.
20 // isPathInside returns false when both paths are the same.
21 const isSafePath = isPathInside(distPath, cwdPath)
22
23 if (isSafePath===false) {
24 return next(new Error(`Specified distPath must be inside the current working directory to prevent us from deleting ourself`))
25 }
26
27 if (opts.verbose===true) log(`{cyan:Deleting folder: {grey:${ distPath }`)
28
29 fse.remove(distPath, (err) => {
30
31 if (err!=null) return next(err)
32
33 if (opts.verbose===true) log(`{cyan:Deleted folder: {grey:${ distPath }`)
34
35 next()
36
37 })
38
39}
\No newline at end of file