snapshot-fs
Version:
Create a filesystem snapshot for use with memfs
1 lines • 3.42 kB
Source Map (JSON)
{"version":3,"file":"export.js","sourceRoot":"","sources":["../src/export.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,wCAoCC;AAnFD,iCAA2C;AAC3C,sDAA6B;AAG7B,wCAImB;AAEnB,wCAAmC;AAuBnC,MAAM,cAAc,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,WAAW,GAAG,SAAS,CAAC;AAChE,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,WAAW,GAAG,SAAS,CAAC;AAE1D;;;;;;;;;;GAUG;AACI,KAAK,UAAU,cAAc,CAIlC,IAAO,EACP,IAAO,EACP,EAAE,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,SAAS,KAA4B,EAAE;IAEvE,IAAI,GAAuB,CAAC;IAC5B,IAAI,EAAS,CAAC;IACd,IAAI,MAAM,EAAE,CAAC;QACX,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,IAAA,aAAK,GAAE,CAAC,CAAC;IAC1B,CAAC;SAAM,CAAC;QACN,EAAE,GAAG,iBAAM,CAAC;IACd,CAAC;IAED,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEnD,MAAM,IAAA,sBAAY,EAAC,IAAW,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAEvE,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,GAAG,GAAG;;;;mCAImB,CAAC;QAEhC,oCAAoC;QACpC,MAAM,aAAa,GAAG,wDAAa,gBAAgB,GAAC,CAAC;QACrD,MAAM,CAAC,eAAe,EAAE,SAAS,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,MAAM;YAC/D,CAAC,CAAC,CAAC,cAAc,EAAE,QAAQ,CAAC;YAC5B,CAAC,CAAC,EAAE,CAAC;QAEP,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,CAAC,KAAK,CAAC,IAAA,gBAAM,EAAC,GAAI,EAAE,EAAE,SAAS,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;IACzE,CAAC;AACH,CAAC","sourcesContent":["import { memfs, type Volume } from 'memfs';\nimport nodeFs from 'node:fs';\nimport { type sep } from 'node:path';\n\nimport {\n readSnapshot,\n type SnapshotForKind,\n type SnapshotKind,\n} from './read.js';\nimport { type FsApi } from './snapshot.js';\nimport { toTree } from './tree.js';\n\n/**\n * Options for functions in this module\n */\nexport interface ExportSnapshotOptions {\n /**\n * Destination directory\n */\n dest?: string;\n\n /**\n * If `true`, write to a virtual filesystem and dump a difflike tree\n */\n\n dryRun?: boolean;\n\n /**\n * Path separator in snapshot (?)\n */\n separator?: typeof sep;\n}\n\nconst colorOverwrite = (str: string) => `\\x1b[91m${str}\\x1b[0m`;\nconst colorNew = (str: string) => `\\x1b[92m${str}\\x1b[0m`;\n\n/**\n * Exports a snapshot to the _real_ filesystem.\n *\n * Creates the directory if it does not exist.\n *\n * @remarks\n * `memfs` sure makes \"dry runs\" easier, doesn't it?\n * @param kind Snapshot kind\n * @param data Snapshot data\n * @param options Options\n */\nexport async function exportSnapshot<\n K extends SnapshotKind,\n T extends SnapshotForKind<K>,\n>(\n kind: K,\n data: T,\n { dest = process.cwd(), dryRun, separator }: ExportSnapshotOptions = {},\n): Promise<void> {\n let vol: undefined | Volume;\n let fs: FsApi;\n if (dryRun) {\n ({ fs, vol } = memfs());\n } else {\n fs = nodeFs;\n }\n\n await fs.promises.mkdir(dest, { recursive: true });\n\n await readSnapshot(kind as any, data, { fs, separator, source: dest });\n\n if (dryRun) {\n const msg = `[INFO] Dry run!\n → Dest dir will be recursively created\n → All extant files will have permissions reset\n → Red or \"!\": Overwritten due to size mismatch\n → Green or \"+\": New files\\n`;\n\n // FIXME: supports-color is ESM-only\n const supportsColor = await import('supports-color');\n const [formatOverwrite, formatNew] = supportsColor.default.stderr\n ? [colorOverwrite, colorNew]\n : [];\n\n console.error(msg);\n console.error(toTree(vol!, { formatNew, formatOverwrite, separator }));\n }\n}\n"]}