snapshot-fs
Version:
Create a filesystem snapshot for use with memfs
71 lines • 2.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_MEMFS_ROOT = void 0;
exports.createCBORSnapshot = createCBORSnapshot;
exports.createCJSONSnapshot = createCJSONSnapshot;
exports.createJSONSnapshot = createJSONSnapshot;
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
const memfs_1 = require("memfs");
const index_js_1 = require("memfs/lib/snapshot/index.js");
const node_fs_1 = __importDefault(require("node:fs"));
const read_js_1 = require("./read.js");
/**
* All files will be relative to this path in the output
* {@link memfs DirectoryJSON} object
*/
exports.DEFAULT_MEMFS_ROOT = '/';
/**
* Creates a CBOR snapshot from the `from` path using the filesystem API `fs`.
*
* @param opts Options for creating the snapshot
* @returns A promise that resolves to the snapshot
*/
async function createCBORSnapshot({ fs, source: path = process.cwd(), } = {}) {
const snapshot = await (0, index_js_1.toBinarySnapshot)({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
fs: fs ? fs.promises : node_fs_1.default.promises,
path,
});
return snapshot;
}
/**
* Creates a CJSON snapshot from the `from` path using the filesystem API `fs`.
*
* @param opts Options for creating the snapshot
* @returns A promise that resolves to the snapshot
*/
async function createCJSONSnapshot({ fs, separator, source: path = process.cwd(), } = {}) {
const snapshot = await (0, index_js_1.toJsonSnapshot)({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
fs: fs ? fs.promises : node_fs_1.default.promises,
path,
separator,
});
return snapshot;
}
/**
* Creates a CJSON snapshot from the `from` path using the filesystem API `fs`.
*
* @param opts Options for creating the snapshot
* @returns A promise that resolves to the snapshot
*/
async function createJSONSnapshot({ fs, separator, source: path = process.cwd(), } = {}) {
const { vol } = (0, memfs_1.memfs)();
const tempSnapshot = await (0, index_js_1.toBinarySnapshot)({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
fs: fs ? fs.promises : node_fs_1.default.promises,
path,
separator,
});
await (0, read_js_1.readSnapshot)(read_js_1.CBOR_KIND, tempSnapshot, { fs: vol, separator });
const snapshot = (0, json_stable_stringify_1.default)(vol.toJSON());
/* c8 ignore next */
if (snapshot === undefined) {
throw new Error('Snapshot is empty!');
}
return snapshot;
}
//# sourceMappingURL=snapshot.js.map