Code Monkey home page Code Monkey logo

snapshot-fs's Introduction

snapshot-fs

Create a snapshot of a directory for use with memfs

About

I wanted something to help convert directories of test fixtures on-disk to a format usable by an in-memory filesystem (memfs).

To that end, the snapshot-fs package ships a CLI, snapshot-fs, which converts a directory tree to a DirectoryJSON object (or JSON snapshot) for use with memfs.

Install

snapshot-fs requires Node.js v20.0.0 or newer.

snapshot-fs can be run via npx snapshot-fs or installed globally via npm install -g snapshot-fs. Or however else you want to consume it.

snapshot-fs exports both CommonJS and ES modules.

Usage

  snapshot-fs [options..] <dir> [dest.json]

  Writes a DirectoryJSON object or snapshot
  (with --binary flag) to file. For use with memfs

  Options:

    --binary/-b     - Output memfs JSON snapshot
    --root/-r       - DirectoryJSON root (default: /)
    --help/-h       - Show this help message

If dest.json is not provided, the output will be written to stdout.

Typical Example

snapshot-fs /some/dir /path/to/output.json
import { memfs } from 'memfs';
import { readFile } from 'node:fs/promises';

/** @type {import('memfs').DirectoryJSON} */
const directoryJson = await readFile('/path/to/output.json', 'utf8').then(
  JSON.parse,
);

const { vol } = memfs(directoryJson);

console.log(vol.toTree());

// ... do your thing

Binary File Example

memfs's snapshots support binary files, but the DirectoryJSON format does not. If you need to support binary files, use the --binary flag. This creates a JSON snapshot (for the curious, it's encoded as "Compact JSON") and must be consumed differently.

Note

The resulting JSON is for machines.

snapshot-fs --binary /some/dir /path/to/output.json
import { readFile } from 'node:fs/promises';
import { fromJsonSnapshot } from 'memfs/lib/snapshot/index.js';

/** @type {string} */
const snapshotJson = await readFile('/path/to/output.json', 'utf8');

/** @type {UInt8Array} */
const snapshot = new TextEncoder().encode(snapshotJson);

const { vol } = memfs();

// `fromJsonSnapshot` will populate the Volume at the root path /
// with the snapshot data
await fromJsonSnapshot(snapshot, { fs: vol.promises, path: '/' });

Alternatively, snapshot-fs exports loadSnapshot(), which does the equivalent of the above:

import { readFile } from 'node:fs/promises';
import { loadSnapshot } from 'snapshot-fs';
import { type Volume } from 'memfs';

/** @type {string} */
const snapshotJson = await readFile('/path/to/output.json', 'utf8');

const vol: Volume = await loadSnapshot(snapshotJson);

// or, if you already have a Volume
const {vol: myVol} = memfs(); // <!-- your volume from elsewhere

// using a different root path
await loadSnapshot(snapshotJson, { fs: myVol, path: '/some/other/path' });

License

Copyright 2024 Christopher Hiller. Licensed Apache-2.0

snapshot-fs's People

Contributors

boneskull avatar github-actions[bot] avatar renovate[bot] avatar

Stargazers

 avatar

Watchers

 avatar  avatar

snapshot-fs's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency @types/node to v20.12.14
  • chore(deps): update dependency lint-staged to v15.2.5
  • chore(deps): update dependency @types/node to v20.14.1
  • chore(deps): update dependency prettier to v3.3.0
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/commitlint.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
  • bahmutov/npm-install v1.10.2@e5c7e14408aa6089501de32bd16123b41738047e
  • wagoid/commitlint-github-action v6@7f0a61df502599e1f1f50880aaa7ec1e2c0592f2
.github/workflows/nodejs.yml
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
  • bahmutov/npm-install v1.10.2@e5c7e14408aa6089501de32bd16123b41738047e
.github/workflows/release.yml
  • google-github-actions/release-please-action v4.0.3@078b9b8dda7799db29972b379561ff8e19b08e82
  • actions/checkout v4.1.6@a5ac7e51b41094c92402da3b24376905380afc29
  • actions/setup-node v4.0.2@60edb5dd545a775178f52524783378180af0d1f8
  • bahmutov/npm-install v1.10.2@e5c7e14408aa6089501de32bd16123b41738047e
npm
package.json
  • glob 10.3.16
  • is-binary-path 2.1.0
  • memfs 4.8.2
  • @commitlint/cli 19.2.2
  • @commitlint/config-conventional 19.1.0
  • @eslint/js 8.57.0
  • @matteo.collina/snap 0.3.0
  • @stylistic/eslint-plugin-js 1.7.2
  • @stylistic/eslint-plugin-ts 1.7.2
  • @tsconfig/node20 20.1.4
  • @types/eslint__js 8.42.3
  • @types/node 20.12.12
  • c8 9.1.0
  • dependency-cruiser 16.2.4
  • eslint 8.57.0
  • eslint-plugin-jsonc 2.15.1
  • eslint-plugin-n 16.6.2
  • husky 9.0.11
  • lint-staged 15.2.4
  • npm-run-all2 6.1.2
  • prettier 3.2.5
  • prettier-plugin-jsdoc 1.3.0
  • prettier-plugin-organize-imports 3.2.4
  • prettier-plugin-pkg 0.18.1
  • tshy 1.13.1
  • tsx 4.7.3
  • typescript 5.4.3
  • typescript-eslint 7.4.0
  • node >=20.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.