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

go-plugin-templates

v1.0.0

Published

Go plugin to use templates

Downloads

644

Readme

go-plugin-templates

npm Travis Coveralls Vulnerabilities js-standard-style

Go plugin to use templates.

This plugin is made on top of Embedded JavaScript templates and globby. Thanks @mde, @schnittstabil, @sindresorhus and @ult_combo for it!

Table of Contents

Usage

Installation

$ npm install --save-dev go go-plugin-templates
import go from 'go'
import { TemplatesPlugin } from 'go-plugin-templates'
go.use(TemplatesPlugin)

Quick Start

Templates Plugin helps you to you template files in your project. So you can generate your code out of template files. For example, you may want to change the app name in the readme file:

const appName = 'New App'
go.processTemplates({ appName }, 'README.md')

README.md file:

# <%= appName %>

more content…

Or you can create few new modules in your project:

const moduleTemplate = go.loadTemplates.sync('.templates/module.js')
moduleTemplate.write({ name: 'authentication' }, './app/modules/authentication.js')
moduleTemplate.write({ name: 'authorization' }, './app/modules/authorization.js')

You can also create templates out of string:

go.createTemplate(`# Contributors
<% contributors.forEach(contrib => { %>
<%= contrib.name %> <<%= contrib.email %>> (<%= contrib.link %>)
<% }) %>`)

Or you can create the whole project from the directory:

go.processTemplates(data, './microservice-project-template', 'microservices/new-one/')

It is possible to change the name of files before saving them:

go.processTemplates(data, 'templates/**', ({ ext, base }) => `assets/<%= ext %>/<%= base %>`)

API

Methods

createTemplate

crateTemplate( template [ , options ] ): Template

Creates a template from string.

  • template {string} - text with ejs placeholders
  • options {object} - all ejs options + filename and resolve properties
    • filename {string} - a path to the file (used in getSource() and to write rendered template)
    • resolve {string|function} - will be used for write if it is called without resolve argument
    • default ejs option escape is replaced with str => str so it won't affect templates

loadTemplates

loadTemplates( [ search, options ] ): Promise<Template[]>
loadTemplates.sync( [ search, options ] ): Template[]

Creates a list of templates out of matched files.

  • search {string|array|object} - globby options to search files
    • string, becomes a pattern to search
    • array, becomes patterns to search
    • object, is globby options object
    • for each of above cases there are several default options (redefined from globby defaults) will be assigned:
      • cwd: process.cwd()
      • dot: true
      • ignore: ['.git', 'node_modules']
      • pattern: '**'
  • options {object} - this will be given as it is (but filename will be changed) to createTemplate as options

processTemplates

processTemplates( context [ , search, options ] ): Promise<void>
processTemplates.sync( context [ , search, options ] ): void

Creates a list of templates out of matched files and writes them.

  • search {string|array|object} - as it is goes to loadTemplates
  • context {object} - as it is goes to render
  • options {object} - this will be given as it is (but filename will be changed) to createTemplate as options

Templates Methods

There is a Templates List (returned by loadTemplates) and a Template (that is a member of Templates list and a return value of createTemplate).

render

template.render( [ context ] ): string
templates.render( [ context ] ): string[]

Renders templates with a given context.

  • context {object} - a scope for ejs template

write

template.write( [ context, resolve ] ): Promise<void>
template.write.sync( [ context, resolve ] ): void
templates.write( [ context, resolve ] ): Promise<void>
templates.write.sync( [ context, resolve ] ): void

Renders templates with a given context and write them to files.

  • context {object} - as it is goes to render
  • resolve {string|function} - a path to save the file
    • string, if it ends with directory separator (/) will put files in that directory with their names from filename, otherwise it will be an exact path to save file; it also can contain ejs placeholders to use file meta information
    • function, receives meta information about the file and should return a string that is exact path to save the file

getSource

template.getSource(): string

Returns template source file name.

Template Syntax

Embedded JavaScript templates library is used to provide to you template features. Nothing is changed so you can read more about its features on ejs page.

License

MIT © Stanislav Termosa