From b10a8cb578d3821920133b7c6ecf68d5e43ddd12 Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Mon, 22 Apr 2024 18:36:24 +0200 Subject: [PATCH] Include the section covering: Register Custom Filters with the New Backend System Signed-off-by: cmoulliard --- .../software-templates/writing-templates.md | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/docs/features/software-templates/writing-templates.md b/docs/features/software-templates/writing-templates.md index 4b169a0714..766a58d01b 100644 --- a/docs/features/software-templates/writing-templates.md +++ b/docs/features/software-templates/writing-templates.md @@ -814,3 +814,44 @@ spec: input: message: ${{ parameters.userName | betterFilter | base64 }} ``` + +### Register Custom Filters with the New Backend System + +To register the custom filters using the new Backend System, you will have to create a [backend module](../../backend-system/architecture/06-modules.md) calling following extension point: `scaffolderTemplatingExtensionPoint`. + +Here is a very simplified example of how to do that: + +```ts title="packages/backend/src/index.ts" +/* highlight-add-start */ +import { scaffolderTemplatingExtensionPoint } from '@backstage/plugin-scaffolder-node/alpha'; +import { createBackendModule } from '@backstage/backend-plugin-api'; +/* highlight-add-end */ + +/* highlight-add-start */ +const scaffolderModuleCustomFilters = createBackendModule({ + pluginId: 'scaffolder', // name of the plugin that the module is targeting + moduleId: 'custom-filters', + register(env) { + env.registerInit({ + deps: { + scaffolder: scaffolderTemplatingExtensionPoint, + // ... and other dependencies as needed + }, + async init({ scaffolder /* ..., other dependencies */ }) { + scaffolder.addTemplateFilters({ + base64: (...args: JsonValue[]) => btoa(args.join('')), + betterFilter: (...args: JsonValue[]) => { + return `This is a much better string than "${args}", don't you think?`; + }, + }); + }, + }); + }, +}); +/* highlight-add-end */ + +const backend = createBackend(); +backend.add(import('@backstage/plugin-scaffolder-backend/alpha')); +/* highlight-add-next-line */ +backend.add(scaffolderModuleCustomFilters()); +```