Skip to content

Commit 14085a6

Browse files
committed
Auto-generate entry points
1 parent d7e50c2 commit 14085a6

24 files changed

Lines changed: 95 additions & 62 deletions

build.mjs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { copyFile, rm, writeFile } from "node:fs/promises";
1+
import { copyFile, readFile, rm, writeFile } from "node:fs/promises";
22
import { dirname, join } from "node:path";
33
import { fileURLToPath } from "node:url";
44

@@ -62,19 +62,70 @@ const onEndPlugin = {
6262
},
6363
};
6464

65+
/**
66+
* Emit a tiny stub file for each Action entrypoint. Each stub imports the shared bundle
67+
* and calls the respective entry point.
68+
*
69+
* @type {esbuild.Plugin}
70+
*/
71+
const entryPointsPlugin = {
72+
name: "entry-points",
73+
setup(build) {
74+
const actions = [];
75+
76+
const toPascal = (s) =>
77+
s.replace(/(^|-)([a-z0-9])/gi, (_, __, c) => c.toUpperCase());
78+
79+
// Find the source files containing action entry points.
80+
build.onStart(() => {
81+
const actionFiles = globSync("src/*-action{,-post}.ts");
82+
for (const actionFile of actionFiles) {
83+
const match = actionFile.match(/src\/(.*)-action(-post)?.ts/);
84+
const actionName = match[1];
85+
const isPost = match[2] !== undefined;
86+
87+
actions.push({
88+
path: actionFile,
89+
name: actionName,
90+
isPost,
91+
pascalCaseName: `${toPascal(actionName)}${isPost ? "Post" : ""}Action`,
92+
});
93+
}
94+
});
95+
96+
// Emit entry point stubs for each action using the entry template.
97+
build.onEnd(async (result) => {
98+
// Read the entry point template.
99+
const templatePath = "action-entry.js.tpl";
100+
const template = await readFile(join(SRC_DIR, templatePath), "utf-8");
101+
102+
const makeHeader = (sourceFile) =>
103+
`// Automatically generated from '${templatePath}' for '${sourceFile}'.\n\n`;
104+
105+
// Write entry point stubs for each action.
106+
for (const action of actions) {
107+
await writeFile(
108+
join(
109+
OUT_DIR,
110+
`${action.name}${action.isPost ? "-post" : ""}-entry.js`,
111+
),
112+
makeHeader(action.path) +
113+
template.replaceAll("__ACTION__", action.pascalCaseName),
114+
);
115+
}
116+
});
117+
},
118+
};
119+
65120
const context = await esbuild.context({
66121
// Include upload-lib.ts as an entry point for use in testing environments.
67-
entryPoints: globSync([
68-
`${SRC_DIR}/*-entry.ts`,
69-
"src/entry-points.ts",
70-
"src/upload-lib.ts",
71-
]),
122+
entryPoints: globSync(["src/entry-points.ts", "src/upload-lib.ts"]),
72123
bundle: true,
73124
format: "cjs",
74125
outdir: OUT_DIR,
75126
platform: "node",
76127
external: ["./entry-points"],
77-
plugins: [cleanPlugin, copyDefaultsPlugin, onEndPlugin],
128+
plugins: [cleanPlugin, copyDefaultsPlugin, entryPointsPlugin, onEndPlugin],
78129
target: ["node20"],
79130
define: {
80131
__CODEQL_ACTION_VERSION__: JSON.stringify(pkg.version),

lib/analyze-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/analyze-post-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/autobuild-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-post-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/resolve-environment-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-post-entry.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)