mc-publish/scripts/generate.ts
2023-05-17 19:57:21 +03:00

65 lines
4 KiB
TypeScript

/* ************************************************************************** */
/* PREBUILD SCRIPT */
/* */
/* This script performs the following operations: */
/* 1. Processes action metadata template file */
/* 2. Parses action metadata from the processed file */
/* 3. Creates TypeScript definition for the action metadata */
/* 4. Generates a dynamic module loader */
/* 5. Saves the generated TypeScript definition and module loader */
/* ************************************************************************** */
import { parseActionMetadataFromFile, processActionMetadataTemplateFile, createTypeScriptDefinitionForActionMetadata, createModuleLoaderTypeScriptDefinitionForActionMetadata } from "@/utils/actions/action-metadata";
import { SPLIT_BY_WORDS_AND_GROUP_ACTION_PARAMETER_PATH_PARSER } from "@/utils/actions/action-parameter-path-parser";
import { WINDOWS_NEWLINE } from "@/utils/environment";
import { FileNotFoundError } from "@/utils/errors";
import { TypeScriptDocument } from "@/utils/typescript";
import { basename } from "node:path";
// Configure file paths
const ACTION_METADATA_TEMPLATE_FILE = "./action.template.yml"; // Path to the action metadata template file
const ACTION_METADATA_FILE = "./action.yml"; // Path to the processed action metadata file
const ACTION_METADATA_TYPESCRIPT_DEFINITION_FILE = "./src/action.ts"; // Path to the output TypeScript definition file for action metadata
const DYNAMIC_MODULE_LOADER_FILE = "./src/utils/reflection/module-loader.g.ts"; // Path to the output dynamic module loader TypeScript file
// Configure processing options
const OPTIONS: PrebuildOptions = {
sourceFileName: basename(ACTION_METADATA_TEMPLATE_FILE), // The name of the source file everything else is generated from
encoding: "utf8", // The encoding used for reading and writing files
disableESLint: true, // Disable ESLint in the generated TypeScript files
generateAutoGeneratedWarningMessage: true, // Add a warning message about auto-generated files
removeTemplateOnlyFields: true, // Remove fields that are only used in the template from the generated files
rootPath: "@/", // The root path for module imports
pathParser: SPLIT_BY_WORDS_AND_GROUP_ACTION_PARAMETER_PATH_PARSER, // The path parser used for action parameter paths
newline: WINDOWS_NEWLINE, // The newline character(s) to use in the generated files
tabSize: 4, // The number of spaces for indentation in the generated files
lineWidth: 80, // The maximum line width for the generated files
};
// Processing options' type
type PrebuildOptions = Exclude<
& Parameters<typeof processActionMetadataTemplateFile>[2]
& Parameters<typeof parseActionMetadataFromFile>[1]
& Parameters<typeof createTypeScriptDefinitionForActionMetadata>[1]
& Parameters<typeof createModuleLoaderTypeScriptDefinitionForActionMetadata>[1]
& Parameters<TypeScriptDocument["save"]>[1]
, string>;
// Ensure the action metadata template file exists
FileNotFoundError.throwIfNotFound(ACTION_METADATA_TEMPLATE_FILE);
// Process the action metadata template file
await processActionMetadataTemplateFile(ACTION_METADATA_TEMPLATE_FILE, ACTION_METADATA_FILE, OPTIONS);
// Parse the action metadata from the processed file
const metadata = await parseActionMetadataFromFile(ACTION_METADATA_FILE, OPTIONS);
// Create the TypeScript definition for the action metadata
const metadataTypeScriptDefinition = createTypeScriptDefinitionForActionMetadata(metadata, OPTIONS);
await metadataTypeScriptDefinition.save(ACTION_METADATA_TYPESCRIPT_DEFINITION_FILE, OPTIONS);
// Generate the dynamic module loader
const moduleLoaderTypeScriptDefinition = createModuleLoaderTypeScriptDefinitionForActionMetadata(metadata, OPTIONS);
await moduleLoaderTypeScriptDefinition.save(DYNAMIC_MODULE_LOADER_FILE, OPTIONS);