Templates
A hygen template is a header of a markdown-like frontmatter and a body of an ejs templating engine.
Frontmatter
The frontmatter is delimited by a matching --- top and bottom with yaml in it, where we define the template metadata.
Templates are also rendered, so if we have this in the file _templates/mailer/campaign/emails.ejs.t:
And this command:
It builds this frontmatter, behind the scenes:
[[info]]
|###### Frontmatter cleans up our act
|While other generator engines use the file names, folder structure, or arbitrary configuration files to store metadata, hygen uses the frontmatter.
|
|This makes templating and generators clean and maintainable and meta data lives directly in the template it refers to.
Template Body
Let's recall how a template looks like. Templates bodies are ejs:
In hygen, the variable name is blessed, because you can get a capitalized version of it for free, by saying Name. There will be a growing list of variables that are special, where you get 'free' version of them to save some time, but currently it's only that one.
If we wanted to capitalize some other variable then we could do this:
Helpers and Inflections
You can also use the built-in helpers by accessing h:
The special helper object h also hosts inflections. With these you can pluralize, singularize and more:
You can see the full list here. With time, we'll add more utilities onto h.
Change case helpers
hygen provides ability to semantic case changing with change-case library, it's simple to use and very easy to understand:
There is a usecase for react based components generation:
With name HelloWorld will be compiled to:
You can see the full list here.
Local Variables
As we saw earlier, any CLI argument or prompt parameter automatically becomes a local variable in your templates.
There are two ways to refer to variables:
This way refers to the message CLI argument or prompt parameter, in its bare form. This also means this parameter cannot be optional (otherwise a reference error is thrown).
This way refers to the message CLI argument or prompt parameter, through the locals object. This is great if you want to check a variable for existance before using it like so:
There's a small gem here, in the form of -%>. This will slurp the last newline, so that the if(..){ clause won't generate garbage newlines into our final output.
For more of how EJS works take a look here.
Predefined Variables
If you look at the following command:
hygen will break it up for you and place certain values in special variables that are automatically available in your templates:
| Variable | Content | Example |
|---|---|---|
templates | Templates path (absolute) | /User/.../project/_templates |
actionfolder | Action path | /.../component/new |
generator | Generator name | component |
action | Action name | new |
subaction | Sub-action name | story |
cwd | Process working directory | /User/.../project |
For example to use actionfolder say:
Addition
By default templates are 'added' to your project as a new target file. By specifying a to: frontmatter property, we're telling hygen where to put it. force: true will tell hygen to overwrite an existing file without prompting the user ( default is force: false ).
If a target file already exists, and you don't want to overwrite it, you can use unless_exists (here's the pull request for more).
From & Shared Templates
By default the body of the template is used as input to create the target file. By specifying a from: frontmatter property, we're telling hygen from which external file to load the body from. E.g. from: shared/docs/readme.md will tell hygen to load the body from _templates/shared/docs/readme.md. The body of this template is ignored:
Injection
You can also choose to inject a template into an existing target file.
For this to work, you need to use inject: true with the accompanied inject-specific props.
The new props to notice here are after and skip_if. This template will add the react-native-fs dependency into a package.json file, but it will not add it twice (because of skip_if).
[[info]]
|###### Regular expressions everywhere promote flexibility
| In after: dependencies, 'dependencies' is actually a regular expression, so it'll find the "dependencies":{ block in a package.json file
Here are the available properties for an inject: true template:
beforeorafterwhich contain a regular expression of text to locate. The inject line will appearbeforeorafterthe located line.prependorappend, when true, add a line to start or end of file respectively.at_linewhich contains a line number will add a line at this exact line number.
In almost all cases you want to ensure you're not injecting content twice:
skip_ifwhich contains a regular expression / text. If exists, injection is skipped.
Let's see how these play out in the Redux use case.
Shell
Shell actions give you the ability to trigger any shell commands. You can do things such as:
- Copy a resource or an asset from a template into a target folder
- Pipe the output of a template into a shell command
- Perform any other side-effect - touch files, restart processes, trigger a
yarn installor what have you.
Here's how to pipe a generator's output into a shell command:
Using just the sh: property, hygen will understand this is a shell action. Note that you have the cwd variable pre-available to you to indicate the current working directory.
This generator will pipe its output into the shell command, so you can assume it happens - note that cat is expecting someone to give it STDIN.
Some times you want to run a generator and just invoke an additional command. This means the shell action can be added to what ever action you wanted to perform (inject or addition).
Here's a common task: add a dependency and then run yarn install.
Conditional Rendering
If you'd like to render a certain template based on the value of a variable, then you can do something like this:
When hygen meets a to: value that is null, it will skip the output of that template, meaning it won't get rendered at all.
Next up, we'll move on to generators.
All Frontmatter Properties
| Property | Type | Default | Example |
|---|---|---|---|
to: | String (url) | undefined | my-project/readme.md |
from: | String (url) | undefined | shared/docs/readme.md |
force: | Boolean | false | true |
unless_exists: | Boolean | false | true |
inject: | Boolean | false | true |
after: | Regex | undefined | devDependencies |
skip_if: | Regex | undefined | myPackage |
sh: | String | undefined | echo: "Hello this is a shell command!" |