@mstssk/cleandir
Version:
Cleanup directories.
52 lines (51 loc) • 1.4 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleandir = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const IGNORE_FILES = [".keep", ".gitkeep"];
/**
* Cleanup directories.
*
* @param {string | string[]} dirs
*/
async function cleandir(dirs) {
if (typeof dirs === "string") {
dirs = [dirs];
}
for (const dir of dirs) {
await _cleandir(dir);
}
}
exports.cleandir = cleandir;
/**
* Cleanup specified directory.
*
* @param {string} dirPath
*/
async function _cleandir(dirPath) {
let dir;
try {
dir = await node_fs_1.promises.opendir(dirPath);
}
catch (err) {
switch (err.code) {
case "ENOENT":
return; // Noop when directory don't exists.
case "ENOTDIR":
throw new Error(`'${dirPath}' is not a directory.`);
default:
throw err;
}
}
for await (const file of dir) {
if (IGNORE_FILES.includes(file.name)) {
continue;
}
const filePath = node_path_1.default.join(dirPath, file.name);
await node_fs_1.promises.rm(filePath, { recursive: true });
}
}
;