|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +function generateWorkerCode() { |
| 5 | + const buildDir = path.join(__dirname, "../build"); |
| 6 | + const tokenListsDir = path.join(buildDir, "tokenlists"); |
| 7 | + |
| 8 | + // Get all JSON files in the tokenlists directory |
| 9 | + const tokenListFiles = fs.readdirSync(tokenListsDir) |
| 10 | + .filter(file => file.endsWith(".json")) |
| 11 | + .sort(); // Sort for consistent ordering |
| 12 | + |
| 13 | + // Generate import statements |
| 14 | + const imports = [ |
| 15 | + "import listRegistry from \"../build/listRegistry.json\";" |
| 16 | + ]; |
| 17 | + |
| 18 | + const variableNames = []; |
| 19 | + |
| 20 | + tokenListFiles.forEach((file, index) => { |
| 21 | + const baseName = file.replace(".json", ""); |
| 22 | + const variableName = `tokenList${index}`; |
| 23 | + variableNames.push({ variableName, file, baseName }); |
| 24 | + imports.push(`import ${variableName} from "../build/tokenlists/${file}";`); |
| 25 | + }); |
| 26 | + |
| 27 | + // Generate file map |
| 28 | + const fileMapEntries = [ |
| 29 | + " \"/\": listRegistry,", |
| 30 | + " \"/listRegistry.json\": listRegistry," |
| 31 | + ]; |
| 32 | + |
| 33 | + variableNames.forEach(({ variableName, file }) => { |
| 34 | + fileMapEntries.push(` "/tokenlists/${file}": ${variableName},`); |
| 35 | + }); |
| 36 | + |
| 37 | + // Generate the complete worker code |
| 38 | + const workerCode = `${imports.join("\n")} |
| 39 | +
|
| 40 | +const corsHeaders = { |
| 41 | + "Access-Control-Allow-Origin": "*", |
| 42 | + "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS", |
| 43 | + "Access-Control-Allow-Headers": "Content-Type, X-Requested-With", |
| 44 | +}; |
| 45 | +
|
| 46 | +// Auto-generated file mapping based on build directory contents |
| 47 | +const fileMap = { |
| 48 | +${fileMapEntries.join("\n")} |
| 49 | +}; |
| 50 | +
|
| 51 | +export default { |
| 52 | + async fetch(request, env, ctx) { |
| 53 | + const url = new URL(request.url); |
| 54 | +
|
| 55 | + // Handle OPTIONS requests for ALL paths |
| 56 | + if (request.method === "OPTIONS") { |
| 57 | + return new Response(null, { |
| 58 | + status: 204, |
| 59 | + headers: corsHeaders |
| 60 | + }); |
| 61 | + } |
| 62 | +
|
| 63 | + // Look up the file in our auto-generated map |
| 64 | + const fileData = fileMap[url.pathname]; |
| 65 | + if (fileData) { |
| 66 | + const json = JSON.stringify(fileData, null, 2); |
| 67 | + return new Response(json, { |
| 68 | + headers: { |
| 69 | + "content-type": "application/json;charset=UTF-8", |
| 70 | + ...corsHeaders, |
| 71 | + }, |
| 72 | + }); |
| 73 | + } |
| 74 | +
|
| 75 | + // File not found |
| 76 | + return new Response("Not Found", { status: 404 }); |
| 77 | + }, |
| 78 | +}; |
| 79 | +`; |
| 80 | + |
| 81 | + // Write the generated worker code |
| 82 | + const outputPath = path.join(__dirname, "wrangler_main.js"); |
| 83 | + fs.writeFileSync(outputPath, workerCode); |
| 84 | + |
| 85 | + console.log("✅ Generated wrangler_main.js with the following files:"); |
| 86 | + console.log(" - / → listRegistry.json"); |
| 87 | + console.log(" - /listRegistry.json → listRegistry.json"); |
| 88 | + variableNames.forEach(({ file }) => { |
| 89 | + console.log(` - /tokenlists/${file} → tokenlists/${file}`); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +// Run the generator |
| 94 | +generateWorkerCode(); |
0 commit comments