From 0f64c7347429c765f508307beb6f312a93952f85 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 23 Feb 2021 16:35:43 +0100 Subject: [PATCH] scaffolder-backend: added createTemporaryDirectory to action context Signed-off-by: Johan Haals --- .../src/scaffolder/tasks/TaskWorker.ts | 15 +++++++++++++++ .../src/scaffolder/tasks/types.ts | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts index af8f85f371..032fea6410 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts @@ -112,16 +112,31 @@ export class TaskWorker { const stepOutputs: { [name: string]: JsonValue } = {}; + // Keep track of all tmp dirs that are created by the action so we can remove them after + const tmpDirs = new Array(); + await action.handler({ logger: taskLogger, logStream: stream, parameters, workspacePath, + async createTemporaryDirectory() { + const tmpDir = await fs.mkdtemp( + `${workspacePath}_step-${step.id}-`, + ); + tmpDirs.push(tmpDir); + return tmpDir; + }, output(name: string, value: JsonValue) { stepOutputs[name] = value; }, }); + // Remove all temporary directories that were created when executing the action + for (const tmpDir of tmpDirs) { + await fs.remove(tmpDir); + } + templateCtx.steps[step.id] = { output: stepOutputs }; await task.emitLog(`Finished step ${step.name}`, { diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts index 6e0ecd8600..d7f2e6e51f 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts @@ -122,6 +122,11 @@ export type ActionContext = { workspacePath: string; parameters: { [name: string]: JsonValue }; output(name: string, value: JsonValue): void; + + /** + * Creates a temporary directory for use by the action, which is then cleaned up automatically. + */ + createTemporaryDirectory(): Promise; }; export type TemplateAction = {