readfile-go
"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:
var readFileGo = var options = {}var contents =
Options object
Available options are:
encoding | flag | logFunc | message | silent | throw | save | saveMessage
Default options are:
var options = encoding: 'utf8' // See fs.readFileSync encoding option, set to null for a buffer flag: 'r' // See fs.readFileSync flag option logFunc: consolewarn // 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)
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
var readFileGo = var log = var options = logFunc: loginfovar contents =
save (boolean)
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.
var readFileGo = var contents1 = var contents2 =
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)
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:
var readFileGo = var contents1 = var contents2 =
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 :)