2.1.0 • Published 6 years ago

template-gen v2.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

❯ 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

AttributeDescription
nameThe name to enter or select in the CLI
descriptionWill be shown after you selected the name in the CLI
targetThe target directory from the root where the file will be created in
wrapFolderShould be undefined or a function which returns the parent folder name
parametersThe CLI prompts to ask the user, you can use this prompts options
files.templateThe content of the generated file
files.fileNameThe 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

❯ License

MIT

2.1.0

6 years ago

2.0.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago