cli/new: hardcode template file name and always point to dir

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-11 11:44:09 +01:00
parent 10c419cc0c
commit 8ff50542cd
5 changed files with 44 additions and 52 deletions
+12 -17
View File
@@ -64,14 +64,14 @@ Custom templates can be installed from local directories. To install a template
"backstage": {
"cli": {
"new": {
"templates": ["./templates/custom-plugin.yaml"]
"templates": ["./templates/custom-plugin"]
}
}
}
}
```
Each entry in the `templates` array should be relative path that points either directly to a template YAML file or a directory containing a `template.yaml` file. If the path starts with `./` it will be use as is, otherwise it will be resolved as a module within `node_modules`.
Each entry in the `templates` array should be relative path that points to a directory containing a `template.yaml` file. If the path starts with `./` it will be use as is, otherwise it will be resolved as a module within `node_modules`.
When defining the `templates` array it will override the default set of templates. If you want to keep using and of the build-in templates in the Backstage CLI you can reference them directly within the CLI package. This following is the full list of built-in templates:
@@ -100,9 +100,11 @@ When defining the `templates` array it will override the default set of template
## Creating your own CLI templates
Your first step in creating your own CLI template is composing your yaml file:
Each template lives in its own directory and must have a `template.yaml` file that describes the template. The template directory can also contain any files that should be templated or copied to the generated package.
```yaml title="in templates/custom-plugin.yaml"
Start by creating `template.yaml` in a new directory somewhere in your project, in this example we're using `./templates/custom-plugin/template.yaml`:
```yaml title="in templates/custom-plugin/template.yaml"
name: custom-plugin
role: frontend-plugin
description: Description of my CLI template # optional
@@ -117,25 +119,18 @@ The following properties are supported:
- `description` - A description of the type of package that this template produces.
- `values` - A map of additional values that will be present during templating. The values are themselves templated and can reference other values. If the key matches any of the user prompts, such as `pluginId`, the value will be used directly instead of prompting the user.
Once you have your composed template yaml file, [add your new template](#installing-custom-templates) to the CLI config in your root `package.json`:
Next, add any other files you want to be part of the template to the same directory. All files will be copied as is, except any files with a `.hbs` extension. They will be treated as [Handlebars](https://handlebarsjs.com/) templates and will be rendered with the values from the `template.yaml` file as well as additional prompts such as `pluginId`. For example, you could create a `src/index.ts` file with the following content:
```diff
{
// ...
"backstage": {
"cli": {
"new": {
+ "templates": [
+ "./templates/custom-plugin.yaml"
+ ]
}
}
}
```typescript title="in templates/custom-plugin/src/index.ts.hbs"
export function getPluginId() {
return '{{ pluginId }}';
}
```
If you'd like to see more examples, you can find all the default templates and its yaml files [here](https://github.com/backstage/backstage/tree/master/packages/cli/templates).
Once your template is ready, [add it to your config](#installing-custom-templates), and you should now be able to select it when running `yarn new`.
### Template Roles
The `role` property in the template yaml file is used to determine what input will be gathered for the template, as well as what actions will be taken after the new package has been created. The following roles are supported: