UNPKG

snapshot-fs

Version:

Create a filesystem snapshot for use with memfs

50 lines 1.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.exportSnapshot = exportSnapshot; const memfs_1 = require("memfs"); const node_fs_1 = __importDefault(require("node:fs")); const read_js_1 = require("./read.js"); const tree_js_1 = require("./tree.js"); const colorOverwrite = (str) => `\x1b[91m${str}\x1b[0m`; const colorNew = (str) => `\x1b[92m${str}\x1b[0m`; /** * Exports a snapshot to the _real_ filesystem. * * Creates the directory if it does not exist. * * @remarks * `memfs` sure makes "dry runs" easier, doesn't it? * @param kind Snapshot kind * @param data Snapshot data * @param options Options */ async function exportSnapshot(kind, data, { dest = process.cwd(), dryRun, separator } = {}) { let vol; let fs; if (dryRun) { ({ fs, vol } = (0, memfs_1.memfs)()); } else { fs = node_fs_1.default; } await fs.promises.mkdir(dest, { recursive: true }); await (0, read_js_1.readSnapshot)(kind, data, { fs, separator, source: dest }); if (dryRun) { const msg = `[INFO] Dry run! → Dest dir will be recursively created → All extant files will have permissions reset → Red or "!": Overwritten due to size mismatch → Green or "+": New files\n`; // FIXME: supports-color is ESM-only const supportsColor = await import('supports-color'); const [formatOverwrite, formatNew] = supportsColor.default.stderr ? [colorOverwrite, colorNew] : []; console.error(msg); console.error((0, tree_js_1.toTree)(vol, { formatNew, formatOverwrite, separator })); } } //# sourceMappingURL=export.js.map