0.7.0 • Public • Published

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. 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('').then(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('', '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('', '')
       .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('', '')
       .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.


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('', '')
       .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;
console.log(fileBin.getBaseDirectory()); // --> /new/base;



Package Sidebar


npm i file-bin

Weekly Downloads






Last publish


  • aaronc
  • joshuajhun
  • rossedfort
  • stevekinney