UNPKG

snapshot-fs

Version:

Create a filesystem snapshot for use with memfs

181 lines 7.48 kB
#!/usr/bin/env node "use strict"; /** * 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bargs_1 = require("@boneskull/bargs"); const node_fs_1 = __importStar(require("node:fs")); const promises_1 = require("node:fs/promises"); const node_path_1 = __importDefault(require("node:path")); const node_url_1 = require("node:url"); const index_js_1 = require("./index.cjs"); // Extract version from package.json const __dirname = node_path_1.default.dirname((0, node_url_1.fileURLToPath)(require("url").pathToFileURL(__filename))); const pkg = JSON.parse((0, node_fs_1.readFileSync)(node_path_1.default.join(__dirname, '..', 'package.json'), 'utf-8')); // Shared format choices for both commands const FORMAT_CHOICES = [index_js_1.CBOR_KIND, index_js_1.CJSON_KIND, index_js_1.JSON_KIND]; async function main() { // Create command parser: options + positional const createOptions = bargs_1.opt.options({ format: bargs_1.opt.enum(FORMAT_CHOICES, { aliases: ['f'], default: index_js_1.CJSON_KIND, description: 'Snapshot format', }), separator: bargs_1.opt.enum(['posix', 'win32'], { aliases: ['sep'], default: 'posix', description: 'Path separator', }), source: bargs_1.opt.string({ aliases: ['s'], default: process.cwd(), description: 'File or directory to snapshot', }), }); const createPositionals = bargs_1.pos.positionals(bargs_1.pos.string({ description: 'Path to output file', name: 'dest' })); const createParser = createPositionals(createOptions); // Export command parser: options + positionals const exportOptions = bargs_1.opt.options({ 'dry-run': bargs_1.opt.boolean({ aliases: ['D'], description: 'Print what would be written to the filesystem', }), format: bargs_1.opt.enum(FORMAT_CHOICES, { aliases: ['f'], default: index_js_1.CJSON_KIND, description: 'Snapshot format', }), separator: bargs_1.opt.enum(['posix', 'win32'], { aliases: ['sep'], default: 'posix', description: 'Path separator', }), }); const exportPositionals = bargs_1.pos.positionals(bargs_1.pos.string({ description: 'Path to snapshot file (CBOR/CJSON/DirectoryJSON)', name: 'snapshot', required: true, }), bargs_1.pos.string({ default: process.cwd(), description: 'Destination directory', name: 'dest', })); const exportParser = exportPositionals(exportOptions); await bargs_1.bargs .create('snapshot-fs', { epilog: 'For more information, visit https://github.com/boneskull/snapshot-fs', version: pkg.version, }) .command('create', createParser, async ({ positionals, values }) => { const [destRaw] = positionals; const dest = destRaw ? node_path_1.default.resolve(destRaw) : undefined; const kind = values.format; const source = node_path_1.default.resolve(values.source); // Transform separator string to actual path separator const pathSep = values.separator === 'posix' ? node_path_1.default.posix.sep : node_path_1.default.win32.sep; 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); } }, 'Create memfs snapshot from filesystem') .command('export', exportParser, async ({ positionals, values }) => { const [snapshotRaw, destRaw] = positionals; const snapshot = node_path_1.default.resolve(snapshotRaw); const dest = node_path_1.default.resolve(destRaw); const dryRun = values['dry-run']; const kind = values.format; 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); } }, 'Export a JSON snapshot to the filesystem') .defaultCommand('create') .parseAsync(); } main().catch((err) => { console.error(err); process.exitCode = 1; }); //# sourceMappingURL=cli.js.map