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

json-dir-tree

v1.0.5

Published

Getting directory structures and traversing them in JSON

Downloads

17

Readme

Directory Structure JSON

This module exposes functions with which you can:

  • Get a JSON tree/structure of a folder (including subdirectories and files), note that you have specify the filesystem to use yourself. You can get JSON tree for a directory with n levels of children.

  • Traverse a structure, giving callbacks to execute when a file or folder is found.

  • It gives you all the details of the file and folder including date created, size in actual units, file extension and local path.

  • You can use to it get only size of file you are uploading: Size Units: ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

Installation

npm install --save json-dir-tree

Example of a directory structure output

 [
    {
        "type": "file",
        "name": "index.js",
        "size": "2KB",
        "created_at": "Wed Nov 28 2018 15:03:56 GMT+0530 (IST)",
        "local_path": "/Users/test/json-dir-tree/",
        "file_type": "js"
    },
    {
        "name": "node_modules",
        "type": "folder",
        "children_count": "1",
        "created_at": "Wed Nov 28 2018 15:03:56 GMT+0530 (IST)",
        "children": [
            {
                "name": "path",
                "type": "folder",
                "created_at": "Wed Nov 28 2018 15:03:56 GMT+0530 (IST)",
                "children": [
                    {
                        "type": "file",
                        "name": "path.js",
                        "size": "43KB",
                        "created_at": "Wed Nov 28 2018 15:03:56 GMT+0530 (IST)",
                        "local_path": "/Users/test/json-dir-tree/",
                        "file_type": "js"
                    },
                    {
                        "type": "file",
                        "name": "package.json",
                        "size": "113KB",
                        "created_at": "Wed Nov 28 2018 15:03:56 GMT+0530 (IST)",
                        "local_path": "/Users/test/json-dir-tree/",
                        "file_type": "json"
                    },
                    {
                        "type": "file",
                        "name": "README.md",
                        "size": "1KB",
                        "created_at": "Wed Nov 28 2018 15:03:56 GMT+0530 (IST)",
                        "local_path": "/Users/test/json-dir-tree/",
                        "file_type": "md"
                    }
                ]
            }
        ]
    }
]

Get directory structure

var JsonDirTree = require('json-dir-tree');
var basepath = 'path/to/some/folder';
var fs = require('fs'); // you can select any filesystem as long as it implements the same functions that native fs uses.

JsonDirTree.getStructure(fs, basepath, function (err, structure, total) {
    if (err) console.log(err);

    console.log('there are a total of: ', total.folders, ' folders and ', total.files, ' files');
    console.log('the structure looks like: ', JSON.stringify(structure, null, 4));
});

Traverse structure

The structure retrieved from the example above can be traversed.

var JsonDirTree = require('json-dir-tree');
var basepath = 'path/to/some/folder'; // this will be prepended to the paths found in the structure

JsonDirTree.traverseTree(structure, basepath,
function (folder, path) {
    console.log('folder found: ', folder.name, 'at path: ', path);
},
function (file, path) {
    console.log('file found: ', file.name, 'at path: ', path);
});

Get file size

var JsonDirTree = require('json-dir-tree');
var basepath = 'path/to/some/file'; // this will be prepended to the paths found in the structure
var fileSize = JsonDirTree.getFileSize(basepath);

License MIT

  • MIT License

Copyright (c) 2018 anubhav04

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.