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

tree-hugger

v1.4.0

Published

Squeeze delicious metadata from a tree of JSON and YAML files.

Downloads

6

Readme

tree-hugger

🌳 Squeeze metadata from a tree of JSON, YAML, Markdown, and HTML files.

Installation

npm install tree-hugger or yarn add tree-hugger

Usage

Say you have a tree of data files:

├── a.json
├── b.yml
├── c.yaml
└── d
    ├── e.json
    └── f.json

tree-hugger scans the whole tree, collecting the data in those files as it goes. When it finishes scanning, it assembles all the data into a single JSON object and fires a data event:

const hug = require('tree-hugger')

hug(__dirname).on('data', (data) => {
  console.log(data)
})

Filenames and directories become keys in the data object, with filename extensions omitted:

a.json                  => data.a
b.yml                   => data.b
c.yaml                  => data.c
d/e.json                => data.d.e
d/f.json                => data.d.f

tree-hugger will emit the data event when it finishes scanning the tree, then it will continue watching the tree, emitting the data event any time a file is added, changed, or removed.

Data Files

The following file types are treated as data files:

Options

You can use all of the options supported by the chokidar file watcher.

Here's an example that ignores certain paths:

const options = {
  ignored: [
    '**/.git/**',
    '**/node_modules/**',
    '**/*.md',
    'ignore_me.yml',
    (filename) => filename.includes('.html')
  ]
}

hug(dataDir, options)
  .on('data', (data) => {
    console.log(data)
  })

chokidar's ignore option uses the fast and flexible anymatch library under the hood. See anymatch's usage docs for details about the nuances of ignoring with globs, regular expressions, functions, etc.

Frontmatter

In addition to JSON and YML files, tree-hugger also treats HTML and Markdown files as structured data. The Jekyll static site builder popularized the use of YML frontmatter as a way to add key-value data to an otherwise unstructured document, like a blog post:

---
title: "Project of the Week: WebTorrent"
author: zeke
permalink: /blog/webtorrent
---

Here is the actual content of the post...

When tree-hugger encounters a file like this, it parses it using the gray-matter parser.

Assuming the file above was named /posts/webtorrent.md, the following data structure would be generated:

{
  posts: {
    webtorrent: {
      data: {
        title: 'Project of the Week WebTorrent'
      },
      content: 'Here is the actual content of the post...'
    }
  }
}

The above parsing technique is applied to Markdown files and HTML files.

Files that do not contain frontmatter are still parsed, they just end up with an empty data object:

I am lonely markdown.

becomes

{
  lonely: {
    data: {},
    content: 'I am lonely markdown.'
  }
}

onFileData Middleware

You can specify a custom function to modify data files as they're added. This function accepts a data object and should return a modified data object.

const options = {
  onFileData: function (data) {
    // return object untouched
    if (!data.title) return data

    // remove all exclamation points!!!
    return Object.assign({}, data, {
      title: data.title.replace(/!/gm, '')
    })
  }
}

hug(dataDir, options)
  .on('data', (data) => {
    console.log(data)
  })

API

hug(dir, [options])

  • dir String (required) - the full path of the directory to watch
  • options Object (optional) - options to pass to the underlying chokidar file watcher.
    • onFileData Function (optional) - a custom function that can be used to modify datafiles. See onFileData middleware.

Tests

npm install
npm test

License

MIT