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

poopgen

v0.3.0

Published

Simple and flexible project generator

Downloads

4

Readme

poopgen 💩

Simple and flexible project generator.

With poopgen, your project generation logic lives inside of your template.

Why is it named poopgen?

Project generators emit files and I thought poopgen was a fitting name :)

Table Of Contents

Install

npm i poopgen
# or
yarn add poopgen
# or
pnpm add poopgen

Usage

import { poopgen } from "poopgen";

await poopgen({
	// tell poopgen where your template lives
	template: "./template",
	// tell poopgen where to build your project to
	dest: "./dest",
});

A Basic Example

This should be all you need to learn how to use poopgen. It's that easy to use!

A poopgen template is a folder that contains three things:

  • Files
  • Other folders
  • poopfiles

For the following example, we will assume that this is the file tree for our generator:

src/
├─ cli.ts
template/
├─ _poop.js
├─ index.ts.ejs
├─ README.md

src/cli.ts

import { poopgen } from "poopgen";

await poopgen({
	// tell poopgen where your template lives
	template: "./template",
	// tell poopgen where to build your project to
	dest: "./dest",
});

template/_poop.js

This is what poopgen recognizes as a poopfile. A poopfile is any simple JavaScript module named _poop.js that exports functions named before and after. (You can export before, after, or both!)

This allows you to embed logic and hook into the poopgen lifecycle directly within your template!

Poopfiles can live in any directory inside of your template.

The before function is executed before poopgen generates the directory The after function is executed after poopgen generates the directory

It just makes sense, right?

Here are some of the things you could use them for:

  • ask the user for input
  • Install dependencies or create a git repository after the project has been generated
  • skipping a directory if a condition is not met
    • maybe a user doesn't want to opt in to a certain feature
/** @type{import("poopgen").BeforeFn}  */
export async function before(ctx) {
	// the 'before' function is called before the current directory is generated
	console.log("before generating!");

	// IMPORTANT
	// ctx.data is the data used for rendering ejs templates
	ctx.data.name = "poopgen";

	// you could use this to collect information from the user to be used in your templates!
}

/** @type{import("poopgen").AfterFn}  */
export function after(ctx) {
	// the 'after' function is called after the current directory is generated

	console.log("done generating!");
}

template/index.ts.ejs

/* 
poopgen uses ejs to render templates. If a file ends with .ejs, poopgen knows it is a template and will strip the extension off after generating it. 

Note: If a file does not end with .ejs, it is copied to the destination without any templating

Remember how we defined ctx.data.name in this directory's poopfile? ('_poop.js'). This file, 'index.ts.ejs', requires name to be defined on the ctx.data object.
*/

console.log("Hello world from <%- name %>!");

template/README.md

# Example

This file does not end in '.ejs', it will be copied to '/dest/README.md'!

Extra

getCtx

Poopgen exports a function named getCtx that allows you to access the current context inside of a poopfile.

// so this...
import { getCtx } from "poopgen";

export function before() {
	const ctx = getCtx();

	ctx.data.message = "poopgen is awesome!";
}

// does the same as
export function before(ctx) {
	ctx.data.message = "poopgen is awesome!";
}

Context is useful if you want to create helper functions, but don't necessarily want to have to pass the context to all of them.

import { getCtx } from "poopgen";

export function before() {
	printTemplateData();
}

export function after() {
	printDestPath();
}

// logs the inside of ctx.data (ctx.data is used to render the ejs templates)
function printTemplateData() {
	const ctx = getCtx();

	console.log("rendering template files with", ctx.data);
}

// logs the destination of the generation
function printDestinationPath() {
	const ctx = getCtx();

	console.log("project was generated to", ctx.dir.path);
}

Built with poopgen

ts-lib-gen

A simple generator to create a new typescript library