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

@lazylife.ninja/directory-as-data

v0.1.2

Published

Take a directory of files and turn it into a collection of data.

Readme

Directory As Data

NOTE: This is experimental software.

Take a directory of files and turn it into a collection of data.

Getting Started

Given a directory structure as follows:

~/example-directory
 ├── bar.json
 ├── foo-and-then-some.json
 ├─┬ baz-and-stuff\
 │ ├── baz-and-bar.json
 │ └── baz-and-foo.json
 ├─┬ dynamic\
 │ ├── dynamic-js.js
 │ └── dynamic-ts.ts
 └─┬ posts\
   └── hello-world.md

We may build a collection with the following example setup:

// index.ts
import directoryAsData from "@lazylife.ninja/directory-as-data"
import MdToJs from "./utils/md-to-js"


const dataPromise = directoryAsData({
    cwd: __dirname, // optional, default: process.cwd()
    src: "./example-directory",
    resolvers: [
      {
        // Read and parse JSON files
        test: /\.json$/,
        // `file` is a VFile (<https://github.com/vfile/vfile) with empty `contents`
        // options.read is a promisified version of toVFile.read (https://github.com/vfile/to-vfile#tovfilereadoptions-encoding-callback)
        async resolve(file, { read }) {
          await read(file)
          const data = JSON.parse(file.toString())

          return data
        }
      },
      {
        // Dynamically import JS and TS files
        // In this example scripts should export a function that returns a JSON object
        // wrapped in a Promise
        test: /\.[jt]s$/,
        async resolve(file) {
          const { default: run } = await import(file.path)
          const data = await run()

          return data
        }
      },
      {
        // Read and parse markdown files with front matter
        // Create a custom util for liberal choice of markdown engine
        test: /\.md$/,
        async resolve(file, { read }) {
          await read(file)
          const markdown = file.toString()
          const data = MdToJs(markdown)

          return data
        }
      }
    ]

The resulting data looks something like this:

Object {

  /* All files with their resolved data are kept at `result._.files` */

  "_": Object {
    "files": Array [
      VFile {
        "cwd": "~/",
        "data": Object {
          "title": "bar",
        },
        "history": Array [
          "~/bar.json",
        ],
        "messages": Array [],
      },
      VFile {
        "cwd": "~/",
        "data": Object {
          "title": "foo",
        },
        "history": Array [
          "~/foo-and-then-some.json",
        ],
        "messages": Array [],
      },

      /* ... */

    ],
  },

  /* files at the root of the source directory create an entry with the camelcase filename as property name */

  "bar": Object {
    "title": "bar",
  },
  "fooAndThenSome": Object {
    "title": "foo",
  },

  /* first level sub-directories create collection entries */

  "collections": Object {
    "bazPlusMore": Array [
      Object {
        "title": "baz bar",
      },
      Object {
        "title": "baz foo",
      },
    ],
    "dynamic": Array [
      Object {
        "title": "Dynamic Typescipt",
      },
      Object {
        "title": "Dynamic",
      },
    ],
    "posts": Array [
      Object {
        "html": "<p>This is just to say: \\"Hello\\".</p>
",
        "title": "Hello World",
      },
    ],
  }
}

The full example can be found at ./test.

Run the example by cloning this repository, installing dependencies and executing npm test.

API

For lack of documentation please have a look at the Typescript declaration file ./index.d.ts.

Also check out the source located at ./src/index.ts.