@rxap/node-utilities
Version:
Provides a set of utility functions for Node.js development, including file system operations, git integration, and package.json manipulation. It offers functionalities like copying folders, reading JSON files with retry logic, retrieving the current git
31 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoveDirSync = RemoveDirSync;
const fs_1 = require("fs");
const path_1 = require("path");
/**
* Recursively removes a directory and all its contents.
*
* This function synchronously deletes the specified directory along with all its subdirectories and files.
* It first reads the contents of the directory, then iterates over each item. If the item is a directory,
* it recursively calls itself to delete that subdirectory; if the item is a file, it deletes the file directly.
* After all contents are removed, it deletes the empty directory.
*
* @param dirPath The path to the directory that should be removed.
* @throws {Error} Throws if the directory does not exist, or if an error occurs during removal.
*/
function RemoveDirSync(dirPath) {
const files = (0, fs_1.readdirSync)(dirPath);
for (const file of files) {
const filePath = (0, path_1.join)(dirPath, file);
const stat = (0, fs_1.statSync)(filePath);
if (stat.isDirectory()) {
RemoveDirSync(filePath);
}
else {
(0, fs_1.unlinkSync)(filePath);
}
}
(0, fs_1.rmdirSync)(dirPath);
}
//# sourceMappingURL=remove-dir-sync.js.map