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

undergen

v0.5.1-1

Published

Workspace code generator using highly flexible JavaScript configuration and ejs template renderer.

Downloads

35

Readme

Undergen

Usage

under gen yourTemplateName someDir:src/modules

and it will ask you about any other variables you need to define, based on your templates/yourTemplateName/template.json

See workspace-1 for an example workspace for using undergen.

Configuration files

export
interface UndergenConfig {
  templatesDir?: string // defaults to 'templates'
}

type variable = {
  // id used by template files and vars object
  name: string,

  // description used to further explain the variable's purpose.
  description?: string,

  // specify a default value for this variable, based on previous variable entries
  // Ex:
  // ...
  // variables: [
  //   { name: 'componentName' },
  //   { name: 'fileName', default: ({componentName}) => componentName.toLowerCase() }
  // ]
  default?: (vars: {[name: string]: value}) => any
}

module.exports = {
	// Variables which need to be defined for template
  variables?: (string | variable) []

	// files directory relative to this file
  // The files directory is a mirrored representation of these templates
  filesDir?: string // defaults to './files'

  // Where do these files go?
  // It is also the starting directory of the directory selector
  outDir?: string // defaults to root of project './'

  // locals that can be used inside the template files
  locals?: {[key: string]: any}

  // Called after files are created
  onComplete?: (variables: any, renderedFiles: FileToWrite[]) => void
}

Example 1

// File: .undergen.js
module.exports = {
  templatesDir: "./templatesDir"
}
// File: templatesDir/coolTemplate/template.js
module.exports = {
  // put additional variables in here
  // (some variables are already scraped from pathnames)
  "variables": [
    "inputArr" // this is a string array based on the suffix 'Arr'
  ]
}
// File: templatesDir/coolTemplate/files/{fileDir}/{fileName}.txt.erb
<% inputArr.forEach(str => { _%>
  <%= str %>!
<% }) _%>

So, now calling under gen coolTemplate fileDir:dest fileName:hello inputArr:a,b,c, will write the following file at dest/hello.txt:

a!
b!
c!

Example 2: Angular

Let's say you want to generate lots of files like Angular 2 components.

// File: templatesDir/NGC/template.js
module.exports = {
  // put additional variables in here
  // (some variables are already scraped from pathnames)
  "variables": [
    "componentName", // this is a string based on no significant suffix
    "componentCapitallizedName" // this is a string as well
  ]
}

Note, that for didactic purposes I'm skipping a module file.

// File: templatesDir/NGC/files/{fileDir}/{componentName}/{componentName}.component.ts.erb
@Component({
  template: require("./<%= componentName %>.component.html"),
  styles: [require("./<%= componentName %>.component.scss")]
})
class <%= componentCapitallizedName %>Component {
  // insert significant things
  // could have a inputsArr generate the inputs here and in the html file
}
<!-- File: templatesDir/NGC/files/{fileDir}/{componentName}/{componentName}.component.html.erb -->

<!-- <%= componentCapitallizedName %>Component template -->
<!-- could have a inputsArr generate inputs needed here with bindings -->
/* File: templatesDir/NGC/files/{fileDir}/{componentName}/{componentName}.component.scss.erb -->

/* <%= componentCapitallizedName %>Component styles */

So, now calling under gen NGC fileDir:src/app/shared componentName:hello componentCapitallizedName:Hello, will write the following files (excluding File: header):

// File: src/app/shared/hello/hello.component.ts
@Component({
  template: require("./hello.component.html"),
  styles: [require("./hello.component.scss")]
})
class HelloComponent {
  // insert significant things
  // could have a inputsArr generate the inputs here and in the html file
}
<!-- File: src/app/shared/hello/hello.component.html -->

<!-- HelloComponent template -->
<!-- could have a inputsArr generate inputs needed here with bindings -->
/* File: src/app/shared/hello/hello.component.scss -->

/* HelloComponent styles */

This is a code generation utility for when you want to store the template files in the workspace you're in.

This is prompted by quickly changing best practices in Angular 2, and the intricacies of subtle updates to how things need to be written differently in each codebase, having easy to understand and use codegen configured inside the repo itself, makes your workflow much easier for your team.

Variable types work as follows (not case sensitive):

  • *Dir: string Requires directory path
  • *Arr: string[] Requires string array
  • *Num | *Count: number Requires number
  • *: string Requires any string

You can see how this parsing works in the helpers.ts file.

Todo:

  • [x] CLI interface created based on compiled templates
  • [x] :art: Validation error reporting.
  • [ ] :white_check_mark: General Testing (also good for documentation).
  • [ ] :memo: User documentation.