loading...
Cover image for Fluct - making nodejs file storing; friendly

Fluct - making nodejs file storing; friendly

crimsonmed profile image Médéric Burlet ・3 min read

Code: https://github.com/argus-inc/fluct

Package: https://github.com/argus-inc/fluct/packages/279681

While working on dust-cli and some other projects I realized many times I want to save temporary files / user configuration files. When searching on the web for how to save user preferences in a nodejs the first results are about saving app config in .env.

What I was really looking at was how to save user preferences. on the machine for a cli application in a simple easy to use way.

How Fluct solves the problem

I built this package to make handling files easier.

You can simply add the package in your app initialization like this:

import {FileHandler} from '@argus-inc/fluct';
const fs = new FileHandler();

This will create a .fluct file in the home directory. this can then be used to save files and data easily.

You can specify the name of the folder when creating the class:

const fs = new FileHandler(".dev");

With fluct you can then leverage the createPath function which has a few parameters making sure the path geenrate is inside our currently defined temp directory

For instance:

// returns `~/.fluct/mypath/myFile.md`
fs.createPath([`mypath`, `myFile.md`], true)

Another two useful functions are the save and read. The problem I encountered when using the native: fs.writeFileSync() and fs.readFileSync() was that they need extra configuration such as encoding and more.

The save function will let you easily save a javascript non circular object to a json file as follows:

const userSetting = {
    language: "en",
    name: "crimson"
}
const userPath = fs.createPath([`user`, `settings.json`], true)

fs.save(userPath, userSetting)
// This will save the userSetting object in json format at: `~/.fluct/user/settings.json`

The read function will return a string from a file this is made to easily load those config files for instance:

const userPath = fs.createPath([`user`, `settings.json`], true)

console.log(fs.read(userPath))

Available functions

Here is a quick list of available functions. More details can be found here:

@argus-inc/fluct

  • new FileHandler() - Creates a new instance of the class
  • doesTempDirExist() - Returns if the temp directory exists.
  • isTempDirWritable() - Returns if the temp directory is writable.
  • isDirWritable() - Returns if the given directory is writable.
  • createTempDir() - Creates the temp directory
  • createDir() - Creates a directory at the given path in sync
  • touch() - Creates an empty file at the given path
  • delete() - Deletes a file at the given path
  • read() - Reads the content of a file in sync
  • save() - Saves content to a file at a given path in sync
  • exists() - Checks if the given path exists in sync
  • createPath - Builds proper paths

epilogue

Fluct is not a package that is there to replace the basic nodejs fs. It is a wrapper around it that makes it easier for developers to save files with data from their app in an efficient manner.

The library will be updated with more funcitonalities in the future as it is an important part of current projects I am working on.

Author: Argus

Developer: Mederic Burlet

Licence: GPL-3-0-only

Posted on by:

crimsonmed profile

Médéric Burlet

@crimsonmed

『  』| Passionate Coder | Idea Hurricane

Discussion

pic
Editor guide