Skip to main content

run

In this section, you will find a reference for a Declarative Hook of type run.

Declarative hooks of type run are used to execute commands. Some interesting examples of their use are:

  • Install dependencies in the project;
  • Run commands from a tool that is part of the Plugin;
  • Apply configuration steps through shell or batch scripts.

The following is a simple example of a Declarative Hook of type run:

hooks:
- type: run
trigger: before-input
commands:
- echo hello from before-input!
- echo you can define multiple commands in a run hook!

When applying the Template or Plugin that defines this Declarative Hook, before asking for the input parameters, the console shows the following sentences:

  • hello from before-input!
  • you can define multiple commands in a run hook!.

You can use conditional operators with the condition parameter in a hook. Check the example below:

name: my-plugin
description: Describe your plugin explaining its purpose
types:
- app
spec:
inputs:
- label: Deseja instalar as dependĂȘncias?
type: bool
name: install_dependencies
hooks:
- type: run
trigger: after-render
commands:
- echo Instalando dependĂȘncias
- npm install
condition:
variable: install_dependencies
operator: "=="
value: true

In this example, the boolean input asks the user to install dependencies. The condition causes the hook to be executed only if the answer is true (operator == and value true). Only then will the npm install command be executed to install the dependencies.

You can define different commands per operating system. Check the example below:

hooks:
- type: run
trigger: before-input
working-dir: "{{project_name}}/some/dir"
linux:
- echo hello from before-input on linux!
windows:
- cmd /c echo hello from before-input on windows!
mac:
- echo hello from before-input on mac!

In this case, when applying the Template or Plugin, the message in the console will differ for each operating system. The supported operating systems are:

  • linux
  • mac
  • windows
caution

Command chaining and output redirection operators (|, ||, &&, &, >, >>, ;) are not supported and are interpreted as strings when executed.

run command on Windows

To run other applications via run in Windows, use one of the examples below:

  1. Using cmd. In this case, it opens an extra CMD window when the Plugin is being applied, but it is closed automatically afterward.
hooks:
- type: run
trigger: after-render
working-dir: "{{project_name}}/some/dir"
linux:
- npm install
windows:
- cmd /c npm install
mac:
- npm install
  1. Calling the npm binary directly. In this case, npm is executed in the same window:
hooks:
- type: run
trigger: after-render
working-dir: "{{project_name}}/some/dir"
linux:
- npm install
windows:
- npm.cmd install
mac:
- npm install

Available Actions of the run Declarative Hook

trigger:

Field used to define triggers that specify when commands should be executed.

before-input

Runs the hook commands before receiving the input parameters from the user.

trigger: before-input

before-render

Runs the hook commands before the Template generates files in the project.

trigger: before-render

after-render

Runs the hook commands after the Template generates files in the project.

trigger: after-render

working-dir: (Optional)

Allows you to define a directory relative to the project root where commands will be executed. When not set, commands will be executed in the project root. In the working-dir field you can use Jinja expressions, for example:

working-dir: "{{project_name}}/some/dir"

commands:

Allows you to define one or more commands to be executed.

commands: 
- echo hello from before-input!
- echo you can define multiple commands in a run hook!

To use the commands in Windows, the CMD tool must be run with the cmd /c argument. Check out an example below:

commands: 
- cmd /c echo hello from before-input!
- cmd /c echo you can define multiple commands in a run hook!

linux: (Optional)

Allows you to define one or more commands, specifying execution on Linux systems.

linux:
- npm install

windows: (Optional)

Allows you to define one or more commands, specifying execution on Windows systems.

windows: 
- cmd /c npm install

mac: (Optional)

Allows you to define one or more commands, specifying execution on macOS systems.

mac:
- npm install
caution

In actions of the Declarative Hook run, command chaining and output redirection operators (|, ||, &&, &, >, >>, ;) are not supported and are interpreted as strings when executed.