go-plugin-templates v1.0.0
go-plugin-templates
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-templatesimport 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 ] ): TemplateCreates a template from string.
template{string} - text with ejs placeholdersoptions{object} - all ejs options +filenameandresolvepropertiesfilename{string} - a path to the file (used in getSource() and to write rendered template)resolve{string|function} - will be used forwriteif it is called withoutresolveargument- default ejs option
escapeis replaced withstr => strso 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: trueignore: ['.git', 'node_modules']pattern: '**'
options{object} - this will be given as it is (butfilenamewill be changed) tocreateTemplateas options
processTemplates
processTemplates( context [ , search, options ] ): Promise<void>
processTemplates.sync( context [ , search, options ] ): voidCreates a list of templates out of matched files and writes them.
search{string|array|object} - as it is goes toloadTemplatescontext{object} - as it is goes torenderoptions{object} - this will be given as it is (butfilenamewill be changed) tocreateTemplateas 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 ] ): voidRenders templates with a given context and write them to files.
context{object} - as it is goes torenderresolve{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
- string, if it ends with directory separator (/) will put files in that directory with their names from
getSource
template.getSource(): stringReturns 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