Animated Circular Progress Bar0
Font.Lokio
CLI

Template Management

Complete guide to managing templates in Lokio CLI


configs.yaml is the configuration file used by Lokio to register and manage templates that will be used in the code generation process. This file contains a list of all templates, the parameters needed, and the input-output structure for each template.

General Structure of configs.yaml#

templates: # List of available templates
  - name: <template_name>  # Template name
    description: <template_description>  # Short description of the template
    parameters:             # List of parameters required by the template
      - name: <parameter_name>       # Parameter name
        type: <parameter_type>       # Data type of the parameter (string, options, etc.)
        required: <true/false>       # Whether the parameter is mandatory
        prompt: <question_prompt>    # Question or prompt displayed to the user
        options:                     # Options if the parameter type is "options"
          - <option_1>
          - <option_2>
        default: <default_value>     # (Optional) Default value if not filled
    files:                 # List of files to be processed by the template
      - path: <template_file_path>   # Path of the template file
        output: <output_file_path>   # Path of the output file after template is processed

Example configs.yaml Configuration#

Template Api#

Here is an example configuration for the Api template, which is used to generate code structures such as controller, schema, and usecase for APIs.

templates:
  - name: Api
    description: Generate new api [controller, schema, usecase]
    parameters:
      - name: name
        type: string
        required: true
        prompt: Name
      - name: feature
        type: options
        required: true
        prompt: What feature do you want to create?
        options:
          - sso
          - invitation
          - payment
          - learn
      - name: func
        type: string
        required: true
        prompt: What function do you want to create? [create, update, options, etc...]
      - name: method
        type: options
        required: false
        prompt: What HTTP method do you want to use?
        default: POST
        options:
          - POST
          - GET
          - PUT
          - PATCH
          - DELETE
          - HEAD
          - OPTIONS
    files:
      - path: api/controller.lokio
        output: src/<%= feature %>/<%= name %>/controller/<%= func %>.controller.ts
      - path: api/schema.lokio
        output: src/<%= feature %>/<%= name %>/schema/<%= func %>.schema.ts
      - path: api/usecase.lokio
        output: src/<%= feature %>/<%= name %>/usecase/<%= func %>.usecase.ts

Explanation of Items#

1. name#

Every template must have a unique name used to identify the template when executed. Example: Api.

2. description#

A brief description of the template's purpose to make it easier to understand. Example: Generate new api [controller, schema, usecase].

3. parameters#

This section contains the list of inputs (parameters) required from users to process the template.

Structure of an Element in parameters:#

  • name: The name of the parameter used in the template file.
    • Example: name, feature, func.
  • type: The expected data type.
    • Common types include:
      • string: Text input.
      • options: Input based on a predefined list of options (e.g., POST, GET, etc.).
  • required: Whether the parameter is mandatory (true) or optional (false).
  • prompt: The question displayed to the user to fill in the parameter.
  • options (optional): A list of choices if the type is options.
  • default (optional): Default value for optional parameters.

Example:#

parameters:
  - name: func
    type: string
    required: true
    prompt: What function do you want to create? [create, update, options, etc...]

4. files#

This section defines the paths of template files used and the output paths where files will be saved after the template is processed.

Structure of an Element in files:#

  • path: Path to the template file inside the Lokio directory. Example: api/controller.lokio.
  • output: Path of the output file that will be generated, with support for interpolating user-provided parameters (dynamic path).

Example:#

files:
  - path: api/schema.lokio
    output: src/<%= feature %>/<%= name %>/schema/<%= func %>.schema.ts

The output path will generate a file location such as:

src/<feature>/<name>/schema/<func>.schema.ts

For example:

  • feature = "payment"
  • name = "User"
  • func = "create"

It will generate:

src/payment/User/schema/create.schema.ts

Example Workflow#

Here is an illustration of the steps for using the Api template:

  1. Add the Api template to configs.yaml.

  2. Run the command to generate a new API:

    lokio g Api
  3. Provide the required parameters when prompted:

    • Name: User
    • Feature: payment
    • Function (func): create
    • Method (method): (leave as default POST)
  4. Lokio will generate the following files:

    • src/payment/User/controller/create.controller.ts
    • src/payment/User/schema/create.schema.ts
    • src/payment/User/usecase/create.usecase.ts