Show HN: Jumpgen – A tiny framework for file generation in TypeScript

1 pointsposted 18 hours ago
by aleclarsoniv

1 Comments

aleclarsoniv

18 hours ago

I'd love to get everyone's feedback on this library I made.

I've found it helps a lot when writing a library that takes advantage of compile-time code generation. In the future, I think it could be cool to try and integrate jumpgen with bundlers, like Vite or Rollup, so your file generator can be used as a bundler plugin. Currently, jumpgen “generators” only have a programmatic API, but jumpgen will have a CLI as well, if you think that could be useful. Mostly though, jumpgen is intended as a “white label” library that is contained by your own CLI, if you have one.

# Motivation

Code generation can be useful for many tasks. I'm using code generation in my own libraries, pg-nano[1] and alien-rpc (in development). In pg-nano, I'm generating TypeScript functions that mirror Postgres functions (aka UDFs). In alien-rpc, I'm generating runtime metadata for RPC-style API routing.

I could not find a library that simplifies the task of writing a code generator, so I made jumpgen.

# What It Does

It's designed to handle the burdens of implementing a “watch mode” for your generators. It also returns an event emitter, especially useful for logging. Your generator is free to work in relative paths, since jumpgen handles path resolution according to a user-provided root directory.

It tracks which files your generator has read into memory and which directories your generator has listed. When those files/directories are changed or have files added/removed, your generator will rerun automatically (if watch mode is enabled). This even works with globs!

I've also included a "dedent" helper function so you can indent any template literal without that excess indentation carrying over into the emitted files.

# Feedback Wanted

If this sounds interesting to you, I'd love to hear any comments or criticisms about the API, the readme, or whatever else. Thanks!

[1]: https://github.com/pg-nano/pg-nano