1.0.0 • Published 2 years ago

folder-template-generator v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

folder-template-generator

This is a CLI that generate folder structure described in a yaml document.

Getting started

Run npm install -g folder-template-generator to install the CLI globally. Then run npm gfs <template-name> --name=module-name to generate module-name folder structure like described in template-name. View example of the template below for configuration.

Adding the template configuration file

  • Create a folder called gfs-config at the root of the project
  • Create a file name template.yaml inside folder gfs-config which contains the folder structure template
  • Example of folder structure template (inside template.yaml)
---
module:
  rootFolder: src
  folderName: ${name}
  defaultFileExt: js
  actions:
    files:
      -
        name: ${name}-action.types
        extension: ts
        template: template/template-action.types.txt
      - 
        name: ${name}-actions
        extension: ts
        template: path-to-template
  screens: 
    components:
  services:
    files:
      -
        name: ${name}-services
        extension: ts
        template: empty
  store:
    files:
      -
        name: ${name}-reducer
        extension: ts
        template: empty
...

Template configuration file explanation

  • module is the name of the template and will be used when generating the folder structure with command gfs module (You can have multiple template, view the last section of this document for example).
  • rootFolder is the root folder where the folder will be generated. If not provided, it will fallback to src
  • folderName is the name of the folder that will be generated. If not provided, it will fallback to the --name option value.
  • defaultFileExt is required and will be the default file extension used for files.
  • All other node represents folder (actions, screens, components, services, store...)
  • To add file inside a folder, create the child node files inside the target folder:
    • name will be the name of the file
    • extension will be the extension of the file, if not provided will fallback to defaultFileExt
    • template is the path of the file (.txt) which content will be added to the file, specifying its value to empty or omitting it will generate an empty file

Generating the folder structure

With this example of configuration above, when you run gfs module user, the CLI will generate the following folder inside src folder:

- src
  - user
    - actions 
      - user-actions.types.ts
      - user-actions.ts
    - screens
      - components
    - services
      - user-services.ts
    - store
      - user-reducer.ts 

Example of a file template

As stated above, if you specify a path to a file on the key template, the content of this file will be added to the newly created file. For example, this is the content of the template/template-action.types.txt file that will be added inside user-actions.types.ts file.

export enum ${name}ActionTypes {}

--name interpolation

All placeholder $ufl{name} will be replaced by the value of the option --name when executing the CLI. If you want to upper the first letter of the --name option, please use $ufl{name} instead of ${name}.

Adding multiple folder template

To add multiple folder template inside template.yaml, just define another yaml document like below:

---
module:
  rootFolder: src
  folderName: ${name}
  defaultFileExt: ts
  actions:
    files:
      -
        name: ${name}-action.types
        extension: ts
        template: template/template-action.types.txt
      - 
        name: ${name}-actions
        template: path-to-template
  screens: 
    components:
  services:
    files:
      -
        name: ${name}-services
        extension: ts
        template: empty
  store:
    files:
      -
        name: ${name}-reducer
        extension: ts
        template: empty
...
---
test:
  rootFolder: src
  folderName: ${name}
  defaultFileExt: js
  testFolder:
    files:
      -
       name: test
       template: empty
...

Running gfs test test will generate the following folder structure:

- src
  - test
    - testFolder
      - test.js