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

temples

v2.1.1

Published

Generate and insert code automatically

Downloads

49

Readme

Getting Started

Installation

# Install globally
npm install [-g] temples

# Or, in your project
npm install --save-dev temples

Usage

Temples reads from a .temples.yaml configuration file. Refer to Configuration below to create your own configuration file before running temples.

You can run temples by specifying the generator command and props, or invoke a CLI step by step interface where you can input these parameters.

# Invoke CLI interface
temples

# Or, directly specify generator and value for each prop
temples [generator] --[prop]=[value] ...

# Read more in the manual
temples -h

Configuration

generators

Required. Temples will throw error if undefined.

In your .temples.yaml file, specify the generators object. This lists all available generators and what each does, either creating new files or inserting code into existing files.

# .temples.yaml

generators:
  # Give your command a name
  [command]:
    # Documentation for this command will
    # be shown during CLI interface
    doc: ...

    files: ...
    inserts: ...

files

Generate new files given a target path, and an optional template path. If template is given, temples will use the contents of that template, compile it with given props, and output to target path.

# .temples.yaml

generators:
  new-component:
    files:
      # Use `component.hbs` and create `index.js`
      - template: component.hbs
        target: index.js

      # Create empty file at `index.js`
      - target: index.js

      # Compile with `name` and create
      # file at `[component_name]/index.js`
      - target: '{{ name }}/index.js'

inserts

Insert code into targeted files. Temples uses user defined regex to find tags in targeted files and replace them with the parsed content. File comments are the best ways to do this:

/* components/index.js */

// temples(import {{ name }} from './{{ name }}';)
import Button from './Button';
# .temples.yaml

# File extension to ECMAScript regex pattern
#
# The first regex capture group is the
# template for the insert
regex:
  js: '\/\/ temples\((.+)\)'

generators:
  new-component:
    inserts:
      # Insert into `components/index.js`
      - target: components/index.js

      # Insert into `components/[component_name]/index.js`
      - target: 'components/{{ name }}/index.js'

Here's a great playground tool for finding the right regex pattern.

base

Specify the base paths for templates, files, or inserts. base can be specified and overridden in the root configuration file or the generator command's configuration.

# .temples.yaml

# Find templates, files, and inserts
# under `dir/`
base: dir

generators:
  new-component:
    # Override to be `dir/subdir/`
    base: dir/subdir

    # Find templates in `dir/templates`
    # Target files and inserts in `dir/targets`
    base:
      templates: dir/templates
      target: dir/targets

    # Find templates in `dir/templates`
    # Target files in `dir/targets/files`
    # Target inserts in `dir/targets/inserts`
    base:
      templates: dir/templates
      target:
        files: dir/targets/files
        inserts: dir/targets/inserts

default

Default prop values if not provided in CLI.

If there is no default provided and user doesn't specify value, Handlebars compiles undefined props to empty string.

# .temples.yaml

default:
  name: 'NewComponent'

generators:
  new-component:
    # Override default in root level
    default:
      name: 'NewestComponent'
    ...

props

Specify props that should be prompted for in the CLI interface. It's not optimal for temples to search through all props available in templates. You can specify which props to prompt for with this key.

# .temples.yaml

generators:
  new-component:
    # Ask user for `name` and `directory`
    props: [name, directory]

    # In YAML, this is the same
    props:
    - name
    - directory

    # Provide documentation for each prop
    # during CLI interface
    props:
    - name: name
      doc: Name of component
    - name: directory
      doc: Directory to place component in

position

Position to insert output to. This is relative to the regex tag in the target file. Default position is below. The most specific position will be used.

# .temples.yaml

position: above | below | right | left

generators:
  new-component:
    position: above | below | right | left

    inserts:
      - target: components/index.js
        position: above | below | right | left

Handlebars Helpers

Temples uses Handlebars templating engine, and temples has some built-in helpers.

camel-case

Convert into camelCase.

{{ camel name }}

# Input: { name: "BigButton" }
bigButton

kebab-case

Convert into kebab-case

{{ kebab-case name }}

# Input: { name: "bigButton" }
big-button

snake-case

Convert into snake_case

{{ snake-case name }}

# Input: { name: "big-button" }
big_button

upper-case

Convert into UPPER CASE

{{ upper-case name }}

# Input: { name: "big-button" }
BIG BUTTON

lower-case

Convert into lower case

{{ lower-case name }}

# Input: { name: "big-button" }
big button

title-case

Convert into TitleCase

{{ title-case name }}

# Input: { name: "big_button" }
BigButton

Customizing Handlebars

The built-in helpers might not be enough for your use case. You can customize the Handlebars instance temples uses by specifying a path to a JavaScript file that configures Handlebars.

Runtime Options

Use the given handlebars instance to change its settings. See the Handlebars runtime documentation.

Compile Options

Return an object to customize the compile options for Handlebars. See the Handlebars compile options.

# .temples.yaml

handlebars: configureHandlebars.js
// configureHandlebars.js

module.exports = (handlebars) => {
  handlebars.registerHelper('replace', (v) => {
    return v.replace(' ', '-');
  });

  return {
    noEscape: false,
  };
};

Caveats

Naming Conflicts

If a variable conflicts with a helper name (e.g. {{ title }}), Handlebars will treat it as a helper instead of a variable. You can namespace the variable with this or ./ to avoid naming conflicts.

{{ this.title }}

Escaping Handlebars Syntax

Sometimes you need Handlebars to ignore parsing a prop. For example, if you're generating a file from a twig template, Handlebars might parse {{ example }} unintentionally.

You can leave it as it is by escaping:

\{{ example }}

Example

Check out a documented example here.

License

Copyright © 2020 - Present, Gino Jacob. MIT License.