Giter VIP home page Giter VIP logo

file-bin's Introduction

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));

file-bin's People

Contributors

stevekinney avatar

Watchers

James Cloos avatar Robbie Lane avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.