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 🙏

© 2026 – Pkg Stats / Ryan Hefner

grunt-tree

v1.1.1

Published

Parse a directory to a tree with json format.

Readme

grunt-tree v1.0.1

Parse a directory to a tree with json format.

Why I writen this plugin?

I need build my static files with md5 version. The follows is what I want to get:

// static.json
{
    "static/js/base.js": "xxxx-base.js",
    "static/img/logo.png": "xxxx-logo.png",
    "static/css/base.css": "xxxx-base.css"
}

Then, I do not need care about the cache of static files in production environment by create files with md5 version.

Only I need to do is create a get static files function to get it.

Getting Started

This plugin requires Grunt >=0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-tree --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-tree');

The "tree" task

Overview

In your project's Gruntfile, add a section named tree to the data object passed into grunt.initConfig().

grunt.initConfig({
  tree: {
    options: {
        // hash: String, default: null, like 'md5' or 'sha1'
        // hashLen: Number, default: false
        // format: Boolean, default: false
        // type: Array, default: []
        // recurse: Boolean, default: true
        // cwd: String, default: ""
        // prettify: Boolean|number, default: 0
    },
    your_target: {
      /** contents like this:
        files: [
            {
                src: ['relativePath/'],
                dest: 'saveTheResult/'
            }
        ]
      */
    },
  },
})

Options

options.hash

Type: String Default value: null.

Get the hash value of the file and insert before the file name with '-'. Value is the algorithm name, like md5,sha1.

options.hashLen

Type: Number Default value: false

Get the substring of hash value of file. hashValue.substring(0, options.hashLen)

options.format

Type: Boolean Default value: false

A boolean value that you want the format of result.

The Default result is the tree format like the command tree.

And if format set to true, then output a one-to-one mode.

options.type

Type: Array Default value: false

Filter the postfix of the files you set. This is can be replaced by set src option with pattern.

options.recurse

Type: Boolean Default value: true

Whether recurse in your given directory.

options.cwd

Type: String Default Value: ''

Relative to the src directory.

options.prettify

Type: Boolean|Number Default value: 0.

For output style, if set to true, It is equivalent to JSON.stringify(json, null, 4).

Anyway, see the examples.

options.outputType

Type: Array Default value: false Add in version 1.1.1

Filter the postfix of the files you set to output.

options.outputDirectory

Type: String Default value: false Add in version 1.1.1

The directory of files output.

Caution

This task is not support Building the files object dynamically in configuring-tasks of grunt.

Go to Building the files object dynamically, see more infomation.

Usage Examples

Default Options

grunt.initConfig({
    tree: {
        default: {
            options: {},
            files: [
                {
                    src: ['test/'],
                    dest: '/tmp/test.json'
                }
            ]
        }
    }
});

// If the have files: "a.css", "js/b.js", "c" in the test directory,
// 1. run grunt, then the result will like:
{
    "a.css": "a.css",
    "js": {
        "b.js": "b.js"
    },
    "c": "c"
}

// 2. change the options to: { hash: 'md5', hashLen : 8 }, and result will be like:
{
    "a.css": "e8dcfa25-a.css",
    "js": {
        "b.js": "59efd297-b.js"
    },
    "c": "a8e91771-c"
}

// 3. change the options to: { format: true }, and result will be like:

{
    "a.css": "a.css",
    "js/b.js": "b.js"
    "c": "c"
}

// 4. change the options to: { type: ["js", "css"] }, and result will be like:
{
    "a.css": "a.css",
    "js": {
        "b.js": "js/b.js"
    }
}

// 5. change the options to: { recurse: false }, and result will be like:
{
    "a.css": "a.css",
    "c": "c"
}

// 6. change the options to: { outputType: ['js'], outputDirectory: '/tmp' }, and result will be like:
// /tmp/output.json
{
    "a.css": "a.css",
    "js": {
        "b.js": "b.js"
    },
    "c": "c"
}
// New file created in /tmp directory
// /tmp/b.js


// 7. try to mix the options, and have a look.
...

### Test
```shell
npm run-script test

# Once you run the follow command in console, you should run `npm install` before.
# grunt test

Last

About the version. The first version is the same with grunt first version.

Release History

  1. New Start. [2016/02/27] => for 1.0.1
  2. Add option.outputType and option.outputDirectory. [2016/02/29] => for 1.1.1