From 1e6391f5b5df42890415627e0e4974812cf9967d Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sat, 20 May 2023 15:05:32 +0300 Subject: [PATCH] Added ability to remove custom fields from action metadata --- src/utils/actions/action-metadata.ts | 76 ++++++++++++++++++++++++++++ src/utils/actions/index.ts | 4 ++ 2 files changed, 80 insertions(+) diff --git a/src/utils/actions/action-metadata.ts b/src/utils/actions/action-metadata.ts index 5c3e428..182d5f5 100644 --- a/src/utils/actions/action-metadata.ts +++ b/src/utils/actions/action-metadata.ts @@ -681,3 +681,79 @@ export function createModuleLoaderTypeScriptDefinitionForActionMetadata(metadata return document; } + +/** + * Removes custom fields from Action Metadata. + * + * @param metadata - The original action metadata to be stripped. + * + * @returns A new action metadata object stripped of custom fields. + */ +export function stripActionMetadataFromCustomFields(metadata: ActionMetadata): ActionMetadata { + const stripped = { ...metadata }; + delete stripped.groups; + delete stripped.types; + + stripped.inputs = stripped.inputs ? { ...stripped.inputs } : undefined; + for (const [name, input] of Object.entries(stripped.inputs || {})) { + const strippedInput = { ...input }; + delete strippedInput.type; + delete strippedInput.unique; + delete strippedInput.redirect; + + stripped.inputs[name] = strippedInput; + } + + stripped.outputs = stripped.outputs ? { ...stripped.outputs } : undefined; + for (const [name, output] of Object.entries(stripped.outputs || {})) { + const strippedOutput = { ...output }; + delete strippedOutput.type; + delete strippedOutput.unique; + delete strippedOutput.redirect; + + stripped.outputs[name] = strippedOutput; + } + + return stripped; +} + +/** + * Removes custom fields from action metadata and returns a stringified version of the stripped metadata. + * + * @param metadata - The stringified action metadata to be stripped. + * @param options - An optional set of options to apply when processing the metadata. + * + * @returns A stringified version of the stripped action metadata. + */ +export function stripActionMetadataStringFromCustomFields(metadata: string, options?: ActionMetadataFormattingOptions): string { + const newline = options?.newline ?? DEFAULT_NEWLINE; + const generateAutoGeneratedWarningMessage = options?.generateAutoGeneratedWarningMessage ?? true; + + const parsedMetadata = parseActionMetadataFromString(metadata); + const strippedMetadata = stripActionMetadataFromCustomFields(parsedMetadata); + const stringifiedStrippedMetadata = stringifyYaml(strippedMetadata, options); + + const fixedStringifiedStrippedMetadata = newline === UNIX_NEWLINE ? stringifiedStrippedMetadata : stringifiedStrippedMetadata.replaceAll(UNIX_NEWLINE, newline); + const warningMessage = generateAutoGeneratedWarningMessage ? generateAutoGeneratedWarningFrame(options) : undefined; + const stringifiedStrippedMetadataWithWarning = [warningMessage, fixedStringifiedStrippedMetadata].filter(x => x).join(newline); + + return stringifiedStrippedMetadataWithWarning; +} + +/** + * Reads an action metadata file, removes custom fields from it, and writes the resulting metadata to a file. + * + * @param inputMetadataFile - The path to the original action metadata file to be stripped. + * @param outputMetadataFile - The path to the output metadata file. + * @param options - An optional set of read/write options and processing options to apply. + * + * @returns A promise that resolves when the metadata has been written to the output file, or rejects if any step fails. + * @throws If reading, parsing, processing, or writing the action metadata fails. + */ +export async function stripActionMetadataFileFromCustomFields(inputMetadataFile: AsyncFilePath, outputMetadataFile: AsyncFilePath, options?: ActionMetadataTemplateFileProcessingOptions): Promise { + options = { sourceFileName: basename(inputMetadataFile.toString()), ...options }; + + const metadata = (await readFile(inputMetadataFile, options)).toString(); + const stringifiedStrippedMetadata = stripActionMetadataStringFromCustomFields(metadata, options); + await writeFile(outputMetadataFile, stringifiedStrippedMetadata, options); +} diff --git a/src/utils/actions/index.ts b/src/utils/actions/index.ts index 0acc456..2d1735f 100644 --- a/src/utils/actions/index.ts +++ b/src/utils/actions/index.ts @@ -35,6 +35,10 @@ export { processActionMetadataTemplateFile, processActionMetadataTemplateString, + stripActionMetadataFromCustomFields, + stripActionMetadataFileFromCustomFields, + stripActionMetadataStringFromCustomFields, + createTypeScriptDefinitionForActionMetadata, createModuleLoaderTypeScriptDefinitionForActionMetadata, } from "./action-metadata";