Added ability to remove custom fields from action metadata

This commit is contained in:
Kir_Antipov 2023-05-20 15:05:32 +03:00
parent 5cb5700273
commit 1e6391f5b5
2 changed files with 80 additions and 0 deletions

View file

@ -681,3 +681,79 @@ export function createModuleLoaderTypeScriptDefinitionForActionMetadata(metadata
return document; 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<void | never> {
options = { sourceFileName: basename(inputMetadataFile.toString()), ...options };
const metadata = (await readFile(inputMetadataFile, options)).toString();
const stringifiedStrippedMetadata = stripActionMetadataStringFromCustomFields(metadata, options);
await writeFile(outputMetadataFile, stringifiedStrippedMetadata, options);
}

View file

@ -35,6 +35,10 @@ export {
processActionMetadataTemplateFile, processActionMetadataTemplateFile,
processActionMetadataTemplateString, processActionMetadataTemplateString,
stripActionMetadataFromCustomFields,
stripActionMetadataFileFromCustomFields,
stripActionMetadataStringFromCustomFields,
createTypeScriptDefinitionForActionMetadata, createTypeScriptDefinitionForActionMetadata,
createModuleLoaderTypeScriptDefinitionForActionMetadata, createModuleLoaderTypeScriptDefinitionForActionMetadata,
} from "./action-metadata"; } from "./action-metadata";