1.0.0-alpha.1 • Published 4 years ago

proxy-to-fs v1.0.0-alpha.1

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

CircleCI

Description

The way of accessing the File System as close as possible to manipulating JS object. Implemented using Proxy.

import ProxyToFS from 'proxy-to-fs';
import fs from 'fs'; // or NodeJS fs module or BrowserFS

Reading files

// create story by providing fs module and the path where it will be mounted
const store = new ProxyToFS({ fs, path: '/' });

// read the text file
const textContent = await store.directory['content.md'].asText;

// alternatively you can provide the entire path
const theSameTextContent = await store['directory/content.md'].asText;

// read the binary file as a data: URL
const dataURL = await store.directory['image.png'].asDataURL;

Writing files

// write text to a file
store.directory['content-new.md'] = 'New text content';

// wait for the all the operations to finish (like saving the file)
await store;
// bulk write
Object.assign(store, {
    directory: {
        'file1.txt': 'Content of a file1',
        'file2.txt': 'Content of a file2'
    }
});

Removing files

// delete a file
delete store.directory['content.md'];

...

await store;

Removing directory and its contents

// delete directory and its content
delete store.directory;

...

await store;

Reading directory content

// iterating over directory items
for (const item in await store.directory) {

}

// get directory items
Object.keys(await store.directory);