Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create a snapshot of a directory for use with memfs

License

NotificationsYou must be signed in to change notification settings

boneskull/snapshot-fs

Repository files navigation

Create a snapshot of a directory for use withmemfs

snapshot-fs logo

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, thesnapshot-fs package ships a CLI,snapshot-fs, which converts a directory tree to aCompact JSON,CBOR orDirectoryJSON snapshot for use withmemfs.

🎁New in v5.0.0

  • The command-line options have changed; seeUsage below.
  • The default input/output format is now CJSON.
  • CBOR snapshots are now supported.
  • Theexport subcommand got a--dry-run flag.

🎁New in v2.0.0

  • The command-line options have changed; seeUsage below.
  • snapshot-fs can now re-create a directory on the filesystem from aJSON snapshot.
  • loadSnapshot() is nowreadSnapshot()
  • Export functionality exposed viaexportSnapshot()

Install

snapshot-fs requires Node.js v22.13.0 or newer.

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

snapshot-fs exports both CommonJS and ES modules.

Usage

snapshot-fs [dest]Create memfs snapshot from filesystemCommands:  snapshot-fs create [dest]             Create memfs snapshot from filesystem                                                                       [default]  snapshot-fs export <snapshot> [dest]  Export a JSON snapshot to the filesystemOutput:      --separator, --sep  Path separator                                  [choices: "posix", "win32"] [default: "posix"]  -f, --format            Snapshot format                  [string] [choices: "cbor", "cjson", "json"] [default: "cjson"]Positionals:  dest  Path to output fileInput:  -s, --source  File or directory to snapshot                                         [string] [default: (current directory)]Options:      --version  Show version number                                   [boolean]      --help     Show help                                             [boolean]For more information, visit https://github.com/boneskull/snapshot-fs

When run without a subcommand,create will be invoked.

create w/Compact JSON Snapshots

By default (as of v5.0.0),snapshot-fs will create a snapshot inCJSON/Compact JSON format.

snapshot-fs --source /some/dir /path/to/output.json

This is equivalent to:

snapshot-fs create --source /some/dir /path/to/output.json --format=cjson

In your code, you can use the resulting file usingmemfs directly:

import{typeJsonUint8Array,typeSnapshotNode,fromJsonSnapshot}from'memfs/lib/snapshot/index.js';import{memfs}from'memfs';constdata=(awaitreadFile('/path/to/output.json',))asunknownasJsonUint8Array<SnapshotNode>;const{vol}=memfs()awaitfromJsonSnapshot(data,fs:vol.promises);console.log(vol.toTree());// ... do your thing

...or you can use thereadCJSONSnapshot() helper fromsnapshot-fs:

importtype{JsonUint8Array,SnapshotNode}from'memfs/lib/snapshot/index.js';import{readCJSONSnapshot}from'snapshot-fs';constdata=(awaitreadFile('/path/to/output.json',))asunknownasJsonUint8Array<SnapshotNode>;constvol=awaitreadCJSONSnapshot(data);console.log(vol.toTree());

This is fast;JSON.parse() is never called!

(but we can get faster...)

create w/CBOR Snapshots

Similar to the above, you can create a CBOR-formatted snapshot this way:

snapshot-fs --source /some/dir /path/to/output.json --format=cbor

In your code, you can use the resulting file usingmemfs directly:

import{typeSnapshotNode,fromBinarySnapshot}from'memfs/lib/snapshot/index.js';importtype{CborUint8Array}from'@jsonjoy.com/json-pack/lib/cbor/types.js';import{memfs}from'memfs';constdata=(awaitreadFile('/path/to/output.json',))asunknownasCborUint8Array<SnapshotNode>;const{vol}=memfs()awaitfromBinarySnapshot(data,fs:vol.promises);console.log(vol.toTree());// ... do your thing

...or you can use thereadCBORSnapshot() helper fromsnapshot-fs:

import{typeSnapshotNode,fromBinarySnapshot,}from'memfs/lib/snapshot/index.js';importtype{CborUint8Array}from'@jsonjoy.com/json-pack/lib/cbor/types.js';import{readCBORSnapshot}from'snapshot-fs';constdata=(awaitreadFile('/path/to/output.json',))asunknownasCborUint8Array<SnapshotNode>;constvol=awaitreadCBORSnapshot(data);console.log(vol.toTree());

create w/DirectoryJSON Snapshots

Caution

DirectoryJSON is somewhat lossy and should be avoided if you ever want to re-create snapshots on yourreal filesystem (e.g., usingexport). For a directory full of text files, this is fine; for anything else, use CJSON or CBOR.

snapshot-fs --source /some/dir /path/to/output.json --format=json

This can be read into amemfsVolume like so:

import{readFile}from'node:fs/promises';import{memfs}from'memfs';constdirectoryJson=JSON.parse(awaitreadFile('/path/to/output.json','utf8'),);const{ vol}=memfs();vol.fromJSON(directoryJson);

export

This allows you to re-create a directory on the filesystem from a snapshot. Handy!

snapshot-fs export <snapshot> [dest]Export a JSON snapshot to the filesystemPositionals:  snapshot  Path to snapshot file (CBOR/CJSON/DirectoryJSON)          [required]  dest      Destination directory           [default: Current working directory]Output:      --separator, --sep  Path separator                                  [choices: "posix", "win32"] [default: "posix"]Options:      --version  Show version number                                   [boolean]      --help     Show help                                             [boolean]  -D, --dry-run  Print what would be written to the filesystem         [boolean]  -f, --format   Snapshot format                  [string] [choices: "cbor", "cjson", "json"] [default: "cjson"]

If you have a snapshot (either format) and you want to re-create snapshot on the filesystem, use theexport subcommand:

snapshot-fsexport /path/to/snapshot.json /path/to/output

The destination directory will be created if it doesn't exist.

Tip

Use the--dry-run flag withexport to see what would be written to the filesystem.

API

Some potentially-useful stuff exported fromsnapshot-fs:

  • createSnapshot()/createDirectoryJSONSnapshot()/createCJSONSnapshot()/createCBORSNapshot() - Create a JSON snapshot from a real or virtual FS
  • readSnapshot()/readDirectoryJSONSnapshot()/readCBORSnapshot()/readCJSONSnapshot() - Read a snapshot from a file and load it into a real or virtual FS
  • exportSnapshot() - Alias forreadSnapshot() defaulting to the real FS

See the typings for more information.

License

Copyright 2024Christopher Hiller. Licensed Apache-2.0

About

Create a snapshot of a directory for use with memfs

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp