npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fs-handy-wraps

v4.2.0

Published

Handy wraps for Node.js FS functions

Downloads

18

Readme

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 text or 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 -rf does. Resolves to a true value if succeeded.

append (path[, text, successCallback, errCallback])

Appends text or 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 make is not specified new file will be empty. If typeof make === string it will be the content of the new file. If typeof make === function it will be a callback with arguments: (resolve, reject). This callback should call resolve with a content for the new file. See examples below.

dir (path[, successCallback, errCallback])

Creates a directory specified by path. This function is imported from fs-extra. Here is its documentation.


Read or Create a JSON config-file using simple CLI

getConfig (path[, defProvider, CLIQuestions, successCallback, errCallback])

Reads the path file, checks if for JSON errors and calls successCallback (parsedConfig). If the file does not exist -- creates it according to default content provided by defProvider. defProvider may be: an object or a function that returns an object or a promise. If CLIQuestions specified, a simple CLI will be started. Example for CLIQuestions object:

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 callback every time a file specified by path is 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));
    }
})()