Add publish:log action for debugging

Signed-off-by: Oliver Sand <oliver.sand@sda-se.com>
This commit is contained in:
Oliver Sand
2021-05-28 10:44:44 +02:00
parent bdcd994160
commit f26e6008f7
4 changed files with 80 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Add `publish:log` action for debugging.
@@ -26,6 +26,7 @@ import {
createPublishGithubAction,
createPublishGithubPullRequestAction,
createPublishGitlabAction,
createPublishLogAction,
} from './publish';
export const createBuiltinActions = (options: {
@@ -61,6 +62,7 @@ export const createBuiltinActions = (options: {
createPublishAzureAction({
integrations,
}),
createPublishLogAction(),
createCatalogRegisterAction({ catalogClient, integrations }),
];
};
@@ -20,3 +20,4 @@ export { createPublishAzureAction } from './azure';
export { createPublishGitlabAction } from './gitlab';
export { createPublishBitbucketAction } from './bitbucket';
export { createPublishFileAction } from './file';
export { createPublishLogAction } from './log';
@@ -0,0 +1,72 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { readdir, stat } from 'fs-extra';
import { relative, resolve } from 'path';
import { createTemplateAction } from '../../createTemplateAction';
/**
* This task is useful for local development and testing of both the scaffolder
* and scaffolder templates.
*/
export function createPublishLogAction() {
return createTemplateAction<{ message?: string; listWorkspace?: boolean }>({
id: 'publish:log',
description:
'Writes a message into the log or list all files in the workspace.',
schema: {
input: {
type: 'object',
properties: {
message: {
title: 'Message to output.',
type: 'string',
},
listWorkspace: {
title: 'List all files in the workspace, if true.',
type: 'boolean',
},
},
},
},
async handler(ctx) {
const files = await recursiveReadDir(ctx.workspacePath);
if (ctx.input?.message) {
ctx.logStream.write(ctx.input.message);
}
if (ctx.input?.listWorkspace) {
ctx.logStream.write(
`Workspace:\n${files
.map(f => ` - ./${relative(ctx.workspacePath, f)}`)
.join('\n')}`,
);
}
},
});
}
export async function recursiveReadDir(dir: string): Promise<string[]> {
const subdirs = await readdir(dir);
const files = await Promise.all(
subdirs.map(async subdir => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? recursiveReadDir(res) : [res];
}),
);
return files.reduce((a, f) => a.concat(f), []);
}