repeat

Learn how to execute actions in a loop.

This action loops over a list of actions while a condition remains true.

For example, you can loop until an available filename is found or retry a check until a condition is met.

Example

The following template finds an available directory name by appending an incrementing suffix:

template.json5
{  "$schema": "https://schema.croct.com/json/v1/template.json",  "title": "Available name finder",  "description": "Finds an available directory name.",  "actions": [    {      "name": "define",      "variables": {        "baseName": "my-project",        "attempt": 0      }    },    {      "name": "locate-path",      "path": "my-project",      "limit": 1,      "depth": 0,      "result": "matches"    },    {      "name": "repeat",      "condition": "${this.matches.length !== 0}",      "actions": [        {          "name": "define",          "variables": {            "attempt": "${this.attempt + 1}"          }        },        {          "name": "locate-path",          "path": "${this.baseName}-${this.attempt}",          "limit": 1,          "depth": 0,          "result": "matches"        }      ]    },    {      "name": "print",      "semantics": "info",      "message": "Available name: ${this.baseName}${this.attempt > 0 ? '-' + this.attempt : ''}"    }  ]}

To apply this template, run:

npx croct@latest use template.json5

If my-project already exists, the loop increments the suffix until it finds an available name like my-project-1.

Properties

These are the supported properties:

name
string

The action identifier. Always repeat for this action.

condition
boolean

An expression that evaluates to a boolean.

The loop continues while the condition is true.

actions
Array<Action>

The actions to execute on each iteration.