@dmitriyzverev/fs-map
v1.0.0-dev.0
Published
_Node.JS filesystem facade implementing the Map interface. It can be useful when you need simplify access to filesystem._
Downloads
2
Readme
@dmitriyzverev/fs-map
Node.JS filesystem facade implementing the Map interface. It can be useful when you need simplify access to filesystem.
Usage
Install dependencies:
npm i -SE @dmitriyzverev/fs-map
Create a file system map:
import {createFsMap} from '@dmitriyzverev/fs-map'; const fsMap = createFsMap({});
Now you can create/read/delete files as Map items:
fsMap.set('/foo/bar/Baz.txt', 'Some file content'); console.log(fsMap.get('/foo/bar/Baz.txt')); // Some file content
Options
rootDir
By default, the root directory is '/'
. But you can define your own:
const fsMap = createFsMap({rootDir: '/root'});
All relative paths will be resolved relative new root directory:
fsMap.set('foo/bar/Baz.txt', ''); // a file will be created with the path /root/foo/bar/Baz.txt
files
It is possible to create needed files on initialization step, using files
option:
const fsMap = createFsMap({
files: [
['/foo.txt', 'Foo text'],
['/bar/baz.txt', 'Baz text'],
],
});
console.log(fsMap.get('/bar/baz.txt')); // Baz text
All files will be created include all needed directories.
fs
You can inject your own file system instead of the native Node.JS file system. For example, you can use in-memory file system:
import {fs} from 'memfs';
import {createFsMap} from '@dmitriyzverev/fs-map';
const fsMap = createFsMap({fs});