@srhenry/storage-manager
TypeScript icon, indicating that this package has built-in type declarations

1.4.12 • Public • Published

Storage Manager

Storage Manager module to wrap Node.js filesystem access with ease. This module is Promise-ready, all methods return promises or stream objects. Both Javascript (CommonJS) and Typescript (ES6+) are supported.


GitHub GitLab

Npm package version Npm package total downloads Npm package license


Table of Contents



Installing

npm install @srhenry/storage-manager --save

or from git repository:

git clone git@gitlab.com:SrHenry/storage-manager.git
cd storage-manager
npm run build

Docs


Some examples

/**
 * Basic file manipulation
 */

import { StorageManager } from "@srhenry/storage-manager"

// Get content from file as string
let content = await StorageManager.get("path/to/file")

// Write content to file
await StorageManager.put("path/to/file", "Hello World!")

// Append content to file
await StorageManager.append("path/to/file", "Tail here :)")

// Check if file or directory exists:
if (await StorageManager.exists("path/to/file")) {
    // Do something if file exists
} else {
    // Do something if file doesn't exist
}

// Create directory (if not exist):
await StorageManager.mkdir("new/path")

// Delete file
await StorageManager.delete("path/to/file")

// Get stream object of a file
let stream = StorageManager.fileStream("path/to/file")

// Fetching directory list from path (no recursion)
let dirs = await StorageManager.listDirectory("path")

/*
    Fetching directory list from path (with recursion).
    It returns a DirectoryList representing given path,
    which is iterable (for..of, spread operator),
    and it contains strings for inner files and another DirectoryList instance for each inner directory.
    You can see directory name in DirectoryList with `name` property accessor.
*/
let dirs_deep = await StorageManager.listDirectory("path", true)

// copying files or directories:
await StorageManager.copy("path/to/file", "new/path")
// copying files or directories (w/ renaming):
await StorageManager.copy("path/to/file", "new/path", "new_name")

// renaming files or directories
await StorageManager.rename("path/to/file", "new_name")

// moving files or directories
await StorageManager.move("path/to/file", "new/path")
// moving files or directories (w/ renaming)
await StorageManager.move("path/to/file", "new/path", "new_name")

// fetching file or directory metadata
// @see https://nodejs.org/api/fs.html#fs_class_fs_stats for further documentation on those metadata
const metadata = await Storage.stats("path/to/file")

let isFile = metadata.isFile() // Returns true if is a file
let isDirectory = metadata.isDirectory() // Returns true if is a directory

// shortand for check if given path is a file
isFile = await StorageManager.isFile("path/to/file")
// shortand for check if given path is a directory
isDirectory = await StorageManager.isDirectory("path/to/file")

Package Sidebar

Install

npm i @srhenry/storage-manager

Weekly Downloads

2

Version

1.4.12

License

MIT

Unpacked Size

78.3 kB

Total Files

11

Last publish

Collaborators

  • srhenry