|
| 1 | +import { z } from "zod"; |
| 2 | +import { Node, Project, ArrayLiteralExpression } from "ts-morph"; |
| 3 | +import { defineExtension } from "@webiny/project/defineExtension"; |
| 4 | +import crypto from "crypto"; |
| 5 | +import path from "path"; |
| 6 | +import fs from "fs"; |
| 7 | + |
| 8 | +export const BuildParam = defineExtension({ |
| 9 | + type: "Api/BuildParam", |
| 10 | + tags: { runtimeContext: "app-build", appName: "api" }, |
| 11 | + description: "Add build-time parameter to API app.", |
| 12 | + multiple: true, |
| 13 | + paramsSchema: () => { |
| 14 | + return z.object({ |
| 15 | + paramName: z.string(), |
| 16 | + value: z.union([ |
| 17 | + z.string(), |
| 18 | + z.record(z.any()), |
| 19 | + z.array(z.any()), |
| 20 | + z.number(), |
| 21 | + z.boolean() |
| 22 | + ]) |
| 23 | + }); |
| 24 | + }, |
| 25 | + async build(params, ctx) { |
| 26 | + const extensionsTsFilePath = ctx.project.paths.workspaceFolder |
| 27 | + .join("apps", "api", "graphql", "src", "extensions.ts") |
| 28 | + .toString(); |
| 29 | + |
| 30 | + const buildParamsDir = ctx.project.paths.workspaceFolder |
| 31 | + .join("apps", "api", "graphql", "src", "buildParams") |
| 32 | + .toString(); |
| 33 | + |
| 34 | + const { paramName, value } = params; |
| 35 | + |
| 36 | + // Serialize value to a TypeScript literal. |
| 37 | + const valueStr = JSON.stringify(value, null, 4); |
| 38 | + |
| 39 | + // Generate a unique class name based on the paramName. |
| 40 | + const hash = crypto.createHash("sha256").update(paramName).digest("hex"); |
| 41 | + const className = `BuildParam_${hash.slice(-10)}`; |
| 42 | + const fileName = `${className}.ts`; |
| 43 | + const filePath = path.join(buildParamsDir, fileName); |
| 44 | + |
| 45 | + // Ensure buildParams directory exists. |
| 46 | + if (!fs.existsSync(buildParamsDir)) { |
| 47 | + fs.mkdirSync(buildParamsDir, { recursive: true }); |
| 48 | + } |
| 49 | + |
| 50 | + // Check if file already exists. |
| 51 | + if (fs.existsSync(filePath)) { |
| 52 | + // File exists, just ensure it's imported in extensions.ts |
| 53 | + } else { |
| 54 | + // Create the BuildParam implementation file. |
| 55 | + const fileContent = `import { BuildParam } from "webiny/api/buildParams"; |
| 56 | +
|
| 57 | +class ${className} implements BuildParam.Interface { |
| 58 | + key = "${paramName}"; |
| 59 | + value = ${valueStr}; |
| 60 | +} |
| 61 | +
|
| 62 | +export default BuildParam.createImplementation({ |
| 63 | + implementation: ${className}, |
| 64 | + dependencies: [] |
| 65 | +}); |
| 66 | +`; |
| 67 | + fs.writeFileSync(filePath, fileContent, "utf8"); |
| 68 | + } |
| 69 | + |
| 70 | + // Now update extensions.ts to import and register this BuildParam. |
| 71 | + const project = new Project(); |
| 72 | + project.addSourceFileAtPath(extensionsTsFilePath); |
| 73 | + |
| 74 | + const source = project.getSourceFileOrThrow(extensionsTsFilePath); |
| 75 | + |
| 76 | + // Calculate import path relative to extensions.ts. |
| 77 | + let importPath = path |
| 78 | + .relative(path.dirname(extensionsTsFilePath), filePath) |
| 79 | + .replace(/\.tsx?$/, ".js"); |
| 80 | + |
| 81 | + // Ensure the path starts with ./ |
| 82 | + if (!importPath.startsWith(".")) { |
| 83 | + importPath = "./" + importPath; |
| 84 | + } |
| 85 | + |
| 86 | + // Check if import already exists. |
| 87 | + const existingImportDeclaration = source.getImportDeclaration(importPath); |
| 88 | + if (existingImportDeclaration) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + let index = 1; |
| 93 | + |
| 94 | + const importDeclarations = source.getImportDeclarations(); |
| 95 | + if (importDeclarations.length) { |
| 96 | + const last = importDeclarations[importDeclarations.length - 1]; |
| 97 | + index = last.getChildIndex() + 1; |
| 98 | + } |
| 99 | + |
| 100 | + // Add import for the BuildParam implementation. |
| 101 | + source.insertImportDeclaration(index, { |
| 102 | + defaultImport: className, |
| 103 | + moduleSpecifier: importPath |
| 104 | + }); |
| 105 | + |
| 106 | + // Add the registration to the plugins array. |
| 107 | + const pluginsArray = source.getFirstDescendant(node => |
| 108 | + Node.isArrayLiteralExpression(node) |
| 109 | + ) as ArrayLiteralExpression; |
| 110 | + |
| 111 | + pluginsArray.addElement( |
| 112 | + `\ncreateContextPlugin(ctx => {\n\tregisterExtension(ctx.container, ${className});\n})` |
| 113 | + ); |
| 114 | + |
| 115 | + { |
| 116 | + let index = 1; |
| 117 | + |
| 118 | + const importDeclarations = source.getImportDeclarations(); |
| 119 | + if (importDeclarations.length) { |
| 120 | + const last = importDeclarations[importDeclarations.length - 1]; |
| 121 | + index = last.getChildIndex() + 1; |
| 122 | + } |
| 123 | + |
| 124 | + const contextPluginImportPath = "@webiny/api/plugins/ContextPlugin"; |
| 125 | + const existingContextPluginImport = |
| 126 | + source.getImportDeclaration(contextPluginImportPath); |
| 127 | + if (!existingContextPluginImport) { |
| 128 | + source.insertImportDeclaration(index, { |
| 129 | + namedImports: ["createContextPlugin"], |
| 130 | + moduleSpecifier: contextPluginImportPath |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + await source.save(); |
| 136 | + } |
| 137 | +}); |
0 commit comments