fb

file-bin

An abstraction for accessing the file system in Node.

Showing:

Popularity

Downloads/wk

11

GitHub Stars

0

Maintenance

Last Commit

5yrs ago

Contributors

5

Package

Dependencies

2

Size (min+gzip)

9.4KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

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;

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100