template-gen
v2.1.0
Published
A flexible template generator for any project
Downloads
1
Readme
❯ Why
It's tedious to always copy & past the same file or file content over and over again. In addition the content has be clean up over and over because mostly we need a clean version of the content. This module can be used as a cli or integrated into any node based project.
❯ Table of Contents
❯ Installation
As global CLI
You can install this module globally by
npm install -g template-gen
Have a look at RC configuration how you can setup a template path.
As project dependency
Add this module to your project dependencies
npm install template-gen --save-dev
then add the following entry to your npm scripts
{
"tg": "tg -d ./templates"
}
❯ Getting Started
Setup a template file
First you need to create a template folder. We recommend to use templates
as name as this module will look for this folder automatically.
mkdir templates
Alternatively you can pass the template path with parameter as shown in the installation instructions.
Next we need a template file. Create a file (e.g. controller.js
) within the templates
folder with the following content
module.exports = {
name: 'Controller',
description: 'Creating a controller',
target: 'controllers',
wrapFolder: params => `${params.controller.toLowerCase()}`,
parameters: [
{
type: 'text',
name: 'controller',
message: 'Whats the controller name?'
},
{
type: 'confirm',
name: 'haveConstructor',
message: 'With a constructor?'
}
],
files: [
{
template: params => {
return `export default class ${params.controller} {
someAttribute = '';` +
(params.haveConstructor ? `
constructor () {
}` : '') +
`
}
`;
},
fileName: params => `${params.controller}Controller.ts`
},
{
template: () => '<template></template>',
fileName: params => `${params.controller}Controller.html`
}
]
}
Don't forget to create the
controllers
folder
| Attribute | Description | | -------------- | ----------- | | name | The name to enter or select in the CLI | | description | Will be shown after you selected the name in the CLI | | target | The target directory from the root where the file will be created in | | wrapFolder | Should be undefined or a function which returns the parent folder name | | parameters | The CLI prompts to ask the user, you can use this prompts options | | files.template | The content of the generated file | | files.fileName | The file name of the generated file |
The parameters attribute can be a object or an array of prompts options.
Usage
With the above example setup you can now run
npm run tg
Then you will be asked for the template parameters and finally create the file.
Use Parameters
If you have template like the above with name Controller
and prompt controller
then you could use this to execute without prompts
npm run tg -- Controller --controller User --haveConstructor true
Or just if you just like to create a controller with prompts
npm run tg Controller
❯ RC configuration
In you project root you can create a file named .tgrc
to configure you template path
{
"path": "./templates"
}
❯ Related Projects
- aurelia-typescript-boilerplate - An Aurelia starter kit with TypeScript
- express-typescript-boilerplate - An express starter kit with TypeScript