Skip to content
Prev Previous commit
Next Next commit
Auto-generate entry points
  • Loading branch information
mbg committed May 14, 2026
commit 14085a675cb6d8cddc805b946cc1d51e3232a204
65 changes: 58 additions & 7 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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`,
});
}
});
Comment thread
henrymercer marked this conversation as resolved.

// 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),
Expand Down
5 changes: 3 additions & 2 deletions lib/analyze-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/analyze-post-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/autobuild-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/init-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/init-post-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/resolve-environment-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/setup-codeql-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/start-proxy-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/start-proxy-post-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/upload-sarif-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/upload-sarif-post-entry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/action-entry.js.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

const import_entry_points = require("./entry-points");
void (0, import_entry_points.run__ACTION__)();
3 changes: 0 additions & 3 deletions src/analyze-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/analyze-post-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/autobuild-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/init-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/init-post-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/resolve-environment-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/setup-codeql-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/start-proxy-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/start-proxy-post-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/upload-sarif-entry.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/upload-sarif-post-entry.ts

This file was deleted.