|
| 1 | +import path from "path"; |
| 2 | +import fs from "fs-extra"; |
| 3 | + |
| 4 | +const DEFAULT_WRANGLER_VERSION = "^4.61.1"; |
| 5 | +const DEFAULT_WORKERS_TYPES_VERSION = "^4.20241112.0"; |
| 6 | + |
| 7 | +function sanitizeWorkerName(name: string): string { |
| 8 | + return name |
| 9 | + .replace(/^@[^/]+\//, "") |
| 10 | + .replace(/[^a-zA-Z0-9-]/g, "-") |
| 11 | + .toLowerCase(); |
| 12 | +} |
| 13 | + |
| 14 | +function getProjectName(projectPath: string): string { |
| 15 | + try { |
| 16 | + const packageJsonPath = path.join(projectPath, "package.json"); |
| 17 | + if (fs.existsSync(packageJsonPath)) { |
| 18 | + const packageJson = fs.readJsonSync(packageJsonPath); |
| 19 | + if (packageJson.name) { |
| 20 | + return sanitizeWorkerName(packageJson.name); |
| 21 | + } |
| 22 | + } |
| 23 | + } catch { |
| 24 | + // fall back to directory name |
| 25 | + } |
| 26 | + |
| 27 | + return sanitizeWorkerName(path.basename(projectPath)); |
| 28 | +} |
| 29 | + |
| 30 | +function ensureWranglerConfig(projectPath: string): void { |
| 31 | + const wranglerTomlPath = path.join(projectPath, "wrangler.toml"); |
| 32 | + const wranglerJsoncPath = path.join(projectPath, "wrangler.jsonc"); |
| 33 | + if (fs.existsSync(wranglerTomlPath) || fs.existsSync(wranglerJsoncPath)) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const projectName = getProjectName(projectPath); |
| 38 | + const compatibilityDate = new Date().toISOString().split("T")[0]; |
| 39 | + |
| 40 | + const wranglerConfig = `{ |
| 41 | + "$schema": "node_modules/wrangler/config-schema.json", |
| 42 | + // Wrangler config generated by: create-xmcp-app --cloudflare |
| 43 | + // Docs: https://developers.cloudflare.com/workers/wrangler/configuration/ |
| 44 | + "name": ${JSON.stringify(projectName)}, |
| 45 | + "main": "worker.js", |
| 46 | + "compatibility_date": ${JSON.stringify(compatibilityDate)}, |
| 47 | + "compatibility_flags": ["nodejs_compat"], |
| 48 | +
|
| 49 | + // Observability (Workers Logs) |
| 50 | + "observability": { |
| 51 | + "enabled": true |
| 52 | + } |
| 53 | +} |
| 54 | +`; |
| 55 | + |
| 56 | + fs.writeFileSync(wranglerJsoncPath, wranglerConfig); |
| 57 | +} |
| 58 | + |
| 59 | +function ensureTsConfig(projectPath: string): void { |
| 60 | + const tsconfigPath = path.join(projectPath, "tsconfig.json"); |
| 61 | + if (!fs.existsSync(tsconfigPath)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const tsconfig = fs.readJsonSync(tsconfigPath); |
| 66 | + tsconfig.compilerOptions = tsconfig.compilerOptions ?? {}; |
| 67 | + |
| 68 | + if (!Array.isArray(tsconfig.compilerOptions.types)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const types: string[] = tsconfig.compilerOptions.types; |
| 73 | + |
| 74 | + if (!types.includes("@cloudflare/workers-types")) { |
| 75 | + types.push("@cloudflare/workers-types"); |
| 76 | + } |
| 77 | + |
| 78 | + tsconfig.compilerOptions.types = types; |
| 79 | + |
| 80 | + fs.writeJsonSync(tsconfigPath, tsconfig, { spaces: 2 }); |
| 81 | +} |
| 82 | + |
| 83 | +export function applyCloudflareSettings(projectPath: string): void { |
| 84 | + const packageJsonPath = path.join(projectPath, "package.json"); |
| 85 | + if (fs.existsSync(packageJsonPath)) { |
| 86 | + const packageJson = fs.readJsonSync(packageJsonPath); |
| 87 | + |
| 88 | + packageJson.scripts = packageJson.scripts ?? {}; |
| 89 | + packageJson.scripts.build = "xmcp build --cf"; |
| 90 | + if (!packageJson.scripts.dev || packageJson.scripts.dev === "xmcp dev") { |
| 91 | + packageJson.scripts.dev = |
| 92 | + "concurrently \"xmcp dev --cf\" \"npx wrangler dev\""; |
| 93 | + } |
| 94 | + packageJson.scripts.deploy = |
| 95 | + packageJson.scripts.deploy ?? "npx wrangler deploy"; |
| 96 | + packageJson.scripts.preview = |
| 97 | + packageJson.scripts.preview ?? "npx wrangler dev"; |
| 98 | + |
| 99 | + if (!packageJson.type) { |
| 100 | + packageJson.type = "module"; |
| 101 | + } |
| 102 | + |
| 103 | + packageJson.devDependencies = packageJson.devDependencies ?? {}; |
| 104 | + packageJson.devDependencies.concurrently = |
| 105 | + packageJson.devDependencies.concurrently ?? "^9.2.0"; |
| 106 | + packageJson.devDependencies.wrangler = |
| 107 | + packageJson.devDependencies.wrangler ?? DEFAULT_WRANGLER_VERSION; |
| 108 | + packageJson.devDependencies["@cloudflare/workers-types"] = |
| 109 | + packageJson.devDependencies["@cloudflare/workers-types"] ?? |
| 110 | + DEFAULT_WORKERS_TYPES_VERSION; |
| 111 | + |
| 112 | + fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 }); |
| 113 | + } |
| 114 | + |
| 115 | + ensureWranglerConfig(projectPath); |
| 116 | + ensureTsConfig(projectPath); |
| 117 | +} |
0 commit comments