snapshot-fs
Version:
Create a filesystem snapshot for use with memfs
44 lines • 1.5 kB
JavaScript
import { memfs } from 'memfs';
import nodeFs from 'node:fs';
import { readSnapshot, } from './read.js';
import { toTree } from './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
*/
export async function exportSnapshot(kind, data, { dest = process.cwd(), dryRun, separator } = {}) {
let vol;
let fs;
if (dryRun) {
({ fs, vol } = memfs());
}
else {
fs = nodeFs;
}
await fs.promises.mkdir(dest, { recursive: true });
await 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(toTree(vol, { formatNew, formatOverwrite, separator }));
}
}
//# sourceMappingURL=export.js.map