Giter Club home page Giter Club logo

generate-template-files's Introduction

generate-template-files

NPM version Downloads

A simple generator that is independent from any language. Create custom boilerplate, scaffolding, skeleton, and templating code files that you need to create over and over again. All you need is NodeJS installed to get started.

Find this useful? Give it a ⭐

gif image created with licecap

Medium Article - Generate Template Files with Ease

Install

With NPM:

$ npm install generate-template-files

With Yarn:

$ yarn add generate-template-files

Usage

  1. Check out the examples folder or create a file called generate.js. Note that this file name is flexible.
  2. In that file, add in the example code below.
  3. Run node generate.js within Terminal (Mac) or Powershell (Win) once you've added your template files.
const {generateTemplateFiles} = require('generate-template-files');

const config = require('../package.json');

generateTemplateFiles([
    {
        option: 'Create Redux Store',
        defaultCase: '(pascalCase)',
        entry: {
            folderPath: './tools/templates/react/redux-store/',
        },
        stringReplacers: ['__store__', '__model__'],
        output: {
            path: './src/stores/__store__(lowerCase)',
            pathAndFileNameDefaultCase: '(kebabCase)',
        },
    },
    {
        option: 'Create Reduce Action',
        defaultCase: '(pascalCase)',
        entry: {
            folderPath: './tools/templates/react/redux-store/__store__Action.ts',
        },
        stringReplacers: ['__store__', '__model__'],
        dynamicReplacers: [
            {slot:'__version__', slotValue: config.version},
            {slot:'__description__', slotValue: config.description}
        ],
        output: {
            path: './src/stores/__store__/__store__(lowerCase)/__store__(pascalCase)Action.ts',
            pathAndFileNameDefaultCase: '(kebabCase)',
        },
        onComplete: (results) => {
            console.log(`results`, results);
        },
    },
]);

As outlined in the examples folder, I prefer to create a tools folder and place generate.js w/ templates files in there. Additionally, I'll add a script task ("generate": "node ./tools/generate.js") to my package.json file for convienent running of the generator using npm run generate or yarn generate.

┣━ package.json
┣━ src
┗━ tools/
   ┣━ generate.js
   ┗━ templates/
      ┣━ SomeFile.js
      ┗━ __name__(pascalCase)Action.ts

API

The generateTemplateFiles function takes an array of IConfigItem items.

IConfigItem

  • option - The name of the option to choose when asked.
  • defaultCase - The default Case Converters to use with the Replacer Slots in the template files. Default is (noCase).
  • entry.folderPath - Path to a folder of files or a single template file.
  • stringReplacers - An array of Replacer Slots used to replace content in the designated entry.folderPath.
  • dynamicReplacers - (Optional) An array of IReplacer used to replace content in the designated entry.folderPath.
  • output.path - The desired output path for generated files. Case Converters and Replacer Slots can be used to make the path somewhat dynamic.
  • output.pathAndFileNameDefaultCase - The Case Converters to use for the file path and file name(s).
  • onComplete - (Optional) Takes a callback function that is called once the file(s) have been outputted. A IResults object will be passed to the callback.
Example
{
    option: 'Create Redux Store',
    defaultCase: '(pascalCase)',
    entry: {
        folderPath: './tools/templates/react/redux-store/',
    },
    stringReplacers: ['__store__', '__model__'],
    dynamicReplacers: [
        {slot:'__version__', slotValue: config.version},
        {slot:'__description__', slotValue: config.description}
    ],
    output: {
        path: './src/stores/__store__(lowerCase)',
        pathAndFileNameDefaultCase: '(kebabCase)',
    },
    onComplete: (results) => {
        console.log(results);
    },
},

IResults

Below is an example of what you receive from the onComplete callback. It has the output path, list of files created and the Replacer Slots with the value entered.

  • output.path - The file(s) output path
  • output.files - List of files created
  • stringReplacers - List of Replacer Slots; name and values entered during the setup process
Example data you would get from the onComplate callback
{
    output: {
        path: './src/stores/some-thing',
        files: [
            './src/stores/some-thing/SomeThingModule.ts',
            './src/stores/some-thing/SomeThingModuleAction.ts',
            './src/stores/some-thing/SomeThingModuleGetter.ts',
            './src/stores/some-thing/SomeThingModuleMutation.ts',
            './src/stores/some-thing/SomeThingService.ts',
            './src/stores/some-thing/models/actions/ISomeThingState.ts',
            './src/stores/some-thing/models/actions/OtherThingResponseModel.ts'
        ]
    },
    stringReplacers: [
        {
            slot: '__store__',
            slotValue: 'some thing'
        },
        {
            slot: '__model__',
            slotValue: 'other thing'
        }
    ]
}

Replacer Slots

Replacer Slots are unique string value(s) to be replaced by the generator.

For example you can use something like this in your template files and/or in the file path names.

  • ~replacerSlot~
  • {{something else}}
  • __AnythingYouWant__

Case Converters

Case Converters transform the string value entered upon use of the generator.

Example

  • In the generator template __replacerSlot__ is appended by the (pascalCase) converter such as __replacerSlot__(pascalCase).
  • When the generator is run, the string "product reducer" is provided for __replacerSlot__.
  • As a result, the converter will produce ProductReducer.

Here is the string Lives down BY the River with each of the converters:

(noCase)        // Lives down BY the River
(camelCase)     // livesDownByTheRiver
(constantCase)  // LIVES_DOWN_BY_THE_RIVER
(dotCase)       // lives.down.by.the.river
(kebabCase)     // lives-down-by-the-river
(lowerCase)     // livesdownbytheriver
(pascalCase)    // LivesDownByTheRiver
(pathCase)      // lives/down/by/the/river
(sentenceCase)  // Lives down by the river
(snakeCase)     // lives_down_by_the_river
(titleCase)     // Lives Down By The River

One Rule: no spaces between the Replacer Slots and Case Converters. If there is a space, Case Converters will not work.

  • __name__(camelCase)
  • ⚠️ __name__ (camelCase)

generate-template-files's People

Contributors

codebelt avatar dependabot[bot] avatar ccheney avatar jimschofield avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.