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

@allmarkedup/dirscribe

v1.0.0

Published

Describe a recursive directory structure as a nested JSON object

Downloads

14

Readme

Dirscribe

Describe a recursive directory structure as a nested JSON object.

Supports filtering, post-processing and the ability to supply a custom callback to determine the structure of the JSON objects.

Installation

$ npm install @allmarkedup/dirscribe --save

Usage

The dirscribe module exports a single function with the following signature:

dirscribe(<path-to-directory> [,opts]);

And returns a promise. Internally, dirscribe uses Bluebird for promise creation.

Basic example

const dirscribe = require('@allmarkedup/dirscribe');

const fileTree = dirscribe('test/fixtures/root-directory'); // returns a Promise

fileTree.then(tree => console.dir(tree));

// Outputs:
// { root: '',
//   dir: 'test/fixtures',
//   base: 'root-directory',
//   ext: '',
//   name: 'root-directory',
//   path: 'test/fixtures/root-directory',
//   stat:
//    { dev: 16777220,
//      mode: 16877,
//      nlink: 5,
//      uid: 501,
//      gid: 20,
//      ... },
//   children:
//    [ { root: '',
//        dir: 'test/fixtures/root-directory',
//        base: 'file-1.md',
//        ext: '.md',
//        name: 'file-1',
//        path: 'test/fixtures/root-directory/file-1.md',
//        stat: [Object]
//      },
//      { root: '',
//        dir: 'test/fixtures/root-directory',
//        base: 'sub-directory-1',
//        ext: '',
//        name: 'sub-directory-1',
//        path: 'test/fixtures/root-directory/sub-directory-1',
//        stat: [Object],
//        children: [Object]},
//      { root: '',
//        dir: 'test/fixtures/root-directory',
//        base: 'sub-directory-2',
//        ext: '',
//        name: 'sub-directory-2',
//        path: 'test/fixtures/root-directory/sub-directory-2',
//        stat: [Object],
//        children: [Object] } ] }

Example with options

const dirscribe = require('@allmarkedup/dirscribe');

const fileTree = dirscribe('test/fixtures/root-directory', {
    build: (filePath, stat) => ({
        path:     filePath,
        modified: new Date(stat.mtime).getTime(),
        type:     stat.isDirectory() ? 'dir' : 'file'
    }),
    after: items => items.sort((a, b) =>  b.modified - a.modified )
});

fileTree.then(tree => console.dir(tree));

// Outputs:
// { path: 'test/fixtures/root-directory',
//   modified: 1453901431000,
//   type: 'dir',
//   children:
//    [ { path: 'test/fixtures/root-directory/sub-directory-2',
//        modified: 1453901475000,
//        type: 'dir',
//        children: [Object] },
//      { path: 'test/fixtures/root-directory/sub-directory-1',
//        modified: 1453901444000,
//        type: 'dir',
//        children: [Object] },
//      { path: 'test/fixtures/root-directory/file-1.md',
//        modified: 1453899612000,
//        type: 'file' } ] }

Options

opts.filter

A callback filter function that is called with the filePath as it's only argument. This is called before the build function.

const opts = {
    filter: filePath => ! (/(^|\/)\.[^\/\.]/g).test(filePath), // ignore hidden files
};

opts.build

A function that gets called for each item and must return a JSON representation of that item. Function is called with the file path and the stat object for that file.

This can be used to customise the properties you want on each of the JSON objects representing a file/directory.

Note that for directories, children are appended to the returned object after the build function has been called.

const opts = {
    build: (filePath, stat) => ({
        path:     filePath,
        modified: new Date(stat.mtime).getTime(),
        type:     stat.isDirectory() ? 'dir' : 'file'
    })
};

The default build function looks as follows:

function build(filePath, stat) {
    const p = Path.parse(filePath);
    p.path = filePath;
    p.stat = stat;
    return p;
}

opts.after

A callback function to be run after each directories items have been converted into JSON objects. Receives an array of JSON objects representing the contents of that directory. The function should return the final list of objects.

This is a good place to do any sorting or post-filtering of items.

const opts = {
    after: items => items.slice(0, 3), // limit results to three per directory
};

opts.recursive

Whether to recurse down through directories or not. Defaults to true.

const opts = {
    recursive: false, // only runs through the top level directory
};

opts.childrenKey

The key to use for the array of children that gets appended to objects representing directories.

const opts = {
    childrenKey: 'kids',
};

// {
//     path: 'path/to/directory'
//     kids: [
//         {
//             path: 'path/to/directory/foo.txt'
//             ...
//         }
//     ]
// }

Tests

Use npm test to run tests.