smart-fs
Abstraction Layer for File Management.
Getting Started
$ npm install --save smart-fs
Functions
To ignore file extension and force treat the file as a certain type, you can pass the option treatAs
as e.g. json
.
smartRead(filepath, options = { treatAs = null })
Read and parse file based on file extension.
The following extensions are handled in order:
.json
: Loads file using JSON.parse..yml
and .yaml
: Loads file using yaml-boost..js
: Loads file using require, but invalidating the cache before doing so..*
: Treats file as text file and loads as array of lines.
smartWrite(filepath. content, options = { treatAs = null, mergeStrategy = (existing, changeset) => changeset })
Serialize and write content to file based on file extension.
The file and all necessary folders are created if not present.
The file is only actually written if the content has changed.
Returns true if the file was written, false otherwise.
The following extension are handled in order:
.json
: Serialize uses JSON.stringify..yml
and .yaml
: Serialize uses yaml-boost..*
: Expects content as array and serializes by joining array using new line character.
The mergeStrategy
option can be used to customize how the new content is merged if the target file already exists.
By default the file is simply overwritten.
Important
Do not use this library for loading if you don't trust the source of the files you are loading!