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

traversdir

v1.0.0

Published

recursively walks the folder/file structure, starting with a given path, and returns the information as an object.

Downloads

6

Readme

##Traversdir

Installation

First, install it from npm:

npm install traversdir

Then, require it as a node.js module:

const traversdir = require('traversdir');

###Usage

Provide an absolute path, of the directory that you want to scan, to the traverse() function. It will return an object, that has the following properties, for each directory node:

  • _path - a path relative to the starting point of traversal
  • _files - an array containing names of all the files in the given folder
  • _dirs - an array containing names of all the directories in the given folder
  • it will also have properties named exactly same, as the names of all the directories. Each of those properties will be the next traversal step (an object) with their own _path,_files,dirs.

//create a string with an absolute path to the dir you want to work on
var test_path = path.join(path.dirname(__dirname),"your_path");

//traverse all the files and directories
//starting with the given path
//and return an object with a result

var dir_object = traversdir(test_path);

console.log( dir_object );

###Example

Imagine that you have a following file/directory structure:

folder
+── app.js
+── bg.jpg
+── first
│   +── a
│   +── component2.js
│   +── component.js
+── index.html
+── second
│   +── a
│   +── b
│   │   +── b.jade
│   +── menu1.jade
│   +── menu2.jade
│   +── navbar.jade
+── third
    +── 01.jpg
    +── 02.jpg
    +── 03.jpg

You can provide the path of the 'folder' to the traversdir() function, like so:

var your_path = path.join(path.dirname(__dirname), "folder");
var folder = traversdir(your_path);
console.log( folder );

And it will return the following object:

{
    _path: '/',
    _files: ['app.js', 'bg.jpg', 'index.html'],
    _dirs: ['first', 'second', 'third'],
    first: {
        _path: '/first',
        _files: ['component.js', 'component2.js'],
        _dirs: ['a'],
        a: {
            _path: '/first/a',
            _files: [],
            _dirs: []
        }
    },
    second: {
        _path: '/second',
        _files: ['menu1.jade', 'menu2.jade', 'navbar.jade'],
        _dirs: ['a', 'b'],
        a: {
            _path: '/second/a',
            _files: [],
            _dirs: []
        },
        b: {
            _path: '/second/b',
            _files: 'b.jade',
            _dirs: []
        }
    },
    third: {
        _path: '/third',
        _files: ['01.jpg', '02.jpg', '03.jpg'],
        _dirs: []
    }
}

Now you can access arrays of names of the files and directories on any node, as well as the relative paths to it, like so:

//for the ./folder/second path
console.log(folder.second._files)  // ['menu1.jade', 'menu2.jade', 'navbar.jade']
console.log(folder.second._dirs)   // ['a', 'b']
console.log(folder.second._path)   // '/second'