Giter Club home page Giter Club 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));

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;

file-bin's People

Contributors

acareaga avatar adamki avatar jasonpilz avatar jillmd501 avatar matt-stj avatar mattrooney avatar rossedfort avatar stevekinney avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

file-bin's Issues

Add getters and setters for base directory

As of 0.2.0, you can just change the .base property. It would be cool to put in a getter and setter method that would allow us to add additional hooks later for things (e.g. fire an event when the base directory was changed).

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

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

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

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

Add rename method

As of right 0.2.0, there is no way to rename a file. Ideally, it would look something like this.

fileBin.rename('old-name.md', 'new-name.md')
  .then((f, old, new) => console.log(`${old} was successfully renamed to ${new}.')
  .catch(e => console.log(`Something bad happened: ${e.message}`);

Add delete or destroy method

As of right 0.2.0, there is no way to delete a file. Ideally, it would look something like this.

fileBin.delete('filename.md')
  .then(f => console.log(`${f} was successfully destroyed.')
  .catch(e => console.log(`Something bad happened: ${e.message}`);

Use native promises if possible

Check to see if the browser or Node environment already supports Promises and—if so—don't require RSVP and just use the built-in support instead.

Add copy method

As of right 0.2.0, there is no way to copy a file. Ideally, it would look something like this:

fileBin.copy('source-name.md', 'copy-name.md')
  .then((copy, source) => console.log(`${source.id} was successfully copied to ${copy.id}.`)
  .catch(e => console.log(`Something bad happened: ${e.message}`);

Base directory must exist

FileBin should throw an error if it's instantiated with a valid base directory.

This should also be the case when using setBaseDirectory in #3.

Add creation time and modified time

As of 0.2.0, File Bin only returns the title and content of the file. We need it to have the creation time and time of last modification as well.

Here is an example:

fileBin.find('hello.md').then(file => console.log(file));

/*
Output:

{
  id: 'hello.md',
  content: 'lorem ipsum…',
  createdAt: "Mon Feb 22 2016 08:27:19 GMT-0700 (MST)"
  lastModified: "Mon Feb 22 2016 08:27:19 GMT-0700 (MST)"
*/

I'm thinking these should be Date objects.

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.