diff --git a/package.json b/package.json index eda8616..c8cc04d 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,10 @@ "scripts": { "clean": "rimraf dist", "generate": "ncc run scripts/generate.ts -t", + "strip": "ncc run scripts/strip.ts -t", "prebuild": "npm run clean && npm run generate", "build": "ncc build -m -s --license license.txt", + "postbuild": "npm run strip", "test:lint": "eslint \"@(src|scripts)/**/*.ts\" && eslint --rule \"no-invalid-this: off\" tests/**/*.ts", "test:unit": "jest --testPathPattern=unit --watchAll=false", "test:integration": "jest --testPathPattern=integration --watchAll=false --passWithNoTests", diff --git a/scripts/strip.ts b/scripts/strip.ts new file mode 100644 index 0000000..3dabdbb --- /dev/null +++ b/scripts/strip.ts @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* 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[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);