fs-handy-wraps v4.2.0
Handy Wraps for Node.js FS
A pretty simple library. It uses fs-extra under the hood.
Installation
npm i -S fs-handy-wraps or yarn add fs-handy-wraps
Constants
HOME is a path to the Home directory of the current OS user.
CWD is a path to the Current Working Directory.
fse is an object that exposes fs-extra functions as is.
Functions for Reading and Writing files
All the functions are promisified. Only first argument is required.
check (path, existCallback, absentCallback)
Checks the file existence. All arguments are required.
read (path, successCallback, errCallback)
Reads the file contents.
write (path, text, successCallback, errCallback)
Rewrites the file content by
textor an empty string. Also may be used for a new file creation. If the target folder does not exist, it will be created.
rm (path)
Removes specified file of folder as it
rm -rfdoes. Resolves to atruevalue if succeeded.
append (path, text, successCallback, errCallback)
Appends
textor an empty string to the end of the file.
rom (path, make, readCallback)
Reads the file if it exists and calls readCallback then. Creates a new file if it does not exist. If argument
makeis not specified new file will be empty. Iftypeof make === stringit will be the content of the new file. Iftypeof make === functionit will be a callback with arguments:(resolve, reject). This callback should callresolvewith a content for the new file. See examples below.
dir (path, successCallback, errCallback)
Creates a directory specified by
path. This function is imported fromfs-extra. Here is its documentation.
Read or Create a JSON config-file using simple CLI
getConfig (path, defProvider, CLIQuestions, successCallback, errCallback)
Reads the
pathfile, checks if for JSON errors and callssuccessCallback (parsedConfig). If the file does not exist -- creates it according to default content provided bydefProvider.defProvidermay be: an object or a function that returns an object or a promise. IfCLIQuestionsspecified, a simple CLI will be started. Example forCLIQuestionsobject:
const CLIQuestions_EXAMPLE = [
{ prop: 'pathToBase', question: 'Full path to database file:' },
{ prop: 'pathToNotefile', question: 'Path to temp file:' },
{ prop: 'editor', question: 'Command to open your text editor:' },
];It asks CLIQuestions to user, then assigns received values to a default object.
A callback successCallback (config) will be executed in the result.
Watching on file changes
Only for for testing purposes. In production, use chokidar instead.
This function cannot be promisified.
watch (path, callback)
Creates a Watcher that will call the
callbackevery time a file specified bypathis changed. There are a 30ms delay between the system event and the callback is called.
Usage example (with callbacks)
const FILE = require('fs-handy-wraps');
const configPath = '~/config.json';
const configDefaults = {
base: '~/base.txt',
name: 'My new Project'
};
const cli = [
{ prop: 'base', question: 'Where to store the database?' },
{ prop: 'name', question: 'What is the name of your Project?' },
];
start();
function start() {
FILE.getConfig (configPath, configDefaults, cli, checkBase);
}
function checkBase (config) {
FILE.rom (config.base, createNewBase, parseBase);
}
function parseBase (baseContent) {
// do something with baseContent...
}
function createNewBase (resolve, reject) {
// do something to get the new base content...
resolve(content);
}Usage with promises (async / await syntax)
const FILE = require('fs-handy-wraps');
const pathDefaultFile = 'fileDef.txt';
const pathFile = 'file.txt';
// if pathFile is already exists --> read it
// else --> create a new file based on the another one.
(async function start() {
const content = await FILE.rom(pathFile, makeDefault);
console.log(content);
async function makeDefault(resolve, reject) {
resolve(await FILE.read(pathDefaultFile));
}
})()