Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Create a snapshot of a directory for use with memfs
License
boneskull/snapshot-fs
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Create a snapshot of a directory for use with
memfs
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.
- The command-line options have changed; seeUsage below.
- The default input/output format is now CJSON.
- CBOR snapshots are now supported.
- The
exportsubcommand got a--dry-runflag.
- The command-line options have changed; seeUsage below.
snapshot-fscan now re-create a directory on the filesystem from aJSON snapshot.loadSnapshot()is nowreadSnapshot()- Export functionality exposed via
exportSnapshot()
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.
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-fsWhen 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());
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);
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/outputThe 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.
Some potentially-useful stuff exported fromsnapshot-fs:
createSnapshot()/createDirectoryJSONSnapshot()/createCJSONSnapshot()/createCBORSNapshot()- Create a JSON snapshot from a real or virtual FSreadSnapshot()/readDirectoryJSONSnapshot()/readCBORSnapshot()/readCJSONSnapshot()- Read a snapshot from a file and load it into a real or virtual FSexportSnapshot()- Alias forreadSnapshot()defaulting to the real FS
See the typings for more information.
Copyright 2024Christopher Hiller. Licensed Apache-2.0
About
Create a snapshot of a directory for use with memfs
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
