DKCutter hooks empower you to customize and automate tasks during project generation. These hooks are JavaScript or TypeScript scripts that execute at specific points in the generation process, allowing you to perform actions like data validation, pre-processing, and post-processing.
DKCutter offers two primary hook types:
Hook Name | Execution Timing | Working Directory | Template Variables Access |
---|---|---|---|
preGenProject | Executes after user prompts are answered, but before the template processing begins. | Root directory of the generated project | Yes |
postGenProject | Executes after the entire project generation process is complete. | Root directory of the generated project | Yes |
Hooks reside within a dedicated hooks
directory located at the root of your DKCutter template. Both JavaScript (.js) and TypeScript (.ts) files are supported for creating hooks.
Here's an example directory structure demonstrating how hooks are organized within your template:
It's essential for hooks to be robust and handle potential errors gracefully. If a hook exits with a non-zero status code, DKCutter will halt the project generation process and remove any partially generated project directory to ensure a clean slate.
Similar to project templates, preGenProject
and postGenProject
hooks can leverage Nunjucks templating for dynamic content. This allows you to access and manipulate configuration values defined in your dkcutter.json
file.
Here's an example demonstrating how to access a template variable within a JavaScript hook:
A preGenProject
hook can be used to validate user-provided input during the interactive configuration stage. The following JavaScript code snippet checks if the project slug adheres to a specific format:
A postGenProject
hook can conditionally manage files and directories within the generated project. This example demonstrates how to remove unnecessary lock files based on the chosen package manager:
By incorporating hooks into your DKCutter projects, you can significantly enhance automation, data validation, and project customization, leading to a more streamlined and efficient project generation experience.