Merge pull request #8666 from kuangp/feat/catalogWrite

feat(catalog:write): support custom filenames
This commit is contained in:
Patrik Oldsberg
2021-12-29 19:03:56 +01:00
committed by GitHub
3 changed files with 43 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Support custom file name for `catalog:write` action
@@ -69,4 +69,33 @@ describe('catalog:write', () => {
yaml.stringify(entity),
);
});
it('should support a custom filename', async () => {
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'n',
namespace: 'ns',
annotations: {
[ORIGIN_LOCATION_ANNOTATION]: 'url:https://example.com',
},
},
spec: {},
};
await action.handler({
...mockContext,
input: {
filePath: 'some-dir/entity-info.yaml',
entity,
},
});
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledWith(
resolvePath(mockContext.workspacePath, 'some-dir/entity-info.yaml'),
yaml.stringify(entity),
);
});
});
@@ -21,13 +21,18 @@ import { Entity } from '@backstage/catalog-model';
import { resolveSafeChildPath } from '@backstage/backend-common';
export function createCatalogWriteAction() {
return createTemplateAction<{ name?: string; entity: Entity }>({
return createTemplateAction<{ filePath?: string; entity: Entity }>({
id: 'catalog:write',
description: 'Writes the catalog-info.yaml for your template',
schema: {
input: {
type: 'object',
properties: {
filePath: {
title: 'Catalog file path',
description: 'Defaults to catalog-info.yaml',
type: 'string',
},
entity: {
title: 'Entity info to write catalog-info.yaml',
description:
@@ -39,10 +44,11 @@ export function createCatalogWriteAction() {
},
async handler(ctx) {
ctx.logStream.write(`Writing catalog-info.yaml`);
const { entity } = ctx.input;
const { filePath, entity } = ctx.input;
const path = filePath ?? 'catalog-info.yaml';
await fs.writeFile(
resolveSafeChildPath(ctx.workspacePath, 'catalog-info.yaml'),
resolveSafeChildPath(ctx.workspacePath, path),
yaml.stringify(entity),
);
},