npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sapphirejs/filesystem

v0.1.3

Published

Abstract Filesystem

Downloads

4

Readme

Filesystem

An abstract filesystem that makes it working with files and directories a breeze. It's main advantage are drivers, swappable pieces of code that interact with a specific filesystem. It includes a Local driver, with plans to add interfaces for at least AWS, FTP/SFTP, and a Test driver. Adding to the list of benefits are things like Promises, and abstractions and error checking over native functions.

Usage

We'll install and setup a new instance of Filesystem with the included Local driver.

$ npm install --save @sapphirejs/filesystem
const { Filesystem, Driver } = require('@sapphirejs/filesystem')

const fs = new Filesystem(new Driver.Local())
const contents = await fs.read('some/file.txt')

Simple and with await, a pleasure to work with. All Filesystem's functions return a Promise, so no more promisify or wrapping the functions into your own promises. As you may imagine, await needs to be called in an async function, but we're ommiting that for simplicity's sake.

Drivers can even be switched on the fly. Currently we have only one, but imagine we have a Memory driver that for some reason we want to write a file into.

const fs = new Filesystem(new Driver.Local())
// Fictitious Memory driver
const memory = new Transport.Memory()

// Using the Local driver.
const exists = await fs.exists('file.txt')
if (!exists) {
  // Switching to the Memory driver.
  await fs.in(memory).write('file.txt', 'Hello from Memory')
}

// Now we're back to the Local driver.
await fs.createDir('some/dir')

API

read(path) : string Read the contents of the file in path.

write(path, data) Write data to the file in path.

exists(path) : boolean Check if file in path exists.

isDirectory(path) : boolean Check if path is a directory.

isFile(path) : boolean Check if path is a file.

isSymbolicLink(path) : boolean Check if path is a symbolic link.

delete(path) Delete path. Supports both files and directories.

deleteAll(path) Delete directory in path recursively, deleting every file and subdirectory. Be cautious when using it, especially with dynamic parameters, as it will wipe out entire directories.

append(path, data) Append data to the file in path.

chmod(path, mode) Set mode (octal, ie: 0o755) permissions to the file in path.

copy(source, destination, overwrite = true) Copy file source into destination.

createDir(path, mode = 0o777, recursively = false) Create a directory in path with mode permissions. If recursively is set to true, it will try to create parent directories too, similar to mkdir -p.

readDir(path) : array Read non-recursively files and directories in directory path.

rename(oldPath, newPath) : array Rename file in oldPath to newPath if it doesn't exist.

Drivers

Drivers are classes that need to implement all of the above methods and wrap the return value into a Promise. Those methods that don't make sense in the environment the driver is being built for, you can just return a dummy Promise and consider it's call always valid. For example, let's say you're developing a Dropbox driver but won't be using chmod.

class DropboxDriver {
  chmod(path, mode) {
    return Promise.resolve(true)
  }
}