readfile-go

I just want to read my file and go

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
readfile-go
1.0.75 years ago7 years agoMinified + gzip package size for readfile-go in KB

Readme

readfile-go

Linux Build Windows Build Coverage Status
npm Dependencies Status npm node
"I just want to read my file and go"

Introduction

readfile-go is a zero-dependency module that wraps fs.readFileSync
readfile-go focuses on file not found behaviour. Default behaviour is to console.warn when you get ENOENT: no such file or directory instead of throwing an error but options give you full control.
readfile-go removes the need for code scaffolding. One liners guaranteed.

Usage

npm install --save readfile-go
readfile-go takes 2 arguments, the path to the file to read and optionally an options object
readfile-go returns the file contents or undefined on file not found.
An example to demonstrate usage:
```javascript 1.5 var readFileGo = require('readfile-go') var options = {} var contents = readFileGo(dirname + '/path/to/my/file.md', options)
<br>

## Options object

Available options are:

encoding | flag | [logFunc](#logfunc-function-) | message | silent | throw | [save](#save-boolean-) | [saveMessage](#savemessage-boolean-)

Default options are:

```javascript 1.5
var options = {
  encoding: 'utf8',       // See fs.readFileSync encoding option, set to null for a buffer
  flag: 'r',              // See fs.readFileSync flag option
  logFunc: console.warn,  // Logger function to be called with the file not found message
  message: null,          // Message to be logged in place of - ENOENT: no such file or directory
  silent: false,          // Turn off logging?
  throw: false,           // Restore the normal file not found throw behaviour?
  save: false,            // Remember the -- encoding, flag, logFunc, silent, throw -- options for next time?
  saveMessage: false      // Remember the -- message -- option for next time?
}

Most options are self explanatory. Further explanation, where required, is given below.

logFunc (function) top

Logger function to be called with the file not found message.
Defaults to console.warn but you can set logFunc to use your preferred logger function.
To illustrate, if you are using loglevel
you could set log.info as your logFunc
```javascript 1.5 var readFileGo = require('readfile-go') var log = require('loglevel') var options = {logFunc: log.info} var contents = readFileGo(dirname + '/path/to/my/file.md', options)
<br>

#### **save** (boolean) [![top](top.png)](#options-object)

Its annoying to keep passing the same options every time.

readfile-go, if instructed to do so, will remember previously used options for you.

When the `{save: true}` option is used in a call to readfile-go subsequent calls remember the -- encoding, flag, logFunc, silent, throw -- options.

```javascript 1.5
var readFileGo = require('readfile-go')
var contents1 = readFileGo(__dirname + '/path/to/my/file1.md', {silent: true, save: true})
var contents2 = readFileGo(__dirname + '/path/to/my/file2.md')

Here, the second call remembers that the silent option is on. {silent: true} is now the default.
Therefore, even if neither file exists, nothing will be logged for either call.

saveMessage (boolean) top

Similar to the save
option but subsequent calls only remember the -- message -- option.
Thereby allowing you to save a generic custom file not found message:
```javascript 1.5 var readFileGo = require('readfile-go') var contents1 = readFileGo(dirname + '/path/to/my/file1.md', {message: 'oopsie', saveMessage: true}) var contents2 = readFileGo(dirname + '/path/to/my/file2.md') ```
After saving a message, if you wish to later restore the normal file not found message ENOENT: no such file or directory simply set {message: null, saveMessage: true}

Author says

I would like to share these words of Jesus, who is precious to me, with you:
The thief comes only to steal and kill and destroy. I came that they may have life and have it abundantly. John 10:10 ESV

Jesus is a friend and not an enemy to all those who would welcome Him :)