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 */
16module.exports = function(distPath, cwdPath, opts, next) {
17
18 // Check if distPath is inside cwdPath. This prevents us from deleting ourselves.
19 // isPathInside returns false when both paths are the same.
20 const isSafePath = isPathInside(distPath, cwdPath)
21
22 if (isSafePath===false) {
23 return next(new Error(`Specified distPath must be inside the current working directory to prevent us from deleting ourself`))
24 }
25
26 if (opts.verbose===true) log(`{cyan:Deleting folder: {grey:${ distPath }`)
27
28 fse.remove(distPath, (err) => {
29
30 if (err!=null) return next(err)
31
32 if (opts.verbose===true) log(`{cyan:Deleted folder: {grey:${ distPath }`)
33
34 next()
35
36 })
37
38}
\No newline at end of file