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

file-bin

v0.7.0

Published

An abstraction for accessing the filesystem in Node.js.

Downloads

15

Readme

File Bin

File Bin is an exercise in writing an abstraction for file system access in Node.js.

Why does this even exist?

We're doing a project at the Turing School of Software and Design building a note-taking application using Ember and Electron. Ember Data was designed to work with APIs—not the filesystem.

  • fs.readdir returns an array of strings. APIs normally return an array of objects.
  • You have to distinguish between directories and files. fs.read doesn't work on directories.
  • Ember really likes promises and Node really likes callbacks. It would be nice to have an abstraction to bridge that gap.
  • We don't want to open weird Vim temp files and stuff like that. It would be cool if we could pass in an array of file extensions that we'd like to return.

How does it work?

Instantiating an Instance

const FileBin = require('file-bin');

let fileBin = new FileBin('/base-directory', ['.md', '.txt']);

The constructor takes two arguments:

  1. The base directory where we want to look for files.
  2. Valid extensions.

You can leave either blank. If you do, then it will default to process.cwd() and allowing all extensions respectively.

Finding a File

Instances have a #find method that will look for a file with a given file name and return a promise.

fileBin.find('README.md').then(file => {
  console.log(file);
});

The resuling file has two properties: id and content. id is the file name. content is the content of the file.

Finding All of the Files

#all will find all of the files in the base directory. If you provided an array of valid extensions, then it will filter by those extenions. The resulting files are fulling instantiated objects—just like #find above.

Note: At this moment, File Bin does not support subdirectories. They are omitted.

fileBin.all().then(files => console.log(files));

Writing a File

#write takes two arguments fileName and content. It will write the file to the filesystem and then return the object via a promise.

fileBin.write('CONTRIBUTORS.md', 'Pull requests accepted')
       .then(file => console.log(file));

Copying a File

#copy takes two arguments sourceFile and copyFile. It will write the copied file to the filesystem and then return the copy via a promise.

fileBin.copy('orignal.md', 'original-copy.md')
       .then(copy => console.log(copy));

Renaming a file

#rename takes two arguments oldFileName and newFileName. It will rename the old file to the specified new file name and return the file object via a promise.

fileBin.rename('old-name.md', 'new-name.md')
       .then((file, oldFileName, newFileName) => console.log(`${oldFileName} was successfully renamed to ${newFileName}.`)

Destroying a file

#destroy takes a single argument of the fileName that you want to delete. It will delete the specified file and return the fileName via a promise.

fileBin.destroy('filename.md')
  .then(console.log(`filename.md`))

Copying a File

#copy takes two arguments sourceFile and copyFile. It will write the copied file to the filesystem and then return the copy via a promise.

fileBin.copy('orignal.md', 'original-copy.md')
       .then(copy => console.log(copy));

Base Directory Getters and Setters

#getBaseDirectory can be called on a FileBin instance and it will return the current base directory.

#setBaseDirectory('/new/base') takes in a directory as an argument and will update the FileBin's base directory to the given directory.

#setBaseDirectory will emit an event that contains the oldDirectory and the newDirectory.

var fileBin = new FileBin('/some/directory')

console.log(fileBin.getBaseDirectory()); // --> /some/directory;

fileBin.setBaseDirectory('/new/base');

console.log(fileBin.getBaseDirectory()); // --> /new/base;