/* ************************************************************************** */
/*                              POSTBUILD SCRIPT                              */
/*                                                                            */
/* This script performs the following operations:                             */
/*   1. Strips action metadata file from custom fields                        */
/* ************************************************************************** */

import { stripActionMetadataFileFromCustomFields } from "@/utils/actions";
import { WINDOWS_NEWLINE } from "@/utils/environment";
import { FileNotFoundError } from "@/utils/errors";
import { basename } from "node:path";

/**
 * Path to the action metadata template file.
 */
const ACTION_METADATA_TEMPLATE_FILE = "./action.template.yml";

/**
 * Path to the processed action metadata file.
 */
const ACTION_METADATA_FILE = "./action.yml";

/**
 * Processing options.
 */
const OPTIONS: PostbuildOptions = {
    // The name of the source file everything else is generated from.
    sourceFileName: basename(ACTION_METADATA_TEMPLATE_FILE),

    // The encoding used for reading and writing files.
    encoding: "utf8",

    // Add a warning message about auto-generated files
    generateAutoGeneratedWarningMessage: true,

    // Remove fields that are only used in the template from the generated files.
    removeTemplateOnlyFields: true,

    // The newline character(s) to use in the generated files.
    newline: WINDOWS_NEWLINE,

    // The maximum line width for the generated files.
    lineWidth: 80,
};

/**
 * Processing options' type.
 */
type PostbuildOptions = Exclude<
    & Parameters<typeof stripActionMetadataFileFromCustomFields>[2]
, string>;

// Ensure the action metadata file exists
FileNotFoundError.throwIfNotFound(ACTION_METADATA_FILE);

// Strip the action metadata file
await stripActionMetadataFileFromCustomFields(ACTION_METADATA_FILE, ACTION_METADATA_FILE, OPTIONS);