UNPKG

snapshot-fs

Version:

Create a filesystem snapshot for use with memfs

108 lines 4.43 kB
"use strict"; /** * Provides {@link toTree a function} to dump the contents of a `Volume`, * comparing it to the real filesystem. * * Portions of this code adapted from * https://github.com/streamich/memfs/blob/2c6a6ca55ad2e661f40e488fe5ea4087438bae0e/src/print/index.ts#L6-L30 * and are copyrighted by the original author and licesned under Apache-2.0. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toTree = void 0; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const posix_1 = require("path/posix"); const tree_dump_1 = require("tree-dump"); const toTree = (fs, { depth = 10, separator = posix_1.sep, dir = separator, formatNew = (str) => `+ ${str}`, formatOverwrite = (str) => `! ${str}`, tab = '', }) => { if (dir[dir.length - 1] !== separator) { dir += separator; } let subtree = ' (...)'; if (depth > 0) { const list = fs.readdirSync(dir, { withFileTypes: true }); let actualFiles; try { actualFiles = new Map(node_fs_1.default .readdirSync(dir, { withFileTypes: true }) .map((entry) => [entry.name, entry])); } catch { actualFiles = new Map(); } subtree = (0, tree_dump_1.printTree)(tab, list.map((entry) => (tab) => { if (entry.isDirectory()) { return (0, exports.toTree)(fs, { depth: depth - 1, dir: `${dir}${entry.name}`, formatNew, formatOverwrite, tab, }); } let formattedName; let willBeOverwritten = false; const willBeCreated = !actualFiles.has(`${entry.name}`); if (!willBeCreated) { const actualDirent = actualFiles.get(`${entry.name}`); if (entry.isSymbolicLink()) { if (!actualDirent.isSymbolicLink()) { willBeOverwritten = true; } else { try { const actualRealpath = node_fs_1.default.realpathSync(node_path_1.default.join(dir, `${entry.name}`)); const realpath = fs.readlinkSync(node_path_1.default.join(dir, `${entry.name}`)); if (actualRealpath !== realpath) { willBeOverwritten = true; } } catch { willBeOverwritten = true; } } } else { // implies file if (!actualDirent.isFile()) { willBeOverwritten = true; } else { try { const actualStat = node_fs_1.default.statSync(node_path_1.default.join(dir, `${entry.name}`)); const stat = fs.statSync(node_path_1.default.join(dir, `${entry.name}`)); if (actualStat.size !== stat.size) { willBeOverwritten = true; } } catch { willBeOverwritten = true; } } } } if (willBeOverwritten) { formattedName = formatOverwrite(`${entry.name}`); } else if (willBeCreated) { formattedName = formatNew(`${entry.name}`); } else { // identical, hopefully formattedName = `${entry.name}`; } if (entry.isSymbolicLink()) { return `${formattedName}${fs.readlinkSync(node_path_1.default.join(dir, `${entry.name}`))}`; } else { return formattedName; } })); } const base = (0, posix_1.basename)(dir, separator) + separator; return base + subtree; }; exports.toTree = toTree; //# sourceMappingURL=tree.js.map