|
1 | | -import { copyFile, rm, writeFile } from "node:fs/promises"; |
2 | | -import { dirname, join } from "node:path"; |
| 1 | +import { copyFile, readFile, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { basename, dirname, join } from "node:path"; |
3 | 3 | import { fileURLToPath } from "node:url"; |
4 | 4 |
|
5 | 5 | import * as esbuild from "esbuild"; |
@@ -62,18 +62,123 @@ const onEndPlugin = { |
62 | 62 | }, |
63 | 63 | }; |
64 | 64 |
|
| 65 | +/** The name of the virtual `entry-points` module. */ |
| 66 | +const SHARED_ENTRYPOINT = "entry-points"; |
| 67 | + |
| 68 | +/** |
| 69 | + * This plugin finds all source files that contain action entry points. |
| 70 | + * It then generates the virtual `entry-points` module which imports all identifies files, |
| 71 | + * and re-exports their `runWrapper` functions with suitable aliases. |
| 72 | + * A tiny stub file is emitted for each Action entrypoint. Each stub imports the shared bundle |
| 73 | + * and calls the respective entry point. |
| 74 | + * |
| 75 | + * @type {esbuild.Plugin} |
| 76 | + */ |
| 77 | +const entryPointsPlugin = { |
| 78 | + name: "entry-points", |
| 79 | + setup(build) { |
| 80 | + const namespace = "actions"; |
| 81 | + const actions = []; |
| 82 | + |
| 83 | + const toPascal = (s) => |
| 84 | + s.replace(/(^|-)([a-z0-9])/gi, (_, __, c) => c.toUpperCase()); |
| 85 | + |
| 86 | + // Find the source files containing action entry points. |
| 87 | + build.onStart(() => { |
| 88 | + const actionFiles = globSync("src/*-action{,-post}.ts"); |
| 89 | + for (const actionFile of actionFiles) { |
| 90 | + const match = basename(actionFile).match(/(.*)-action(-post)?/); |
| 91 | + |
| 92 | + if (match.length < 2) { |
| 93 | + throw new Error(`'${actionFile}' didn't match expected pattern.`); |
| 94 | + } |
| 95 | + |
| 96 | + const actionName = match[1]; |
| 97 | + const isPost = match[2] !== undefined; |
| 98 | + |
| 99 | + actions.push({ |
| 100 | + path: actionFile, |
| 101 | + name: actionName, |
| 102 | + isPost, |
| 103 | + pascalCaseName: `${toPascal(actionName)}${isPost ? "Post" : ""}Action`, |
| 104 | + }); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + // Resolve the virtual `entry-points` file and set the corresponding namespace. |
| 109 | + // Ideally, we'd `RegExp.escape` the entrypoint here, but that API isn't supported in Node 20. |
| 110 | + // Since we're dealing with a hardcoded string, this isn't too much of a problem. |
| 111 | + build.onResolve({ filter: new RegExp(`^${SHARED_ENTRYPOINT}$`) }, () => { |
| 112 | + return { path: SHARED_ENTRYPOINT, namespace }; |
| 113 | + }); |
| 114 | + |
| 115 | + // Generate the virtual `entry-points` file based on the actions we discovered. |
| 116 | + // Restrict using the namespace. The path filter does not need to discriminate any further. |
| 117 | + build.onLoad({ filter: /.*/, namespace }, async () => { |
| 118 | + const wrapperTemplatePath = "entry-wrapper.js.tpl"; |
| 119 | + const wrapperTemplate = await readFile( |
| 120 | + join(SRC_DIR, wrapperTemplatePath), |
| 121 | + "utf-8", |
| 122 | + ); |
| 123 | + |
| 124 | + const actionsSorted = actions.sort((a, b) => |
| 125 | + a.name.localeCompare(b.name), |
| 126 | + ); |
| 127 | + const imports = actionsSorted |
| 128 | + .map( |
| 129 | + (action) => |
| 130 | + `import * as ${action.pascalCaseName} from "./src/${basename(action.path)}"`, |
| 131 | + ) |
| 132 | + .join("\n"); |
| 133 | + const wrappers = actionsSorted |
| 134 | + .map((action) => |
| 135 | + wrapperTemplate.replaceAll("__ACTION__", action.pascalCaseName), |
| 136 | + ) |
| 137 | + .join("\n\n"); |
| 138 | + |
| 139 | + return { |
| 140 | + contents: `"use strict";\n${imports}\n\n${wrappers}\n`, |
| 141 | + resolveDir: ".", |
| 142 | + loader: "ts", |
| 143 | + }; |
| 144 | + }); |
| 145 | + |
| 146 | + // Emit entry point stubs for each action using the entry template. |
| 147 | + build.onEnd(async (result) => { |
| 148 | + // Read the entry point template. |
| 149 | + const templatePath = "action-entry.js.tpl"; |
| 150 | + const template = await readFile(join(SRC_DIR, templatePath), "utf-8"); |
| 151 | + |
| 152 | + const makeHeader = (sourceFile) => |
| 153 | + `// Automatically generated from '${templatePath}' for 'src/${basename(sourceFile)}'.\n\n`; |
| 154 | + |
| 155 | + // Write entry point stubs for each action. |
| 156 | + for (const action of actions) { |
| 157 | + await writeFile( |
| 158 | + join( |
| 159 | + OUT_DIR, |
| 160 | + `${action.name}${action.isPost ? "-post" : ""}-entry.js`, |
| 161 | + ), |
| 162 | + makeHeader(action.path) + |
| 163 | + template.replaceAll("__ACTION__", action.pascalCaseName), |
| 164 | + ); |
| 165 | + } |
| 166 | + }); |
| 167 | + }, |
| 168 | +}; |
| 169 | + |
65 | 170 | const context = await esbuild.context({ |
66 | 171 | // Include upload-lib.ts as an entry point for use in testing environments. |
67 | | - entryPoints: globSync([ |
68 | | - `${SRC_DIR}/*-action.ts`, |
69 | | - `${SRC_DIR}/*-action-post.ts`, |
70 | | - "src/upload-lib.ts", |
71 | | - ]), |
| 172 | + entryPoints: [ |
| 173 | + { in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT }, |
| 174 | + join(SRC_DIR, "upload-lib.ts"), |
| 175 | + ], |
72 | 176 | bundle: true, |
73 | 177 | format: "cjs", |
74 | 178 | outdir: OUT_DIR, |
75 | 179 | platform: "node", |
76 | | - plugins: [cleanPlugin, copyDefaultsPlugin, onEndPlugin], |
| 180 | + external: ["./entry-points"], |
| 181 | + plugins: [cleanPlugin, copyDefaultsPlugin, entryPointsPlugin, onEndPlugin], |
77 | 182 | target: ["node20"], |
78 | 183 | define: { |
79 | 184 | __CODEQL_ACTION_VERSION__: JSON.stringify(pkg.version), |
|
0 commit comments