UNPKG

enola

Version:

API and CLI for obliterating files and directories

71 lines (70 loc) 2.39 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); const nuke_1 = require("./../system/fs/nuke"); const cwd_1 = require("./../system/cwd"); const error = require("./../system/errors"); const index = require("./index"); function timedNuke(dir) { const start = process.hrtime(); return nuke_1.nuke(dir) .then(function (result) { const elapsed = process.hrtime(start); const seconds = elapsed[0] + elapsed[1] * 1e-12; return ({ elapsed: seconds, warn: result.warn, result, }); }); } exports.timedNuke = timedNuke; (function (argv) { const logger = index.getLogger(argv); const paths = argv._.slice(2) .map((x) => path.isAbsolute(x) ? x : cwd_1.cwd(x)); const promises = paths .map((x) => ({ timestamp: process.hrtime(), path: x, promise: nuke_1.nuke(x) })) .map((x) => x.promise.then((res) => { const elapsed = process.hrtime(x.timestamp); return ({ elapsed: elapsed[0] + elapsed[1] * 1e-12, timestamp: x.timestamp, path: x.path, result: res }); })) .map((x) => { function printChildren(res) { res.children.forEach(printChildren); if (res.warn) { const message = error.getDefaultErrorMessage(res.warn); logger.warn(message); } if (res.success) { logger.info(`Nuked ${res.stats.path}`); } } return x.then((res) => { printChildren(res.result); logger.debug(`Took ${res.elapsed.toFixed(12)} seconds`); }); }); Promise.all(promises) .then(() => void logger.info("Done!")) .catch((err) => void logger.error(err)); })(index.argh .scriptName("nuke") .usage("$0 <\"path\"> [\"path2\" [... \"pathN\"]]") .example("$0 \"./node_modules\"", "Delete directory \"./node_modules\"") .example("$0 \"./dir1\" \"./dir2\" \"./dir3\"", "Delete directories \"./dir1\", \"dir2\", and \"dir3\"") .example("$0 \"file.txt\"", "Delete file \"file.txt\"") .example("$0 \"./node_modules\" -p", "Delete directory \"./node_modules\", with pretty output") .parse(process.argv));