UNPKG

enola

Version:

API and CLI for obliterating files and directories

107 lines (106 loc) 3.94 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs-extra"); const path = require("path"); const errors_1 = require("./../errors"); const stat = require("./stat"); /** * Asynchronously obliterate the file/directory at the specified path. * * If the file/directory does not exist, a warning will be returned. This is * because the operation is invalid, but the desired result of the operation has * been met. * * @param {string} path * The path of the file/directory to destroy. The path can either be * absolute, or relative to the current working directory. * @param {boolean | undefined} exists * Optional `boolean` parameter. If `false`, the function will return * immediately. This parameter is automatically provided internally * when this function is called recursively. * @return {Promise<NukeResult>} * A `Promise` which resolves to a `NukeResult` object containing * information about the operation once the operation has completed. */ function nuke(dir) { return __awaiter(this, void 0, void 0, function* () { const stats = yield stat.statSafe(dir); const result = ({ stats: { type: stat.getResourceType(stats), path: dir }, children: [], success: true }); if (typeof stats === "undefined") { result.warn = errors_1.Errors.ResourceNotFound; } else if (stats.isDirectory()) { const files = (yield fs.readdir(dir)) .map(x => path.join(dir, x)); const callbacks = files .map(x => nuke(x)); const children = yield Promise.all(callbacks); yield fs.rmdir(dir); result.children.push(...children); } else if (stats.isFile()) { yield fs.unlink(dir); } return result; }); } exports.nuke = nuke; /** * Obliterate the file/directory at the specified path. * * If the file/directory does not exist, a warning will be returned. This is * because the operation is invalid, but the desired result of the operation has * been met. * * @param {string} dir * The path of the file/directory to destroy. The path can either be * absolute, or relative to the current working directory. * @return {NukeResult} * A `NukeResult` object containing information about the operation * once the operation has completed. * @throws {Error} */ function nukeSync(dir) { const stats = stat.statSyncSafe(dir); const result = ({ stats: { type: stat.getResourceType(stats), path: dir }, children: [], success: true }); if (typeof stats === "undefined") { result.warn = errors_1.Errors.ResourceNotFound; } else if (stats.isDirectory()) { const files = fs.readdirSync(dir); files .map(x => path.join(dir, x)) .map(x => nukeSync(x)) .forEach((x) => { result.children.push(x); }); fs.rmdirSync(dir); } else if (stats.isFile()) { fs.unlinkSync(dir); } return result; } exports.nukeSync = nukeSync;