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

make-vfs

v1.0.15

Published

Easily make a virtual filesystem from a directory

Downloads

21,025

Readme

make-vfs

Easily make a virtual filesystem from a directory.

  • Create a typescript module that imports all your filesystem routes
  • Create a javascript module that contains the content of all of your migrations

See some examples of what a vfs module looks like here

Usage

$ make-vfs --help
Options:
  --help            Show help                                          [boolean]
  --version         Show version number                                [boolean]
  --dir             The directory to load files from                    [string]
  --extensions      Valid extensions to write into vfs module           [string]
  --outfile         The output vfs module file e.g. vfs.ts or migrations.ts
                                                                        [string]
  --no-import-ext   Do not add the extension to the import path        [boolean]
  --content-format  The format to store files in the vfs module.
         [string] [choices: "buffer", "string", "import-star", "import-default",
                                                  "require"] [default: "buffer"]

Examples:
  make-vfs ./migrations --extensions sql    Put all your migrations into a
  --content-format=string ./migrations.ts   typescript module
  make-vfs ./routes --extensions ts,js      Put all your filesystem routes into
  ./routes.generated.ts                     a typescript module
  --content-format=import-default
  --no-import-ext

Statically Bundling Routes

A common use-case for make-vfs is to statically bundle all the routes in a directory so that a server can be packaged without needing to do runtime filesystem operations.

To do this, you can add an npm script build:routes that generates a static-routes.ts file that imports all your route files. The script can be defined as...

make-vfs --dir ./routes --content-format import-star --outfile static-routes.ts

It will generate something like:

import * as _api_dev_package_examples_create from "./../routes/api/dev_package_examples/create"
import * as _api_dev_package_examples_get from "./../routes/api/dev_package_examples/get"
import * as _api_health from "./../routes/api/health"

export default {
  "api/dev_package_examples/create": _api_dev_package_examples_create,
  "api/dev_package_examples/get": _api_dev_package_examples_get,
  "api/health": _api_health
}

You can now use your routes statically, e.g. here's a simple web server using bun using the statically generated routes:

// server.ts
import staticRoutes from "./static-routes"

Bun.serve({
  fetch(req) {
    const url = new URL(req.url);
    const path = url.pathname.slice(1); // Remove leading slash
    
    if (!(path in staticRoutes)) {
      return new Response("Not found", { status: 404 });
    }

    const route = staticRoutes[path]
    return route.default(req)
  },
  port: 3000,
})

[!NOTE] An example of a route file would be something like:

export default (req) => {
  return new Response("hello world!")
}

Statically Bundling Files

You can also use make-vfs to generate a Typescript file that contains buffers or string representations of files such as images, markdown etc.

make-vfs --dir ./assets --content-format buffer --outfile bundle.js

# If you're only bundling text files, use the "string" output format so you don't
# need to deal with buffers!
make-vfs --dir ./assets --content-format string --outfile bundle.js

The output format for these is just an object a buffer or string as values (depending on your selected --content-format)

export default {
  "assets/file.txt": "hello world!",
  "assets/some-other-file.md": "#Some Other File\nContent goes here"
}

You can now import and use this file!

Usage as Library

import {
  getVirtualFilesystemModuleFromDirPath,
  getVirtualFileSystemFromDirPath,
  getMatchingFilePaths,
} from "./index"

const moduleString = await getVirtualFilesystemModuleFromDirPath({
  dir: "./migrations",
  extensions: ["sql"],
  outfile: "./migrations.ts",
  contentFormat: "string",
})
/*
export default {
  "0001_initial_migration": "CREATE TABLE users (id serial primary key)",
  "0002_add_email_to_users": "ALTER TABLE users ADD COLUMN email text",
}
*/

Embedding as Part of a Build System

make-vfs can be embedded as part of your build system. For example, maybe you want to build an application with filesystem routes, after creating a new route or as a prebuild step you would just run make-vfs ./src/routes ./src/routes.generated.ts e.g. inside a package.json prebuild script.

{
  "scripts": {
    "prebuild": "make-vfs --dir ./src/routes --content-format string --outfile asset-bundle.js"
  }
}