New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@jf/fs

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jf/fs

Class for manipulating file system in a synchronous way with useful methods.

latest
Source
npmnpm
Version
4.0.0
Version published
Weekly downloads
3
-57.14%
Maintainers
1
Weekly downloads
 
Created
Source

@jf/fs stable

Class for manipulating file system in a synchronous way using NodeJS.

You can create/remove directories recursively and read/write files too.

I use it mainly as base for file generators.

Usage

npm install @jf/fs

Example

Generator class

const jfFileSystem = require('@jf/fs');
const path         = require('path');
const tr           = require('./translations');

class Generator extends jfFileSystem {
    constructor(directory)
    {
        this.directory = directory;
        // Clean directory recursively before generating files.
        this.rmdir(directory);
    }
    
    generate(data)
    {
        // If outfile has several levels of depth, no problem.
        // `write` method will create all required directories.
        data.forEach(
            config => this.write(
                path.join(
                    this.directory,
                    config.outfile
                ),
                this.parse(config.data)
            )
        );
    }
    
    log(level, name, label, ...args)
    {
        // Translating logs.
        // tr is a map with translations.
        super.log(level, name, tr[label], ...args);
    }
    
    parse(data)
    {
        // In your class, process data and convert it to string.
        return JSON.stringify(data);
    }
}

Translating texts

All texts are in spanish but if you want to translate them you can overwrite log method in child class (as in the previous example) or to listen log event.

const chalk        = require('chalk');
const siNumber     = require('si-number');
const fs           = require('@jf/fs').i();
fs.on(
    'log',
    data =>
    {
        // Show only errors
        if (data.level === 'error')
        {
            const _args = data.args;
            if (Array.isArray(_args))
            {
                _args.forEach(
                    (arg, index) =>
                    {
                        if (typeof arg === 'number')
                        {
                            // Format number in green using SI prefixes.
                            _args[index] = chalk.green(
                                siNumber(
                                    arg,
                                    {
                                        decimal   : ',',
                                        precision : 1,
                                        thousands : true
                                    }
                                )
                            );
                        }
                        else
                        {
                            // Texts in cyan.
                            _args[index] = chalk.cyan(arg);
                        }
                    }
                )
            }
        }
        else
        {
            delete data.label;
        }
    }
);
fs.log('info', '', 'Test %s', 'pl1'); // Omitted because is not an error.
fs.log('error', '', 'File %s already exists', '/tmp/exists.js'); // Filename in cyan
fs.log('error', '', 'Filesize %s', 1324); // Number formatted in green as 1,3k

Keywords

copy

FAQs

Package last updated on 24 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts