diff --git a/.changeset/slow-jeans-cheer.md b/.changeset/slow-jeans-cheer.md new file mode 100644 index 0000000000..11e5029898 --- /dev/null +++ b/.changeset/slow-jeans-cheer.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Added a `publish:file` action to use for local development. The action is not installed by default. diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/file.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/file.ts new file mode 100644 index 0000000000..5c564147ad --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/file.ts @@ -0,0 +1,56 @@ +/* + * 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 fs from 'fs-extra'; +import { dirname } from 'path'; +import { InputError } from '@backstage/errors'; +import { createTemplateAction } from '../../createTemplateAction'; + +/** + * This task is useful for local development and testing of both the scaffolder + * and scaffolder templates. + * + * This action is not installed by default and should not be installed in + * production, as it writes the files to the local filesystem of the scaffolder. + */ +export function createPublishFileAction() { + return createTemplateAction<{ path: string }>({ + id: 'publish:file', + description: 'Writes contents of the workspace to a local directory', + schema: { + input: { + type: 'object', + required: ['path'], + properties: { + path: { + title: 'Path to a directory where the output will be written', + type: 'string', + }, + }, + }, + }, + async handler(ctx) { + const { path } = ctx.input; + + const exists = await fs.pathExists(path); + if (exists) { + throw new InputError('Output path already exists'); + } + await fs.ensureDir(dirname(path)); + await fs.copy(ctx.workspacePath, path); + }, + }); +} diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/index.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/index.ts index 70a7909144..419922704d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/index.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/index.ts @@ -18,3 +18,4 @@ export { createPublishGithubAction } from './github'; export { createPublishAzureAction } from './azure'; export { createPublishGitlabAction } from './gitlab'; export { createPublishBitbucketAction } from './bitbucket'; +export { createPublishFileAction } from './file';