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

@fabiospampinato/template

v2.3.1

Published

A super-simple way to create new projects based on templates.

Downloads

15

Readme

A super-simple way to create new projects based on templates.

A list of known active templates for this program can be found here.

Install

npm install -g @fabiospampinato/template

Usage

https://github.com/fabiospampinato/template/assets/1812093/4c53bf75-eb08-49c2-8b7f-8dae6dfa04eb

At a high level a template looks like this:

  1. A template is just a folder installed locally simply by git clone-ing it into ~/.templates, or you can copy it manually there.
  2. Each template has a folder named "template" inside it, which is what will be used for creating projects.
  3. Each template has a "template.json" file inside it, which lists all variables used by files inside the "template" folder.
  4. Each template may optionally have a folder named "hooks" inside it, containing files that will be executed at the appropriate times.

When creating a project from a template this happens:

  1. The user is asked to provide a value for each variable listed by the template inside its "template.json" file.
  2. The entire "template" folder inside the template is duplicated.
  3. Each non-binary file inside this folder is rendered with picolate using the provided variables.
  4. All rendered and binary files are copied in a newly created folder that has the name of the project you want to create.
  5. The postinstall hook, if present, is executed.
  6. That's it.

The following commands are available:

# Show help for the entire program
template --help

# Show help for a specific command
template install --help

# List all installed templates
template ls

# CD into an installed template
template cd my-template

# Install a template using a custom git endpoint
template install https://gitlab.com/some-user/some-repo.git my-template

# Install a template using a GitHub url
template install https://github.com/fabiospampinato/template-typescript-package typescript-package

# Install a template using a GitHub shorthand
template install fabiospampinato/template-typescript-package typescript-package

# Install a template from a local path
template install ./work-in-progress-template wip-template

# Automatically update all templates backed by git
template update

# Automatically update a specific template
template update my-template

# Uninstall a template
template uninstall my-template

# Create a new project from a template
template new typescript-package my-package

As mentioned in the "Overview" section a template must have this structure on disk:

.
├── hooks (optional)
│   └── postinstall.js (optional)
├── template
│   └── <anything>
└── template.json
  • You can put any files and folders you want under the "template" folder.
  • Everything inside it will be copied when creating a new project from it, and non-binary files will be rendered with picolate before saving them to disk.
  • Syntax highlighting for those files in your editor will probably be broken, but if you change the language to "Handlebars" it should look nice again.
  • Hooks are optional, each of them must export a function, that this program will call at the right times. Inside hooks process.cwd () will return the path of the new project.
  • The following hooks are supported:
    • postinstall.js: it will be called right after a new project has been created. It will receive the object of variables used to render the project.

The "template.json" file should look somewhat like this:

{
  "delimiters": [ // This is optional
    "start": "[[",
    "end": "]]"
  ],
  "variables": {
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "version": {
      "type": "string",
      "default": "1.0.0"
    },
    "author": {
      "type": "string"
    },
    "owner": {
      "type": "string"
    },
    "scoped": {
      "type": "boolean"
    },
    "tested": {
      "type": "boolean"
    }
  }
}
  • Custom template delimiters that picolate will use can be provided, if you need them.
  • The user will be prompted to provide a value for each variable you list here.
  • You should list every single variable referenced by any of your template files.
  • A variable can either be of type "string" or of type "boolean".
  • A variable can also provide a default value, to allow the user to use that value quickly just by pressing enter.

As we saw in the "Templates Folder" section each template must provide a metadata "template.json" file, listing all the variables it uses, and optionally providing some default values for them.

There's also a way to override this metadata, by having a file at ~/.templates/templates.json that looks like this:

{
  "typescript-package": {
    "variables": {
      "author": {
        "default": "Fabio Spampinato"
      },
      "owner": {
        "default": "fabiospampinato"
      }
    }
  },
  "some-other-template": {
    "variables": {
      "someOtherVariable": {
        "default": "Some default value"
      }
    }
  }
}

This is very useful because:

  • In order for a template to be usable by a wide range of people it makes no sense to specify for example a default value for the "author" variable.
  • Still, once you install a template you probably know who the author is going to be, and you don't want to type out that informattion a million times, so this way you can override defaults for a template.

License

MIT © Fabio Spampinato