Debian Codegraph @

fs2/rmdir-sync.js

raw
1 "use strict";
2
3 const fs = require("fs")
4 , path = require("path")
5 , ensureString = require("type/string/ensure")
6 , isObject = require("type/object/is")
7 , toShortString = require("type/lib/to-short-string");
8
9 module.exports = (dirnameInput, options = {}) => {
10 const dirname = path.resolve(ensureString(dirnameInput));
11 if (!isObject(options)) options = {};
12 if (!options.recursive) {
13 fs.rmdirSync(dirname);
14 return;
15 }
16 try {
17 emptyDirSync(dirname, options);
18 fs.rmdirSync(dirname);
19 } catch (error) {
20 if (!error.errors) throw error;
21 let errorMessage = `Could not remove ${ toShortString(dirnameInput) } due to:\n- ${
22 error.errors.slice(0, 5).map(({ code, _filename }) => `${ code } on ${ _filename }`)
23 }`;
24 if (error.errors.length > 5) {
25 errorMessage += `\nand ${ error.errors.length - 5 } other errors`;
26 }
27 throw Object.assign(new Error(errorMessage), { errors: error.errors });
28 }
29 };
30
31 const emptyDirSync = require("./empty-dir-sync");