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

node-filesystem-utils

v1.0.2

Published

Node extended file system utilities

Downloads

2

Readme

Node FileSystem Utils

Installation

npm install node-filesystem-utils

Why?

When using the native Node filesystem API you interact with a single file at any given time, which I find limiting. This module supports Java-style method overloading making it possible to pass a wider array of arguments.

For this module you can pass a single file or an Array of files making it possible to read or write one or many files with just one method call.

Features:

  • Read / write one or multiple files with a single method call
  • Automatically create non existing folders when saving files
  • Append data to existing JSON files
  • Get a list of all files in a directory (non recursive)
  • Count the number of files in a directory (non recursive)
  • Get File Objects from a file containing the file's mimetype, name, dir, path and extension

Examples

Save multiple files

var fileUtils = require('node-filesystem-utils');

var manifest = [
    {
        path: '/Users/christianrich/test',
        filename: 'file1.txt',
        data: 'this is same text for the 1st file'
    },

    {
        path: '/Users/christianrich/test',
        filename: 'file1.txt',
        data: 'this is same text for the 2nd file'
    },

    {
        path: '/Users/christianrich/test',
        filename: 'file3.txt',
        data: 'this is same text for the 3rd file'
    }
];

fileUtil.writeFiles(manifest, function(err, files){
    if(err) throw err;
    console.log(files);
});

Read multiple files

var fileUtils = require('node-filesystem-utils');

var manifest = [
    '/User/christianrich/test/1/myFile1.txt',
    '/User/christianrich/test/1/myFile2.txt',
    '/User/christianrich/test/1/myFile3.txt'
];

fileUtil.readFiles(manifest, 'utf8', function(err, files){
    if(err) throw err;
    console.log(files);
}

The returned files array has the same order as the manifest.

Get the directory part of a path

var fileUtils = require('node-filesystem-utils');

fileUtils.getDirectory('/Users/christianrich/test/someFile.txt'); // /Users/christianrich/test/
fileUtils.getDirectory('/Users/christianrich/test/'); // /Users/christianrich/test/

Is a path a directory or a file?

var fileUtils = require('node-filesystem-utils');

fileUtils.isDirectory('/Users/christianrich/'); // true
fileUtils.isDirectory('/Users/christianrich/test/someFile.txt'); // false

Copy a file or directory not caring if the destination path exists

var fileUtils = require('node-filesystem-utils');

fileUtils.copy('/Users/christianrich/folder1/', '/Users/christianrich/may-or-may-not-exist/', function(err, dest){
    if(err) throw err;
});

Get a file descriptor object

var fileUtils = require('node-filesystem-utils');
var fo = fileUtils.getFileObject('/Users/christianrich/myFile.txt');

Yields:

{
    mimetype: 'text/plain',
    name: 'myFile.txt',
    path: '/Users/boom/myFile.txt',
    dir: '/Users/boom/',
    extension: 'txt'
}

Append JSON to existing files

Assume we have the file 'example.json' saved on disc:

{
    "myData": []
}

Let's manipulate the file and append an element to the 'myData' array. First create a modifer function. This makes it possible to interact with the JSON structure in any which way you like hence decoupling the data modification from the file IO operation:

var modifier = function(jsonObj, appendData, cb){
    jsonObj.data.push(appendData);
    cb(json);
};

Load the file passing in the modifier:

fileUtil.appendJson('/Users/christianrich/example.json', 'hello', modifier, function(err, file){
    if(err) throw err;
});

Now the file on disc looks like this:

{
    "myData": ['hello']
}

Test

mocha ./test

Limitations

Only tested on Mac and Linux file systems. Will probably not work on Windows OS.

File and folder permissions

When accessing files and folders the logged in user must have permissions to access these. E.g on Amazon AWS EC2 Linux instances, if the default ec2-user tries to access folders above the /home directory you'll get EACCESS errors.

My blog

http://chrisrich.io

License

MIT