go-plugin-templates

1.0.0 • Public • Published

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

Package Sidebar

Install

npm i go-plugin-templates

Weekly Downloads

217

Version

1.0.0

License

MIT

Unpacked Size

16.6 kB

Total Files

9

Last publish

Collaborators

  • termosa