Executing Templates
With the sketch exec command, you can render a template and then execute it as a shell command.
To do this, you can use a regular file, a template with an id (either defined in the config file or inside the templates_dir), or even one defined within the command itself.
Examples
From Template
Let's say that we have a file named cmd_template.j2 inside our templates_dir, and it looks like this:
echo "{{ category }} engine... {{ category }}... argh!" > output_from_templates_dir.txt
ℹ️ You do not have to use
.j2as an extension for the template files. Any extension can be used.
We can refer to it by its id:
sketch --set category="gp2" tests/output/commands_tests -t cmd_template.j2
This will trigger the command and create a file containing
gp2 engine... gp2... argh!
From File
We can also use a file that is not inside templates_dir by using the -f flag and providing the path to said file. Relative paths will be resolved starting from the cwd.
So let's say that we have this template file:
echo "all the time you have to leave the {{ something }}!" > output_from_file.txt
Command:
sketch --set something="space" tests/output/commands_tests -f tests/commands_tests/cmd_from_file.j2
Output:
all the time you have to leave the space!
From Literal Template
Templates can also be defined directly as part of the command.
Command:
sketch --set condition="slower" tests/output/commands_tests 'echo "engine feels good... much {{ condition }} than before... amazing" > command_output.txt'
Output:
engine feels good... much slower than before... amazing
Hooks
Templates can be executed as commands as pre or post hooks in a git repo, typecript monorepo or typescript package presets:
# Commands to run before generation, from the root of the new project
hooks_pre:
- command:
# Inlining a new template definition here,
# but as always, we can use stored templates too
name: pre_hook
content: "echo '{{ greeting }}' > pre.txt"
context:
greeting: hi
# Commands to run after generation, from the root of the new project
hooks_post:
- command:
name: post_hook
content: "echo '{{ greeting }}' > post.txt"
context:
greeting: hi