Skip to content
/ filer Public

Filer is a simple file reader/writer for node.

License

Notifications You must be signed in to change notification settings

ngoylufo/filer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Filer

Filer is a simple file reader/writer for node.

Installation

If you happened to have stumbled upon this and have some time to kill, installing filer isn't too complicated. Also I couldn't think of a different name.

npm install @ngoylufo/filer

Usage

Filer only has two use cases, reading and writing to files. If want to order pizza whilst on the toilet, you've come to the wrong place.

Require filer, preferably, at the top of your file

const filer = require('@ngoylufo/filer');

Now you can read and write files.

const package = filer.readFileSync({ filename: `${__dirname}/package.json` });
console.log(package); // <Buffer ...>

filer
  .readFile({
    filename: `${__dirname}/app/index.html`
  })
  .then(text => {
    console.log(text); // <Buffer ...>
  })
  .catch(err => {
    console.log('Something went wrong');
  });

// Returns a <Buffer >.
filer.readFileSync('sample.py');

[Optional] Register some formats for default behaviour

// How to deal with .json files.
filer.formats.register({
  extension: '.json',
  attributes: {
    reader: {
      options: { encoding: 'utf8' }
      coerce: buffer => JSON.parse(buffer.toString())
    },
    writer: {
      coerce: data => typeof data === 'string' ? data : JSON.stringify(data)
    }
  }
});

// Multiple formats following the same rules.
filer.formats.register({
  extension: ['.html', '.css', '.txt'],
  attributes: {
    reader: { options: { encoding: 'utf8' } },
    writer: { options: { encoding: 'utf8' } },
  }
});

const package = filer.readFileSync({ filename: `${__dirname}/package.json` });
console.log(package.name); // @ngoylufo/filer

filer.writeFile({
  filename: `${__dirname}/index.html`,
  data: '<!doctype html>'
}).then(() => {
  console.log('Wrote to file, now reading from file...');
  filer
    .readFile({ filename: `${__dirname}/index.html` })
    .then(data => {
      console.log(data); // <!doctype html>
    }).catch((contents) => {
      // contents === undefined not an error. At least not yet.
      console.log('Something went wrong!');
    });
});

// Unregistered format, returns a <Buffer ...>, if file exists.
filer.readFileSync('sample.py');

API

Coming soon...

License

This project is under the MIT License - see the LICENSE details.

About

Filer is a simple file reader/writer for node.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published