Auto-generate entry points

This commit is contained in:
Michael B. Gale
2026-05-14 16:49:43 +01:00
parent d7e50c23fe
commit 14085a675c
24 changed files with 95 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
import { copyFile, rm, writeFile } from "node:fs/promises";
import { copyFile, readFile, rm, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
@@ -62,19 +62,70 @@ const onEndPlugin = {
},
};
/**
* Emit a tiny stub file for each Action entrypoint. Each stub imports the shared bundle
* and calls the respective entry point.
*
* @type {esbuild.Plugin}
*/
const entryPointsPlugin = {
name: "entry-points",
setup(build) {
const actions = [];
const toPascal = (s) =>
s.replace(/(^|-)([a-z0-9])/gi, (_, __, c) => c.toUpperCase());
// Find the source files containing action entry points.
build.onStart(() => {
const actionFiles = globSync("src/*-action{,-post}.ts");
for (const actionFile of actionFiles) {
const match = actionFile.match(/src\/(.*)-action(-post)?.ts/);
const actionName = match[1];
const isPost = match[2] !== undefined;
actions.push({
path: actionFile,
name: actionName,
isPost,
pascalCaseName: `${toPascal(actionName)}${isPost ? "Post" : ""}Action`,
});
}
});
// Emit entry point stubs for each action using the entry template.
build.onEnd(async (result) => {
// Read the entry point template.
const templatePath = "action-entry.js.tpl";
const template = await readFile(join(SRC_DIR, templatePath), "utf-8");
const makeHeader = (sourceFile) =>
`// Automatically generated from '${templatePath}' for '${sourceFile}'.\n\n`;
// Write entry point stubs for each action.
for (const action of actions) {
await writeFile(
join(
OUT_DIR,
`${action.name}${action.isPost ? "-post" : ""}-entry.js`,
),
makeHeader(action.path) +
template.replaceAll("__ACTION__", action.pascalCaseName),
);
}
});
},
};
const context = await esbuild.context({
// Include upload-lib.ts as an entry point for use in testing environments.
entryPoints: globSync([
`${SRC_DIR}/*-entry.ts`,
"src/entry-points.ts",
"src/upload-lib.ts",
]),
entryPoints: globSync(["src/entry-points.ts", "src/upload-lib.ts"]),
bundle: true,
format: "cjs",
outdir: OUT_DIR,
platform: "node",
external: ["./entry-points"],
plugins: [cleanPlugin, copyDefaultsPlugin, onEndPlugin],
plugins: [cleanPlugin, copyDefaultsPlugin, entryPointsPlugin, onEndPlugin],
target: ["node20"],
define: {
__CODEQL_ACTION_VERSION__: JSON.stringify(pkg.version),

5
lib/analyze-entry.js generated
View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/analyze-action.ts'.
"use strict";
// src/analyze-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runAnalyzeAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/analyze-action-post.ts'.
"use strict";
// src/analyze-post-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runAnalyzePostAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/autobuild-action.ts'.
"use strict";
// src/autobuild-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runAutobuildAction)();

5
lib/init-entry.js generated
View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/init-action.ts'.
"use strict";
// src/init-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runInitAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/init-action-post.ts'.
"use strict";
// src/init-post-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runInitPostAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/resolve-environment-action.ts'.
"use strict";
// src/resolve-environment-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runResolveEnvironmentAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/setup-codeql-action.ts'.
"use strict";
// src/setup-codeql-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runSetupCodeqlAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/start-proxy-action.ts'.
"use strict";
// src/start-proxy-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runStartProxyAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/start-proxy-action-post.ts'.
"use strict";
// src/start-proxy-post-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runStartProxyPostAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/upload-sarif-action.ts'.
"use strict";
// src/upload-sarif-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runUploadSarifAction)();

View File

@@ -1,5 +1,6 @@
// Automatically generated from 'action-entry.js.tpl' for 'src/upload-sarif-action-post.ts'.
"use strict";
// src/upload-sarif-post-entry.ts
var import_entry_points = require("./entry-points");
const import_entry_points = require("./entry-points");
void (0, import_entry_points.runUploadSarifPostAction)();

4
src/action-entry.js.tpl Normal file
View File

@@ -0,0 +1,4 @@
"use strict";
const import_entry_points = require("./entry-points");
void (0, import_entry_points.run__ACTION__)();

View File

@@ -1,3 +0,0 @@
import { runAnalyzeAction } from "./entry-points";
void runAnalyzeAction();

View File

@@ -1,3 +0,0 @@
import { runAnalyzePostAction } from "./entry-points";
void runAnalyzePostAction();

View File

@@ -1,3 +0,0 @@
import { runAutobuildAction } from "./entry-points";
void runAutobuildAction();

View File

@@ -1,3 +0,0 @@
import { runInitAction } from "./entry-points";
void runInitAction();

View File

@@ -1,3 +0,0 @@
import { runInitPostAction } from "./entry-points";
void runInitPostAction();

View File

@@ -1,3 +0,0 @@
import { runResolveEnvironmentAction } from "./entry-points";
void runResolveEnvironmentAction();

View File

@@ -1,3 +0,0 @@
import { runSetupCodeqlAction } from "./entry-points";
void runSetupCodeqlAction();

View File

@@ -1,3 +0,0 @@
import { runStartProxyAction } from "./entry-points";
void runStartProxyAction();

View File

@@ -1,3 +0,0 @@
import { runStartProxyPostAction } from "./entry-points";
void runStartProxyPostAction();

View File

@@ -1,3 +0,0 @@
import { runUploadSarifAction } from "./entry-points";
void runUploadSarifAction();

View File

@@ -1,3 +0,0 @@
import { runUploadSarifPostAction } from "./entry-points";
void runUploadSarifPostAction();