From a5dc998323c73bf079efb66e37ec850dde4e86fb Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sat, 6 May 2023 12:35:05 +0000 Subject: [PATCH] Changed build process and added pre-build script --- package.json | 4 +- scripts/fixes/buffer-fix.ts | 10 ----- scripts/generate.ts | 65 +++++++++++++++++++++++++++++++++ scripts/index.ts | 5 --- scripts/templates/action-yml.ts | 57 ----------------------------- 5 files changed, 68 insertions(+), 73 deletions(-) delete mode 100644 scripts/fixes/buffer-fix.ts create mode 100644 scripts/generate.ts delete mode 100644 scripts/index.ts delete mode 100644 scripts/templates/action-yml.ts diff --git a/package.json b/package.json index 82e80bd..a66531c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "src/index.ts", "type": "module", "scripts": { - "build": "ncc build --source-map --license license.txt && ncc run scripts/index.ts", + "generate": "ncc run scripts/generate.ts -t", + "prebuild": "npm run generate", + "build": "ncc build -s --license license.txt", "test:lint": "eslint src/**/*.ts && eslint test/**/*.ts", "test:unit": "jest --testPathPattern=unit --watchAll=false", "test:integration": "jest --testPathPattern=integration", diff --git a/scripts/fixes/buffer-fix.ts b/scripts/fixes/buffer-fix.ts deleted file mode 100644 index 1b27a4b..0000000 --- a/scripts/fixes/buffer-fix.ts +++ /dev/null @@ -1,10 +0,0 @@ -import fs from "fs"; - -export default function fixDeprecatedBuffer() { - const files = fs.readdirSync("./dist", { withFileTypes: true }); - for (const file of files.filter(x => x.isFile())) { - const content = fs.readFileSync(`./dist/${file.name}`, "utf8"); - const fixedContent = content.replace(/new Buffer\(/g, "Buffer.from("); - fs.writeFileSync(`./dist/${file.name}`, fixedContent, "utf8"); - } -} diff --git a/scripts/generate.ts b/scripts/generate.ts new file mode 100644 index 0000000..64d10fb --- /dev/null +++ b/scripts/generate.ts @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* 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[2] + & Parameters[1] + & Parameters[1] + & Parameters[1] + & Parameters[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); diff --git a/scripts/index.ts b/scripts/index.ts deleted file mode 100644 index 49047cc..0000000 --- a/scripts/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import fixDeprecatedBuffer from "./fixes/buffer-fix"; -import processActionYamlTemplate from "./templates/action-yml"; - -fixDeprecatedBuffer(); -processActionYamlTemplate(); diff --git a/scripts/templates/action-yml.ts b/scripts/templates/action-yml.ts deleted file mode 100644 index c1fec04..0000000 --- a/scripts/templates/action-yml.ts +++ /dev/null @@ -1,57 +0,0 @@ -import fs from "fs"; -import yaml from "yaml"; - -interface ActionInput { - description?: string; - required?: boolean; - default?: string; - unique?: boolean; - publisher?: boolean; -} - -export default function processActionYamlTemplate() { - processActionTemplate("./action.template.yml", "./action.yml"); -} - -function processActionTemplate(pathIn: string, pathOut: string) { - const content = fs.readFileSync(pathIn, "utf8"); - const action = yaml.parse(content) as { inputs?: Record }; - if (!action.inputs) { - action.inputs = {}; - } - - action.inputs = processInputs(action.inputs); - - const updatedContent = yaml.stringify(action); - fs.writeFileSync(pathOut, updatedContent, "utf8"); -} - -function processInputs(inputs: Record) { - const publishers = Object.entries(inputs).filter(([_, input]) => input.publisher).map(([key, _]) => key); - const nestedInputs = Object.entries(inputs).filter(([key, input]) => !input.publisher && !input.unique && !publishers.find(p => key.startsWith(p))); - - for (const [key, input] of Object.entries(inputs)) { - if (input.publisher) { - delete inputs[key]; - } - delete input.unique; - - if (typeof input.required !== "boolean") { - input.required = false; - } - if (input.default === undefined) { - input.default = "${undefined}"; - } - } - - for (const publisher of publishers) { - for (const [name, input] of nestedInputs) { - inputs[`${publisher}-${name}`] = { - ...input, - default: "${undefined}" - }; - } - } - - return inputs; -}