mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Changed build process and added pre-build script
This commit is contained in:
parent
5ace90af49
commit
a5dc998323
5 changed files with 68 additions and 73 deletions
|
@ -5,7 +5,9 @@
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"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:lint": "eslint src/**/*.ts && eslint test/**/*.ts",
|
||||||
"test:unit": "jest --testPathPattern=unit --watchAll=false",
|
"test:unit": "jest --testPathPattern=unit --watchAll=false",
|
||||||
"test:integration": "jest --testPathPattern=integration",
|
"test:integration": "jest --testPathPattern=integration",
|
||||||
|
|
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
65
scripts/generate.ts
Normal file
65
scripts/generate.ts
Normal file
|
@ -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<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);
|
|
@ -1,5 +0,0 @@
|
||||||
import fixDeprecatedBuffer from "./fixes/buffer-fix";
|
|
||||||
import processActionYamlTemplate from "./templates/action-yml";
|
|
||||||
|
|
||||||
fixDeprecatedBuffer();
|
|
||||||
processActionYamlTemplate();
|
|
|
@ -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<string, ActionInput> };
|
|
||||||
if (!action.inputs) {
|
|
||||||
action.inputs = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
action.inputs = processInputs(action.inputs);
|
|
||||||
|
|
||||||
const updatedContent = yaml.stringify(action);
|
|
||||||
fs.writeFileSync(pathOut, updatedContent, "utf8");
|
|
||||||
}
|
|
||||||
|
|
||||||
function processInputs(inputs: Record<string, ActionInput>) {
|
|
||||||
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;
|
|
||||||
}
|
|
Loading…
Reference in a new issue