UNPKG

snapshot-fs

Version:

Create a filesystem snapshot for use with memfs

161 lines 5.72 kB
#!/usr/bin/env node "use strict"; /* eslint-disable @typescript-eslint/unbound-method */ /** * Writes a DirectoryJSON object or snapshot (with --binary flag) to file. * * For use with memfs. * * @module snapshot-fs/cli * @see {@link https://npm.im/memfs} */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_fs_1 = __importDefault(require("node:fs")); const promises_1 = require("node:fs/promises"); const node_path_1 = __importDefault(require("node:path")); const yargs_1 = __importDefault(require("yargs")); const helpers_1 = require("yargs/helpers"); const index_js_1 = require("./index.js"); const GROUP_OUTPUT = 'Output:'; const GROUP_INPUT = 'Input:'; async function main() { await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) .version() .strict() .help() .scriptName('snapshot-fs') .epilog(`For more information, visit https://github.com/boneskull/snapshot-fs`) .options({ separator: { alias: 'sep', choices: ['posix', 'win32'], default: 'posix', describe: 'Path separator', global: true, group: GROUP_OUTPUT, }, }) .middleware((argv) => { argv.separator = argv.separator === 'posix' ? node_path_1.default.posix.sep : node_path_1.default.win32.sep; }) .command(['$0 [dest]', 'create [dest]'], 'Create memfs snapshot from filesystem', (yargs) => yargs .positional('dest', { coerce: node_path_1.default.resolve, describe: 'Path to output file', }) .options({ format: { alias: 'f', choices: [index_js_1.CBOR_KIND, index_js_1.CJSON_KIND, index_js_1.JSON_KIND], default: index_js_1.CJSON_KIND, describe: 'Snapshot format', group: GROUP_OUTPUT, nargs: 1, requiresArg: true, type: 'string', }, source: { alias: 's', coerce: node_path_1.default.resolve, default: process.cwd(), defaultDescription: '(current directory)', describe: 'File or directory to snapshot', group: GROUP_INPUT, nargs: 1, requiresArg: true, type: 'string', }, }), async ({ dest, format: kind, separator, source }) => { const pathSep = separator; if (kind === index_js_1.JSON_KIND) { console.error('[WARN] DirectoryJSON output is lossy and should be avoided'); } const output = kind === index_js_1.CBOR_KIND ? await (0, index_js_1.createCBORSnapshot)({ separator: pathSep, source, }) : kind === index_js_1.JSON_KIND ? await (0, index_js_1.createJSONSnapshot)({ separator: pathSep, source }) : await (0, index_js_1.createCJSONSnapshot)({ separator: pathSep, source }); if (dest) { await (0, promises_1.mkdir)(node_path_1.default.dirname(dest), { recursive: true }); await (0, promises_1.writeFile)(dest, output); console.error('[INFO] Wrote %s snapshot of %s to %s', kind.toUpperCase(), source, dest); } else { console.log(output); } }) .command('export <snapshot> [dest]', 'Export a JSON snapshot to the filesystem', (yargs) => yargs .positional('snapshot', { coerce: node_path_1.default.resolve, demandOption: true, describe: 'Path to snapshot file (CBOR/CJSON/DirectoryJSON)', }) .positional('dest', { coerce: node_path_1.default.resolve, default: process.cwd(), defaultDescription: 'Current working directory', describe: 'Destination directory', }) .options({ 'dry-run': { alias: 'D', description: 'Print what would be written to the filesystem', type: 'boolean', }, format: { alias: 'f', choices: [index_js_1.CBOR_KIND, index_js_1.CJSON_KIND, index_js_1.JSON_KIND], default: index_js_1.CJSON_KIND, describe: 'Snapshot format', nargs: 1, requiresArg: true, type: 'string', }, }), async ({ dest, dryRun, format: kind, snapshot }) => { const data = (await node_fs_1.default.promises.readFile(snapshot)); switch (kind) { case index_js_1.CBOR_KIND: { await (0, index_js_1.exportSnapshot)(kind, data, { dest, dryRun, }); break; } case index_js_1.CJSON_KIND: { await (0, index_js_1.exportSnapshot)(kind, data, { dest, dryRun, }); break; } case index_js_1.JSON_KIND: { await (0, index_js_1.exportSnapshot)(kind, data, { dest, dryRun, }); break; } /* c8 ignore next */ default: { throw new TypeError('Invalid format'); } } if (!dryRun) { console.error('[INFO] Exported %s snapshot of %s to %s', kind.toUpperCase(), snapshot, dest); } }) .options({}) .parseAsync(); } main().catch((err) => { console.error(err); process.exitCode = 1; }); //# sourceMappingURL=cli.js.map