swallowkit 1.0.0-beta.3 → 1.0.0-beta.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +21 -21
  2. package/README.ja.md +353 -215
  3. package/README.md +406 -216
  4. package/dist/__tests__/fixtures.d.ts +22 -0
  5. package/dist/__tests__/fixtures.d.ts.map +1 -0
  6. package/dist/__tests__/fixtures.js +146 -0
  7. package/dist/__tests__/fixtures.js.map +1 -0
  8. package/dist/cli/commands/add-auth.d.ts +10 -0
  9. package/dist/cli/commands/add-auth.d.ts.map +1 -0
  10. package/dist/cli/commands/add-auth.js +444 -0
  11. package/dist/cli/commands/add-auth.js.map +1 -0
  12. package/dist/cli/commands/add-connector.d.ts +20 -0
  13. package/dist/cli/commands/add-connector.d.ts.map +1 -0
  14. package/dist/cli/commands/add-connector.js +163 -0
  15. package/dist/cli/commands/add-connector.js.map +1 -0
  16. package/dist/cli/commands/create-model.d.ts +1 -4
  17. package/dist/cli/commands/create-model.d.ts.map +1 -1
  18. package/dist/cli/commands/create-model.js +21 -82
  19. package/dist/cli/commands/create-model.js.map +1 -1
  20. package/dist/cli/commands/dev-seeds.d.ts +57 -0
  21. package/dist/cli/commands/dev-seeds.d.ts.map +1 -0
  22. package/dist/cli/commands/dev-seeds.js +470 -0
  23. package/dist/cli/commands/dev-seeds.js.map +1 -0
  24. package/dist/cli/commands/dev.d.ts +33 -0
  25. package/dist/cli/commands/dev.d.ts.map +1 -1
  26. package/dist/cli/commands/dev.js +628 -146
  27. package/dist/cli/commands/dev.js.map +1 -1
  28. package/dist/cli/commands/index.d.ts +1 -0
  29. package/dist/cli/commands/index.d.ts.map +1 -1
  30. package/dist/cli/commands/index.js +3 -1
  31. package/dist/cli/commands/index.js.map +1 -1
  32. package/dist/cli/commands/init.d.ts +15 -0
  33. package/dist/cli/commands/init.d.ts.map +1 -1
  34. package/dist/cli/commands/init.js +2696 -1706
  35. package/dist/cli/commands/init.js.map +1 -1
  36. package/dist/cli/commands/scaffold.d.ts.map +1 -1
  37. package/dist/cli/commands/scaffold.js +448 -129
  38. package/dist/cli/commands/scaffold.js.map +1 -1
  39. package/dist/cli/index.d.ts +5 -1
  40. package/dist/cli/index.d.ts.map +1 -1
  41. package/dist/cli/index.js +200 -42
  42. package/dist/cli/index.js.map +1 -1
  43. package/dist/core/config.d.ts +8 -2
  44. package/dist/core/config.d.ts.map +1 -1
  45. package/dist/core/config.js +94 -5
  46. package/dist/core/config.js.map +1 -1
  47. package/dist/core/mock/connector-mock-server.d.ts +101 -0
  48. package/dist/core/mock/connector-mock-server.d.ts.map +1 -0
  49. package/dist/core/mock/connector-mock-server.js +480 -0
  50. package/dist/core/mock/connector-mock-server.js.map +1 -0
  51. package/dist/core/mock/zod-mock-generator.d.ts +14 -0
  52. package/dist/core/mock/zod-mock-generator.d.ts.map +1 -0
  53. package/dist/core/mock/zod-mock-generator.js +163 -0
  54. package/dist/core/mock/zod-mock-generator.js.map +1 -0
  55. package/dist/core/operations/create-model.d.ts +15 -0
  56. package/dist/core/operations/create-model.d.ts.map +1 -0
  57. package/dist/core/operations/create-model.js +171 -0
  58. package/dist/core/operations/create-model.js.map +1 -0
  59. package/dist/core/operations/runtime.d.ts +32 -0
  60. package/dist/core/operations/runtime.d.ts.map +1 -0
  61. package/dist/core/operations/runtime.js +225 -0
  62. package/dist/core/operations/runtime.js.map +1 -0
  63. package/dist/core/operations/scaffold-machine.d.ts +16 -0
  64. package/dist/core/operations/scaffold-machine.d.ts.map +1 -0
  65. package/dist/core/operations/scaffold-machine.js +63 -0
  66. package/dist/core/operations/scaffold-machine.js.map +1 -0
  67. package/dist/core/project/manifest.d.ts +92 -0
  68. package/dist/core/project/manifest.d.ts.map +1 -0
  69. package/dist/core/project/manifest.js +321 -0
  70. package/dist/core/project/manifest.js.map +1 -0
  71. package/dist/core/project/validation.d.ts +20 -0
  72. package/dist/core/project/validation.d.ts.map +1 -0
  73. package/dist/core/project/validation.js +209 -0
  74. package/dist/core/project/validation.js.map +1 -0
  75. package/dist/core/scaffold/auth-generator.d.ts +38 -0
  76. package/dist/core/scaffold/auth-generator.d.ts.map +1 -0
  77. package/dist/core/scaffold/auth-generator.js +1244 -0
  78. package/dist/core/scaffold/auth-generator.js.map +1 -0
  79. package/dist/core/scaffold/connector-functions-generator.d.ts +41 -0
  80. package/dist/core/scaffold/connector-functions-generator.d.ts.map +1 -0
  81. package/dist/core/scaffold/connector-functions-generator.js +1027 -0
  82. package/dist/core/scaffold/connector-functions-generator.js.map +1 -0
  83. package/dist/core/scaffold/functions-generator.d.ts +7 -1
  84. package/dist/core/scaffold/functions-generator.d.ts.map +1 -1
  85. package/dist/core/scaffold/functions-generator.js +920 -213
  86. package/dist/core/scaffold/functions-generator.js.map +1 -1
  87. package/dist/core/scaffold/model-parser.d.ts +20 -1
  88. package/dist/core/scaffold/model-parser.d.ts.map +1 -1
  89. package/dist/core/scaffold/model-parser.js +328 -135
  90. package/dist/core/scaffold/model-parser.js.map +1 -1
  91. package/dist/core/scaffold/native-schema-generator.d.ts +13 -0
  92. package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -0
  93. package/dist/core/scaffold/native-schema-generator.js +677 -0
  94. package/dist/core/scaffold/native-schema-generator.js.map +1 -0
  95. package/dist/core/scaffold/nextjs-generator.d.ts +8 -0
  96. package/dist/core/scaffold/nextjs-generator.d.ts.map +1 -1
  97. package/dist/core/scaffold/nextjs-generator.js +314 -182
  98. package/dist/core/scaffold/nextjs-generator.js.map +1 -1
  99. package/dist/core/scaffold/openapi-generator.d.ts +3 -0
  100. package/dist/core/scaffold/openapi-generator.d.ts.map +1 -0
  101. package/dist/core/scaffold/openapi-generator.js +190 -0
  102. package/dist/core/scaffold/openapi-generator.js.map +1 -0
  103. package/dist/core/scaffold/ui-generator.d.ts +10 -4
  104. package/dist/core/scaffold/ui-generator.d.ts.map +1 -1
  105. package/dist/core/scaffold/ui-generator.js +768 -663
  106. package/dist/core/scaffold/ui-generator.js.map +1 -1
  107. package/dist/database/base-model.d.ts +3 -3
  108. package/dist/database/base-model.js +3 -3
  109. package/dist/index.d.ts +2 -2
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js +2 -1
  112. package/dist/index.js.map +1 -1
  113. package/dist/machine/contracts.d.ts +16 -0
  114. package/dist/machine/contracts.d.ts.map +1 -0
  115. package/dist/machine/contracts.js +3 -0
  116. package/dist/machine/contracts.js.map +1 -0
  117. package/dist/machine/errors.d.ts +11 -0
  118. package/dist/machine/errors.d.ts.map +1 -0
  119. package/dist/machine/errors.js +34 -0
  120. package/dist/machine/errors.js.map +1 -0
  121. package/dist/machine/index.d.ts +3 -0
  122. package/dist/machine/index.d.ts.map +1 -0
  123. package/dist/machine/index.js +156 -0
  124. package/dist/machine/index.js.map +1 -0
  125. package/dist/mcp/index.d.ts +25 -0
  126. package/dist/mcp/index.d.ts.map +1 -0
  127. package/dist/mcp/index.js +184 -0
  128. package/dist/mcp/index.js.map +1 -0
  129. package/dist/types/index.d.ts +65 -0
  130. package/dist/types/index.d.ts.map +1 -1
  131. package/dist/utils/package-manager.d.ts +109 -0
  132. package/dist/utils/package-manager.d.ts.map +1 -0
  133. package/dist/utils/package-manager.js +215 -0
  134. package/dist/utils/package-manager.js.map +1 -0
  135. package/dist/utils/python-uv.d.ts +21 -0
  136. package/dist/utils/python-uv.d.ts.map +1 -0
  137. package/dist/utils/python-uv.js +111 -0
  138. package/dist/utils/python-uv.js.map +1 -0
  139. package/package.json +85 -73
  140. package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +1139 -0
  141. package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +194 -0
  142. package/src/__tests__/__snapshots__/ui-generator.test.ts.snap +532 -0
  143. package/src/__tests__/auth.test.ts +654 -0
  144. package/src/__tests__/config.test.ts +274 -0
  145. package/src/__tests__/connector-functions-generator.test.ts +288 -0
  146. package/src/__tests__/connector-mock-server.test.ts +439 -0
  147. package/src/__tests__/connector-model-bff.test.ts +162 -0
  148. package/src/__tests__/dev-seeds.test.ts +173 -0
  149. package/src/__tests__/dev.test.ts +252 -0
  150. package/src/__tests__/fixtures.ts +144 -0
  151. package/src/__tests__/functions-generator.test.ts +237 -0
  152. package/src/__tests__/init.test.ts +115 -0
  153. package/src/__tests__/machine.test.ts +251 -0
  154. package/src/__tests__/mcp.test.ts +117 -0
  155. package/src/__tests__/model-parser.test.ts +52 -0
  156. package/src/__tests__/nextjs-generator.test.ts +97 -0
  157. package/src/__tests__/openapi-generator.test.ts +43 -0
  158. package/src/__tests__/package-manager.test.ts +189 -0
  159. package/src/__tests__/python-uv.test.ts +48 -0
  160. package/src/__tests__/scaffold.test.ts +67 -0
  161. package/src/__tests__/string-utils.test.ts +75 -0
  162. package/src/__tests__/ui-generator.test.ts +144 -0
  163. package/src/__tests__/zod-mock-generator.test.ts +132 -0
  164. package/src/cli/commands/add-auth.ts +500 -0
  165. package/src/cli/commands/add-connector.ts +158 -0
  166. package/src/cli/commands/create-model.ts +62 -0
  167. package/src/cli/commands/dev-seeds.ts +614 -0
  168. package/src/cli/commands/dev.ts +1134 -0
  169. package/src/cli/commands/index.ts +9 -0
  170. package/src/cli/commands/init.ts +3480 -0
  171. package/src/cli/commands/provision.ts +193 -0
  172. package/src/cli/commands/scaffold.ts +1001 -0
  173. package/src/cli/index.ts +196 -0
  174. package/src/core/config.ts +312 -0
  175. package/src/core/mock/connector-mock-server.ts +555 -0
  176. package/src/core/mock/zod-mock-generator.ts +205 -0
  177. package/src/core/operations/create-model.ts +174 -0
  178. package/src/core/operations/runtime.ts +235 -0
  179. package/src/core/operations/scaffold-machine.ts +91 -0
  180. package/src/core/project/manifest.ts +402 -0
  181. package/src/core/project/validation.ts +229 -0
  182. package/src/core/scaffold/auth-generator.ts +1284 -0
  183. package/src/core/scaffold/connector-functions-generator.ts +1128 -0
  184. package/src/core/scaffold/functions-generator.ts +970 -0
  185. package/src/core/scaffold/model-parser.ts +841 -0
  186. package/src/core/scaffold/native-schema-generator.ts +798 -0
  187. package/src/core/scaffold/nextjs-generator.ts +370 -0
  188. package/src/core/scaffold/openapi-generator.ts +212 -0
  189. package/src/core/scaffold/ui-generator.ts +1061 -0
  190. package/src/database/base-model.ts +184 -0
  191. package/src/database/client.ts +140 -0
  192. package/src/database/repository.ts +104 -0
  193. package/src/database/runtime-check.ts +25 -0
  194. package/src/index.ts +27 -0
  195. package/src/machine/contracts.ts +17 -0
  196. package/src/machine/errors.ts +34 -0
  197. package/src/machine/index.ts +173 -0
  198. package/src/mcp/index.ts +185 -0
  199. package/src/types/index.ts +134 -0
  200. package/src/utils/package-manager.ts +229 -0
  201. package/src/utils/python-uv.ts +96 -0
@@ -0,0 +1,3480 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import { spawn, execSync } from "child_process";
4
+ import prompts from "prompts";
5
+ import { BackendLanguage } from "../../types";
6
+ import {
7
+ type PackageManager,
8
+ detectFromUserAgent,
9
+ getCommands,
10
+ getWorkspaceConfig,
11
+ getCiSetupStep,
12
+ getAzurePipelinesSetup,
13
+ getBuildScript,
14
+ getFunctionsPrestart,
15
+ getFunctionsStartScript,
16
+ } from "../../utils/package-manager";
17
+ import { syncProjectManifest } from "../../core/project/manifest";
18
+ import {
19
+ buildCSharpCodegenToolManifestSource,
20
+ buildPythonCodegenRequirementsSource,
21
+ } from "../../core/scaffold/native-schema-generator";
22
+ import { getProjectLocalUvPaths } from "../../utils/python-uv";
23
+
24
+ interface InitOptions {
25
+ name: string;
26
+ template: string;
27
+ nextVersion?: string;
28
+ cicd?: CiCdProvider;
29
+ backendLanguage?: BackendLanguage;
30
+ cosmosDbMode?: CosmosDbMode;
31
+ vnet?: VNetOption;
32
+ }
33
+
34
+ type CiCdProvider = 'github' | 'azure' | 'skip';
35
+ type CosmosDbMode = 'freetier' | 'serverless';
36
+ type VNetOption = 'none' | 'outbound';
37
+
38
+ interface AzureConfig {
39
+ cosmosDbMode: CosmosDbMode;
40
+ vnetOption: VNetOption;
41
+ }
42
+
43
+ const BACKEND_LANGUAGE_CHOICES: Array<{ title: string; value: BackendLanguage }> = [
44
+ { title: "TypeScript", value: "typescript" },
45
+ { title: "C#", value: "csharp" },
46
+ { title: "Python", value: "python" },
47
+ ];
48
+
49
+ function usesNodeFunctionsProject(backendLanguage: BackendLanguage): boolean {
50
+ return backendLanguage === "typescript";
51
+ }
52
+
53
+ function getBackendLanguageLabel(backendLanguage: BackendLanguage): string {
54
+ return BACKEND_LANGUAGE_CHOICES.find((choice) => choice.value === backendLanguage)?.title || backendLanguage;
55
+ }
56
+
57
+ function getFunctionsWorkerRuntime(backendLanguage: BackendLanguage): string {
58
+ if (backendLanguage === "csharp") {
59
+ return "dotnet-isolated";
60
+ }
61
+ if (backendLanguage === "python") {
62
+ return "python";
63
+ }
64
+ return "node";
65
+ }
66
+
67
+ function getFunctionsRuntimeConfig(backendLanguage: BackendLanguage): { name: string; version: string } {
68
+ if (backendLanguage === "csharp") {
69
+ return { name: "dotnet-isolated", version: "10.0" };
70
+ }
71
+ if (backendLanguage === "python") {
72
+ return { name: "python", version: "3.11" };
73
+ }
74
+ return { name: "node", version: "22" };
75
+ }
76
+
77
+ async function promptBackendLanguage(): Promise<BackendLanguage> {
78
+ const response = await prompts({
79
+ type: "select",
80
+ name: "backendLanguage",
81
+ message: "Azure Functions backend language:",
82
+ choices: BACKEND_LANGUAGE_CHOICES,
83
+ initial: 0,
84
+ });
85
+
86
+ return response.backendLanguage || "typescript";
87
+ }
88
+
89
+ async function promptCiCd(): Promise<CiCdProvider> {
90
+ const response = await prompts({
91
+ type: 'select',
92
+ name: 'cicd',
93
+ message: 'CI/CD Setup (choose deployment automation):',
94
+ choices: [
95
+ { title: 'GitHub Actions', value: 'github' },
96
+ { title: 'Azure Pipelines', value: 'azure' },
97
+ { title: 'Skip (manual deployment)', value: 'skip' }
98
+ ],
99
+ initial: 0
100
+ });
101
+
102
+ return response.cicd || 'skip';
103
+ }
104
+
105
+ async function promptAzureConfig(): Promise<AzureConfig> {
106
+ const cosmosResponse = await prompts({
107
+ type: 'select',
108
+ name: 'mode',
109
+ message: 'Cosmos DB mode (affects cost):',
110
+ choices: [
111
+ { title: 'Free Tier (1000 RU/s free, best for first project)', value: 'freetier' },
112
+ { title: 'Serverless (pay-per-use, flexible)', value: 'serverless' }
113
+ ],
114
+ initial: 0
115
+ });
116
+
117
+ const vnetResponse = await prompts({
118
+ type: 'select',
119
+ name: 'vnet',
120
+ message: 'Network security:',
121
+ choices: [
122
+ { title: 'VNet Integration (recommended) - Cosmos DB via Private Endpoint', value: 'outbound' },
123
+ { title: 'None - Public endpoints, simplest but least secure', value: 'none' }
124
+ ],
125
+ initial: 0
126
+ });
127
+
128
+ return {
129
+ cosmosDbMode: cosmosResponse.mode || 'freetier',
130
+ vnetOption: vnetResponse.vnet || 'outbound'
131
+ };
132
+ }
133
+
134
+ const VALID_CICD: CiCdProvider[] = ['github', 'azure', 'skip'];
135
+ const VALID_BACKEND_LANGUAGE: BackendLanguage[] = ['typescript', 'csharp', 'python'];
136
+ const VALID_COSMOS_DB_MODE: CosmosDbMode[] = ['freetier', 'serverless'];
137
+ const VALID_VNET: VNetOption[] = ['none', 'outbound'];
138
+
139
+ function validateInitFlags(options: InitOptions): void {
140
+ if (options.cicd && !VALID_CICD.includes(options.cicd)) {
141
+ console.error(`❌ Invalid --cicd value: "${options.cicd}". Must be: ${VALID_CICD.join(', ')}`);
142
+ process.exit(1);
143
+ }
144
+ if (options.backendLanguage && !VALID_BACKEND_LANGUAGE.includes(options.backendLanguage)) {
145
+ console.error(`❌ Invalid --backend-language value: "${options.backendLanguage}". Must be: ${VALID_BACKEND_LANGUAGE.join(', ')}`);
146
+ process.exit(1);
147
+ }
148
+ if (options.cosmosDbMode && !VALID_COSMOS_DB_MODE.includes(options.cosmosDbMode)) {
149
+ console.error(`❌ Invalid --cosmos-db-mode value: "${options.cosmosDbMode}". Must be: ${VALID_COSMOS_DB_MODE.join(', ')}`);
150
+ process.exit(1);
151
+ }
152
+ if (options.vnet && !VALID_VNET.includes(options.vnet)) {
153
+ console.error(`❌ Invalid --vnet value: "${options.vnet}". Must be: ${VALID_VNET.join(', ')}`);
154
+ process.exit(1);
155
+ }
156
+ }
157
+
158
+ export async function initCommand(options: InitOptions) {
159
+ // Validate flag values before doing anything
160
+ validateInitFlags(options);
161
+
162
+ console.log(`🚀 Initializing SwallowKit project: ${options.name}`);
163
+ console.log(`📋 Template: ${options.template}`);
164
+
165
+ // Detect package manager from invocation context (npx → npm, pnpm dlx → pnpm)
166
+ const pm: PackageManager = detectFromUserAgent();
167
+ const pmCmd = getCommands(pm);
168
+ console.log(`📦 Package manager: ${pm}`);
169
+
170
+ const projectDir = path.join(process.cwd(), options.name);
171
+
172
+ try {
173
+ // Check if directory already exists
174
+ if (fs.existsSync(projectDir)) {
175
+ console.error(`❌ Directory "${options.name}" already exists.`);
176
+ process.exit(1);
177
+ }
178
+
179
+ // Use flag values if provided, otherwise prompt interactively
180
+ const cicdProvider: CiCdProvider = options.cicd || await promptCiCd();
181
+ const backendLanguage: BackendLanguage = options.backendLanguage || await promptBackendLanguage();
182
+
183
+ const azureConfig: AzureConfig = (options.cosmosDbMode && options.vnet)
184
+ ? { cosmosDbMode: options.cosmosDbMode, vnetOption: options.vnet }
185
+ : await promptAzureConfig();
186
+
187
+ // Create Next.js project with create-next-app
188
+ await createNextJsProject(options.name, pm);
189
+
190
+ // Upgrade Next.js to specified version (or latest) to avoid cached old versions
191
+ await upgradeNextJs(projectDir, options.nextVersion || 'latest', pm);
192
+
193
+ // Add SwallowKit specific files
194
+ await addSwallowKitFiles(projectDir, options, cicdProvider, azureConfig, pm, backendLanguage);
195
+
196
+ // Create infrastructure files (Bicep)
197
+ await createInfrastructure(projectDir, options.name, azureConfig, backendLanguage);
198
+
199
+ // Create CI/CD files based on choice
200
+ if (cicdProvider === 'github') {
201
+ await createGitHubActionsWorkflows(projectDir, azureConfig, pm, backendLanguage);
202
+ } else if (cicdProvider === 'azure') {
203
+ await createAzurePipelines(projectDir, pm, backendLanguage);
204
+ }
205
+
206
+ await syncProjectManifest(projectDir);
207
+
208
+ // Initialize Git repository and create initial commit
209
+ try {
210
+ // Try git init with -b main (Git 2.28+), fallback to git init
211
+ try {
212
+ execSync('git init -b main', { cwd: projectDir, stdio: 'ignore' });
213
+ } catch {
214
+ // Fallback for older Git versions
215
+ execSync('git init', { cwd: projectDir, stdio: 'ignore' });
216
+ }
217
+
218
+ // Configure git user if not set (required for commits)
219
+ try {
220
+ execSync('git config user.name', { cwd: projectDir, stdio: 'pipe' });
221
+ execSync('git config user.email', { cwd: projectDir, stdio: 'pipe' });
222
+ } catch {
223
+ // Not configured globally, set locally for this repository
224
+ execSync('git config user.name "SwallowKit"', { cwd: projectDir, stdio: 'ignore' });
225
+ execSync('git config user.email "swallowkit@example.com"', { cwd: projectDir, stdio: 'ignore' });
226
+ }
227
+
228
+ execSync('git add -A', { cwd: projectDir, stdio: 'ignore' });
229
+ execSync('git commit -m "Initial commit from SwallowKit"', { cwd: projectDir, stdio: 'ignore' });
230
+
231
+ // Rename branch to main if git init -b main didn't work
232
+ try {
233
+ const currentBranch = execSync('git branch --show-current', { cwd: projectDir, encoding: 'utf-8' }).trim();
234
+ if (currentBranch !== 'main') {
235
+ execSync('git branch -M main', { cwd: projectDir, stdio: 'ignore' });
236
+ }
237
+ } catch {
238
+ // Ignore errors - branch renaming is not critical
239
+ }
240
+
241
+ console.log('✅ Git repository initialized with initial commit\n');
242
+ } catch (error) {
243
+ console.warn('⚠️ Could not initialize Git repository (is git installed?)');
244
+ if (error instanceof Error) {
245
+ console.warn(` Error: ${error.message}`);
246
+ }
247
+ }
248
+
249
+ console.log(`\n✅ Project "${options.name}" created successfully!`);
250
+ console.log("\n📝 Next steps:");
251
+ console.log(` cd ${options.name}`);
252
+ console.log(` ${pmCmd.dlx} swallowkit create-model <name> # Create your first model`);
253
+ console.log(` ${pmCmd.dlx} swallowkit scaffold shared/models/<name>.ts # Generate CRUD code`);
254
+ console.log(` ${pmCmd.dlx} swallowkit dev # Start development servers`);
255
+ console.log("\n🚀 Deploy to Azure:");
256
+ console.log(` ${pmCmd.dlx} swallowkit provision --resource-group <name>`);
257
+ if (cicdProvider !== 'skip') {
258
+ console.log(" Configure CI/CD secrets and push to repository");
259
+ }
260
+ } catch (error) {
261
+ console.error("❌ Project creation failed:", error);
262
+ // Clean up on failure
263
+ if (fs.existsSync(projectDir)) {
264
+ fs.rmSync(projectDir, { recursive: true, force: true });
265
+ }
266
+ process.exit(1);
267
+ }
268
+ }
269
+
270
+ async function createNextJsProject(projectName: string, pm: PackageManager): Promise<void> {
271
+ return new Promise((resolve, reject) => {
272
+ console.log('\n📦 Creating Next.js project with create-next-app...\n');
273
+
274
+ const pmCmd = getCommands(pm);
275
+
276
+ // Build args: for pnpm use "pnpm dlx create-next-app@latest ... --use-pnpm"
277
+ // for npm use "npx create-next-app@latest ..."
278
+ const baseArgs = pm === 'pnpm'
279
+ ? ['dlx', 'create-next-app@latest']
280
+ : ['create-next-app@latest'];
281
+
282
+ const args = [
283
+ ...baseArgs,
284
+ projectName,
285
+ '--typescript',
286
+ '--tailwind',
287
+ '--app',
288
+ '--no-src',
289
+ '--disable-git',
290
+ '--import-alias',
291
+ '@/*',
292
+ ...(pmCmd.createNextAppFlag ? [pmCmd.createNextAppFlag] : []),
293
+ '--yes'
294
+ ];
295
+
296
+ // Run create-next-app with recommended options for Azure
297
+ const createNextApp = spawn(
298
+ pm === 'pnpm' ? 'pnpm' : 'npx',
299
+ args,
300
+ {
301
+ stdio: 'inherit',
302
+ shell: true,
303
+ }
304
+ );
305
+
306
+ createNextApp.on('close', (code: number | null) => {
307
+ if (code !== 0) {
308
+ reject(new Error(`create-next-app exited with code ${code}`));
309
+ } else {
310
+ console.log('\n✅ Next.js project created\n');
311
+ resolve();
312
+ }
313
+ });
314
+
315
+ createNextApp.on('error', (error: Error) => {
316
+ reject(error);
317
+ });
318
+ });
319
+ }
320
+
321
+ async function upgradeNextJs(projectDir: string, version: string, pm: PackageManager): Promise<void> {
322
+ return new Promise((resolve, reject) => {
323
+ console.log(`\n📦 Installing Next.js ${version} (to ensure latest security patches)...\n`);
324
+
325
+ // pnpm: pnpm add next@... ; npm: npm install next@...
326
+ const args = pm === 'pnpm'
327
+ ? ['add', `next@${version}`, `react@latest`, `react-dom@latest`, '--save-exact']
328
+ : ['install', `next@${version}`, `react@latest`, `react-dom@latest`, '--save-exact'];
329
+
330
+ const child = spawn(
331
+ pm,
332
+ args,
333
+ {
334
+ cwd: projectDir,
335
+ stdio: 'inherit',
336
+ shell: true,
337
+ }
338
+ );
339
+
340
+ child.on('close', (code: number | null) => {
341
+ if (code !== 0) {
342
+ reject(new Error(`${pm} add next@${version} exited with code ${code}`));
343
+ } else {
344
+ console.log(`\n✅ Next.js ${version} installed\n`);
345
+ resolve();
346
+ }
347
+ });
348
+
349
+ child.on('error', (error: Error) => {
350
+ reject(error);
351
+ });
352
+ });
353
+ }
354
+
355
+ async function installDependencies(projectDir: string, pm: PackageManager = 'pnpm'): Promise<void> {
356
+ return new Promise((resolve, reject) => {
357
+ console.log('\n📦 Installing dependencies...\n');
358
+
359
+ const child = spawn(
360
+ pm,
361
+ ['install'],
362
+ {
363
+ cwd: projectDir,
364
+ stdio: 'inherit',
365
+ shell: true,
366
+ }
367
+ );
368
+
369
+ child.on('close', (code: number | null) => {
370
+ if (code !== 0) {
371
+ reject(new Error(`${pm} install exited with code ${code}`));
372
+ } else {
373
+ console.log('\n✅ Dependencies installed\n');
374
+ resolve();
375
+ }
376
+ });
377
+
378
+ child.on('error', (error: Error) => {
379
+ reject(error);
380
+ });
381
+ });
382
+ }
383
+
384
+ export function injectSwallowKitNextConfig(nextConfigContent: string, projectName: string): string {
385
+ return nextConfigContent.replace(
386
+ /(const\s+nextConfig[:\s]*(?::\s*NextConfig\s*)?=\s*\{)(\s*\/\*[^*]*\*\/)?/,
387
+ `$1\n output: 'standalone',\n transpilePackages: ['@${projectName}/shared'],\n serverExternalPackages: ['applicationinsights', 'diagnostic-channel-publishers'],$2`
388
+ );
389
+ }
390
+
391
+ export function buildCSharpFunctionsProgramSource(): string {
392
+ return `using Microsoft.Azure.Functions.Worker;
393
+ using Microsoft.Extensions.DependencyInjection;
394
+ using Microsoft.Extensions.Hosting;
395
+ using Microsoft.Azure.Functions.Worker.ApplicationInsights;
396
+
397
+ var host = new HostBuilder()
398
+ .ConfigureFunctionsWorkerDefaults()
399
+ .ConfigureServices(services =>
400
+ {
401
+ services.AddApplicationInsightsTelemetryWorkerService();
402
+ services.ConfigureFunctionsApplicationInsights();
403
+ })
404
+ .Build();
405
+
406
+ host.Run();
407
+ `;
408
+ }
409
+
410
+ export function buildCSharpFunctionsProjectSource(): string {
411
+ return `<Project Sdk="Microsoft.NET.Sdk">
412
+ <PropertyGroup>
413
+ <TargetFramework>net10.0</TargetFramework>
414
+ <AzureFunctionsVersion>v4</AzureFunctionsVersion>
415
+ <OutputType>Exe</OutputType>
416
+ <ImplicitUsings>enable</ImplicitUsings>
417
+ <Nullable>enable</Nullable>
418
+ </PropertyGroup>
419
+ <ItemGroup>
420
+ <Compile Remove="generated\\**\\bin\\**\\*.cs;generated\\**\\obj\\**\\*.cs" />
421
+ <EmbeddedResource Remove="generated\\**\\bin\\**;generated\\**\\obj\\**" />
422
+ <None Remove="generated\\**\\bin\\**;generated\\**\\obj\\**" />
423
+ </ItemGroup>
424
+ <ItemGroup>
425
+ <PackageReference Include="Microsoft.Azure.Cosmos" Version="3.47.0" />
426
+ <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
427
+ <PackageReference Include="Azure.Identity" Version="1.17.0" />
428
+ <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.52.0" />
429
+ <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.3.0" />
430
+ <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.7" OutputItemType="Analyzer" />
431
+ <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
432
+ <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.50.0" />
433
+ </ItemGroup>
434
+ </Project>
435
+ `;
436
+ }
437
+
438
+ export function buildSwallowKitConfigSource(backendLanguage: BackendLanguage): string {
439
+ return `module.exports = {
440
+ backend: {
441
+ language: '${backendLanguage}',
442
+ },
443
+ functions: {
444
+ baseUrl: process.env.BACKEND_FUNCTIONS_BASE_URL || process.env.FUNCTIONS_BASE_URL || 'http://localhost:7071',
445
+ },
446
+ deployment: {
447
+ resourceGroup: process.env.AZURE_RESOURCE_GROUP || '',
448
+ swaName: process.env.AZURE_SWA_NAME || '',
449
+ },
450
+ }
451
+ `;
452
+ }
453
+
454
+ export function buildGeneratedProjectDependencies(projectName: string): Record<string, string> {
455
+ return {
456
+ '@azure/cosmos': '^4.0.0',
457
+ 'applicationinsights': '^3.3.0',
458
+ [`@${projectName}/shared`]: '*',
459
+ };
460
+ }
461
+
462
+ function getSwallowKitPackageMetadata(): { name: string; version: string } {
463
+ const packageJsonPath = path.resolve(__dirname, "..", "..", "..", "package.json");
464
+
465
+ try {
466
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")) as {
467
+ name?: string;
468
+ version?: string;
469
+ };
470
+
471
+ return {
472
+ name: packageJson.name || "swallowkit",
473
+ version: packageJson.version || "latest",
474
+ };
475
+ } catch {
476
+ // Fall back to the published package name when package metadata is unavailable.
477
+ }
478
+
479
+ return {
480
+ name: "swallowkit",
481
+ version: "latest",
482
+ };
483
+ }
484
+
485
+ export function buildGeneratedProjectDevDependencies(): Record<string, string> {
486
+ const { name, version } = getSwallowKitPackageMetadata();
487
+ return {
488
+ [name]: version,
489
+ };
490
+ }
491
+
492
+ export function buildSwallowKitMcpProjectConfigSource(): string {
493
+ const { name } = getSwallowKitPackageMetadata();
494
+ return JSON.stringify(
495
+ {
496
+ mcpServers: {
497
+ swallowkit: {
498
+ command: "node",
499
+ args: [`./node_modules/${name}/dist/mcp/index.js`],
500
+ cwd: ".",
501
+ },
502
+ },
503
+ },
504
+ null,
505
+ 2
506
+ );
507
+ }
508
+
509
+ async function addSwallowKitFiles(
510
+ projectDir: string,
511
+ options: InitOptions,
512
+ cicdChoice: string,
513
+ azureConfig: AzureConfig,
514
+ pm: PackageManager,
515
+ backendLanguage: BackendLanguage
516
+ ) {
517
+ console.log('📦 Adding SwallowKit files...\n');
518
+
519
+ const projectName = options.name;
520
+
521
+ // 1. Update package.json to add runtime dependencies for generated projects
522
+ const packageJsonPath = path.join(projectDir, 'package.json');
523
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
524
+
525
+ // zod is in the shared workspace package, not here
526
+ packageJson.dependencies = {
527
+ ...packageJson.dependencies,
528
+ ...buildGeneratedProjectDependencies(projectName),
529
+ };
530
+ packageJson.devDependencies = {
531
+ ...packageJson.devDependencies,
532
+ ...buildGeneratedProjectDevDependencies(),
533
+ };
534
+
535
+ packageJson.scripts = {
536
+ ...packageJson.scripts,
537
+ 'build': getBuildScript(pm),
538
+ 'start': 'next start',
539
+ 'functions:start': getFunctionsStartScript(pm, backendLanguage),
540
+ };
541
+
542
+ if (pm === 'pnpm') {
543
+ packageJson.packageManager = 'pnpm@latest';
544
+ }
545
+
546
+ packageJson.engines = {
547
+ node: '20.x',
548
+ };
549
+
550
+ // Workspace configuration depends on package manager
551
+ const workspacePackages = usesNodeFunctionsProject(backendLanguage) ? ['shared', 'functions'] : ['shared'];
552
+ const wsConfig = getWorkspaceConfig(pm, workspacePackages);
553
+ if (wsConfig.type === 'file') {
554
+ // pnpm: workspaces are defined in pnpm-workspace.yaml
555
+ delete packageJson.workspaces;
556
+ } else {
557
+ // npm: workspaces are defined in package.json
558
+ packageJson.workspaces = wsConfig.value;
559
+ }
560
+
561
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
562
+
563
+ // Create workspace config file if needed (pnpm-workspace.yaml)
564
+ if (wsConfig.type === 'file') {
565
+ fs.writeFileSync(path.join(projectDir, wsConfig.filename), wsConfig.content);
566
+ }
567
+
568
+ // Don't install yet — wait until all workspace packages (shared, functions) are created
569
+
570
+ // 2. Update next.config to add standalone output
571
+ // Check for both .ts and .js variants
572
+ let nextConfigPath = path.join(projectDir, 'next.config.ts');
573
+ if (!fs.existsSync(nextConfigPath)) {
574
+ nextConfigPath = path.join(projectDir, 'next.config.js');
575
+ }
576
+
577
+ if (fs.existsSync(nextConfigPath)) {
578
+ let nextConfigContent = fs.readFileSync(nextConfigPath, 'utf-8');
579
+
580
+ // Add output, transpiled workspace package, and server externals for standalone deployment
581
+ if (!nextConfigContent.includes("output:") && !nextConfigContent.includes('output =')) {
582
+ nextConfigContent = injectSwallowKitNextConfig(nextConfigContent, projectName);
583
+ fs.writeFileSync(nextConfigPath, nextConfigContent);
584
+ }
585
+ }
586
+
587
+ // 3. Update tsconfig.json to exclude functions directory
588
+ const tsconfigPath = path.join(projectDir, 'tsconfig.json');
589
+ if (fs.existsSync(tsconfigPath)) {
590
+ const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath, 'utf-8'));
591
+ if (!tsconfig.exclude) {
592
+ tsconfig.exclude = [];
593
+ }
594
+ if (!tsconfig.exclude.includes('functions')) {
595
+ tsconfig.exclude.push('functions');
596
+ }
597
+ if (!tsconfig.exclude.includes('shared')) {
598
+ tsconfig.exclude.push('shared');
599
+ }
600
+ fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2));
601
+ }
602
+
603
+ // 3. Create SwallowKit config
604
+ const swallowkitConfig = buildSwallowKitConfigSource(backendLanguage);
605
+ fs.writeFileSync(path.join(projectDir, 'swallowkit.config.js'), swallowkitConfig);
606
+
607
+ // 4. Create shared workspace package for Zod models (Single Source of Truth)
608
+ await createSharedPackage(projectDir, projectName);
609
+
610
+ // Create lib directory for Next.js-specific utilities
611
+ const libDir = path.join(projectDir, 'lib');
612
+
613
+ // Create lib/api directory for backend utilities
614
+ const apiLibDir = path.join(libDir, 'api');
615
+ fs.mkdirSync(apiLibDir, { recursive: true });
616
+
617
+ // Create backend utility for calling Azure Functions
618
+ const backendUtilContent = `// Get Functions base URL at runtime (not at build time)
619
+ function getFunctionsBaseUrl(): string {
620
+ return process.env.BACKEND_FUNCTIONS_BASE_URL || process.env.FUNCTIONS_BASE_URL || 'http://localhost:7071';
621
+ }
622
+
623
+ /**
624
+ * Simple HTTP client for calling backend APIs
625
+ * Use this to make requests to BFF API routes (which forward to Azure Functions)
626
+ */
627
+ async function request<T>(
628
+ endpoint: string,
629
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE',
630
+ body?: any,
631
+ queryParams?: Record<string, string>
632
+ ): Promise<T> {
633
+ const functionsBaseUrl = getFunctionsBaseUrl();
634
+ let url = \`\${functionsBaseUrl}\${endpoint}\`;
635
+ if (queryParams) {
636
+ const params = new URLSearchParams(queryParams);
637
+ url += \`?\${params.toString()}\`;
638
+ }
639
+
640
+ try {
641
+ const response = await fetch(url, {
642
+ method,
643
+ headers: {
644
+ 'Content-Type': 'application/json',
645
+ },
646
+ body: body ? JSON.stringify(body) : undefined,
647
+ });
648
+
649
+ if (!response.ok) {
650
+ const text = await response.text();
651
+ let errorMessage = text || 'Failed to call backend function';
652
+ try {
653
+ const error = JSON.parse(text);
654
+ errorMessage = error.error || error.message || text;
655
+ } catch {
656
+ // If not JSON, use text as-is
657
+ }
658
+ throw new Error(errorMessage);
659
+ }
660
+
661
+ const contentType = response.headers.get('content-type');
662
+ if (!contentType?.includes('application/json')) {
663
+ const text = await response.text();
664
+ return text as T;
665
+ }
666
+
667
+ return await response.json();
668
+ } catch (error) {
669
+ console.error('Error calling backend:', error);
670
+ throw error;
671
+ }
672
+ }
673
+
674
+ /**
675
+ * Generic API client for making HTTP requests
676
+ * Simply calls endpoints - no DB dependencies, no schema validation
677
+ * Validation happens on the backend (BFF/Functions)
678
+ *
679
+ * @example
680
+ * // Call custom endpoint
681
+ * await api.get('/api/greet?name=World')
682
+ *
683
+ * // Call scaffolded CRUD endpoints
684
+ * await api.get('/api/todos')
685
+ * await api.post('/api/todos', { title: 'New task' })
686
+ * await api.put('/api/todos/123', { title: 'Updated' })
687
+ * await api.delete('/api/todos/123')
688
+ */
689
+ export const api = {
690
+ /**
691
+ * Make a GET request
692
+ */
693
+ get: <T>(endpoint: string, params?: Record<string, string>): Promise<T> => {
694
+ return request<T>(endpoint, 'GET', undefined, params);
695
+ },
696
+
697
+ /**
698
+ * Make a POST request
699
+ */
700
+ post: <T>(endpoint: string, body?: any): Promise<T> => {
701
+ return request<T>(endpoint, 'POST', body);
702
+ },
703
+
704
+ /**
705
+ * Make a PUT request
706
+ */
707
+ put: <T>(endpoint: string, body?: any): Promise<T> => {
708
+ return request<T>(endpoint, 'PUT', body);
709
+ },
710
+
711
+ /**
712
+ * Make a DELETE request
713
+ */
714
+ delete: <T>(endpoint: string): Promise<T> => {
715
+ return request<T>(endpoint, 'DELETE');
716
+ },
717
+ };
718
+ `;
719
+ fs.writeFileSync(path.join(apiLibDir, 'backend.ts'), backendUtilContent);
720
+
721
+ // 5. Create components directory
722
+ const componentsDir = path.join(projectDir, 'components');
723
+ fs.mkdirSync(componentsDir, { recursive: true });
724
+
725
+ // 6. Create .env.example
726
+ const envExample = `# Azure Functions Backend URL
727
+ BACKEND_FUNCTIONS_BASE_URL=http://localhost:7071
728
+
729
+ # Azure Configuration
730
+ AZURE_RESOURCE_GROUP=your-resource-group
731
+ AZURE_SWA_NAME=your-static-web-app-name
732
+ `;
733
+ fs.writeFileSync(path.join(projectDir, '.env.example'), envExample);
734
+
735
+ // 7. Create instrumentation.ts for Application Insights (Next.js official way)
736
+ const instrumentationContent = `// Application Insights instrumentation for Next.js
737
+ // This file is automatically loaded by Next.js when instrumentationHook is enabled
738
+ export async function register() {
739
+ if (process.env.NEXT_RUNTIME === 'nodejs') {
740
+ // Only run on server-side
741
+ const connectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING;
742
+
743
+ if (connectionString) {
744
+ const appInsights = await import('applicationinsights');
745
+
746
+ appInsights
747
+ .setup(connectionString)
748
+ .setAutoCollectConsole(true)
749
+ .setAutoCollectDependencies(true)
750
+ .setAutoCollectExceptions(true)
751
+ .setAutoCollectHeartbeat(true)
752
+ .setAutoCollectPerformance(true, true)
753
+ .setAutoCollectRequests(true)
754
+ .setAutoDependencyCorrelation(true)
755
+ .setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C)
756
+ .setSendLiveMetrics(true)
757
+ .setUseDiskRetryCaching(true);
758
+
759
+ appInsights.defaultClient.setAutoPopulateAzureProperties();
760
+ appInsights.start();
761
+
762
+ // Override console methods to send to Application Insights
763
+ const originalConsoleLog = console.log;
764
+ const originalConsoleError = console.error;
765
+ const originalConsoleWarn = console.warn;
766
+
767
+ console.log = function(...args: any[]) {
768
+ originalConsoleLog.apply(console, args);
769
+ const message = args.map(arg =>
770
+ typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
771
+ ).join(' ');
772
+ appInsights.defaultClient.trackTrace({
773
+ message: message,
774
+ severity: '1'
775
+ });
776
+ };
777
+
778
+ console.error = function(...args: any[]) {
779
+ originalConsoleError.apply(console, args);
780
+ const message = args.map(arg =>
781
+ typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
782
+ ).join(' ');
783
+ appInsights.defaultClient.trackTrace({
784
+ message: message,
785
+ severity: '3'
786
+ });
787
+ };
788
+
789
+ console.warn = function(...args: any[]) {
790
+ originalConsoleWarn.apply(console, args);
791
+ const message = args.map(arg =>
792
+ typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
793
+ ).join(' ');
794
+ appInsights.defaultClient.trackTrace({
795
+ message: message,
796
+ severity: '2'
797
+ });
798
+ };
799
+
800
+ console.log('[App Insights] Initialized for Next.js server-side telemetry with console override');
801
+ } else {
802
+ console.log('[App Insights] Not configured (skipped in development mode)');
803
+ }
804
+ }
805
+ }
806
+ `;
807
+ fs.writeFileSync(path.join(projectDir, 'instrumentation.ts'), instrumentationContent);
808
+
809
+ // 8. Create .env.local for local development
810
+ const envLocalContent = [
811
+ '# Azure Functions Backend URL (Local)',
812
+ 'BACKEND_FUNCTIONS_BASE_URL=http://localhost:7071',
813
+ ''
814
+ ].join('\n');
815
+ fs.writeFileSync(path.join(projectDir, '.env.local'), envLocalContent);
816
+
817
+ // 8. Create staticwebapp.config.json for Azure Static Web Apps (Next.js Hybrid Rendering)
818
+ const swaConfig = {
819
+ platform: {
820
+ apiRuntime: "node:20"
821
+ },
822
+ routes: [
823
+ {
824
+ route: "/*",
825
+ allowedRoles: ["anonymous"]
826
+ }
827
+ ],
828
+ globalHeaders: {
829
+ "X-Content-Type-Options": "nosniff",
830
+ "X-Frame-Options": "DENY",
831
+ "X-XSS-Protection": "1; mode=block"
832
+ },
833
+ mimeTypes: {
834
+ ".json": "application/json"
835
+ }
836
+ };
837
+ fs.writeFileSync(
838
+ path.join(projectDir, 'staticwebapp.config.json'),
839
+ JSON.stringify(swaConfig, null, 2)
840
+ );
841
+
842
+ // 14. Create Azure Functions project
843
+ await createAzureFunctionsProject(projectDir, pm, backendLanguage);
844
+
845
+ // 15. Create BFF API route to call Azure Functions
846
+ await createBffApiRoute(projectDir);
847
+
848
+ // 16. Create home page
849
+ await createHomePage(projectDir, pm);
850
+
851
+ // 17. Install all workspace dependencies (root + shared + functions)
852
+ console.log('📦 Installing workspace dependencies...\n');
853
+ await installDependencies(projectDir, pm);
854
+
855
+ console.log('✅ Project structure created\n');
856
+
857
+ // 18. Create README.md
858
+ createReadme(projectDir, projectName, cicdChoice, azureConfig, pm, backendLanguage);
859
+
860
+ // 19. Create AI agent instruction files (AGENTS.md, CLAUDE.md, .github/copilot-instructions.md, etc.)
861
+ createAiAgentFiles(projectDir, projectName, backendLanguage);
862
+ }
863
+
864
+ async function createSharedPackage(projectDir: string, projectName: string) {
865
+ console.log('📦 Creating shared workspace package for Zod models...\n');
866
+
867
+ const sharedDir = path.join(projectDir, 'shared');
868
+ const modelsDir = path.join(sharedDir, 'models');
869
+ fs.mkdirSync(modelsDir, { recursive: true });
870
+
871
+ // shared/package.json
872
+ const sharedPackageJson = {
873
+ name: `@${projectName}/shared`,
874
+ version: '1.0.0',
875
+ description: 'Shared Zod models — Single Source of Truth for validation schemas',
876
+ main: 'dist/index.js',
877
+ types: 'dist/index.d.ts',
878
+ scripts: {
879
+ build: 'tsc',
880
+ watch: 'tsc --watch',
881
+ },
882
+ dependencies: {
883
+ 'zod': '>=3.25.0',
884
+ },
885
+ devDependencies: {
886
+ 'typescript': '^5.0.0',
887
+ },
888
+ };
889
+ fs.writeFileSync(
890
+ path.join(sharedDir, 'package.json'),
891
+ JSON.stringify(sharedPackageJson, null, 2)
892
+ );
893
+
894
+ // shared/tsconfig.json
895
+ const sharedTsConfig = {
896
+ compilerOptions: {
897
+ target: 'ES2020',
898
+ module: 'commonjs',
899
+ moduleResolution: 'node',
900
+ lib: ['ES2020'],
901
+ outDir: 'dist',
902
+ rootDir: '.',
903
+ declaration: true,
904
+ declarationMap: true,
905
+ sourceMap: true,
906
+ strict: true,
907
+ esModuleInterop: true,
908
+ skipLibCheck: true,
909
+ forceConsistentCasingInFileNames: true,
910
+ },
911
+ include: ['index.ts', 'models/**/*'],
912
+ exclude: ['node_modules', 'dist'],
913
+ };
914
+ fs.writeFileSync(
915
+ path.join(sharedDir, 'tsconfig.json'),
916
+ JSON.stringify(sharedTsConfig, null, 2)
917
+ );
918
+
919
+ // shared/index.ts (empty re-export file, scaffold will add entries)
920
+ fs.writeFileSync(
921
+ path.join(sharedDir, 'index.ts'),
922
+ `// Shared Zod models — auto-managed by SwallowKit scaffold command\n// Do not edit the export list below manually\n`
923
+ );
924
+
925
+ // shared/.gitignore
926
+ fs.writeFileSync(
927
+ path.join(sharedDir, '.gitignore'),
928
+ `node_modules\ndist\n`
929
+ );
930
+
931
+ console.log('✅ Shared package created\n');
932
+ }
933
+
934
+ async function createAzureFunctionsProject(
935
+ projectDir: string,
936
+ pm: PackageManager = 'pnpm',
937
+ backendLanguage: BackendLanguage = 'typescript'
938
+ ) {
939
+ console.log(`📦 Creating Azure Functions project (${getBackendLanguageLabel(backendLanguage)})...\n`);
940
+
941
+ const functionsDir = path.join(projectDir, 'functions');
942
+ fs.mkdirSync(functionsDir, { recursive: true });
943
+
944
+ const projectName = path.basename(projectDir);
945
+ const databaseName = `${projectName.charAt(0).toUpperCase() + projectName.slice(1)}Database`;
946
+
947
+ createFunctionsHostFiles(functionsDir, databaseName, backendLanguage);
948
+
949
+ if (backendLanguage === 'typescript') {
950
+ createTypeScriptFunctionsProject(projectDir, functionsDir, pm);
951
+ } else if (backendLanguage === 'csharp') {
952
+ createCSharpFunctionsProject(projectDir, functionsDir);
953
+ } else {
954
+ createPythonFunctionsProject(projectDir, functionsDir);
955
+ }
956
+
957
+ console.log('✅ Azure Functions project created\n');
958
+ }
959
+
960
+ function createFunctionsHostFiles(functionsDir: string, databaseName: string, backendLanguage: BackendLanguage): void {
961
+ const hostJson = {
962
+ version: '2.0',
963
+ logging: {
964
+ applicationInsights: {
965
+ samplingSettings: {
966
+ isEnabled: true,
967
+ maxTelemetryItemsPerSecond: 20,
968
+ },
969
+ },
970
+ },
971
+ extensionBundle: {
972
+ id: 'Microsoft.Azure.Functions.ExtensionBundle',
973
+ version: '[4.0.0, 4.10.0)',
974
+ },
975
+ };
976
+ fs.writeFileSync(path.join(functionsDir, 'host.json'), JSON.stringify(hostJson, null, 2));
977
+
978
+ const localSettings = {
979
+ IsEncrypted: false,
980
+ Values: {
981
+ AzureWebJobsStorage: '',
982
+ FUNCTIONS_WORKER_RUNTIME: getFunctionsWorkerRuntime(backendLanguage),
983
+ AzureWebJobsFeatureFlags: 'EnableWorkerIndexing',
984
+ CosmosDBConnection: 'AccountEndpoint=http://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==',
985
+ COSMOS_DB_DATABASE_NAME: databaseName,
986
+ NODE_TLS_REJECT_UNAUTHORIZED: '0',
987
+ },
988
+ };
989
+ fs.writeFileSync(path.join(functionsDir, 'local.settings.json'), JSON.stringify(localSettings, null, 2));
990
+
991
+ const gitignoreLines = [
992
+ 'local.settings.json',
993
+ '*.log',
994
+ '.vscode',
995
+ '.DS_Store',
996
+ ];
997
+
998
+ if (backendLanguage === 'typescript') {
999
+ gitignoreLines.unshift('node_modules', 'dist');
1000
+ fs.writeFileSync(path.join(functionsDir, '.funcignore'), `node_modules
1001
+ .git
1002
+ .vscode
1003
+ local.settings.json
1004
+ test
1005
+ tsconfig.json
1006
+ *.ts
1007
+ !dist/**/*.js
1008
+ `);
1009
+ } else if (backendLanguage === 'python') {
1010
+ gitignoreLines.unshift('.venv', '.codegen-venv', '__pycache__', '.python_packages');
1011
+ fs.writeFileSync(path.join(functionsDir, '.funcignore'), `.venv
1012
+ .codegen-venv
1013
+ __pycache__
1014
+ .pytest_cache
1015
+ .mypy_cache
1016
+ .ruff_cache
1017
+ local.settings.json
1018
+ tests
1019
+ `);
1020
+ } else {
1021
+ gitignoreLines.unshift('bin', 'obj');
1022
+ fs.writeFileSync(path.join(functionsDir, '.funcignore'), `bin
1023
+ obj
1024
+ local.settings.json
1025
+ tests
1026
+ `);
1027
+ }
1028
+
1029
+ fs.writeFileSync(path.join(functionsDir, '.gitignore'), `${gitignoreLines.join('\n')}\n`);
1030
+ }
1031
+
1032
+ function createTypeScriptFunctionsProject(projectDir: string, functionsDir: string, pm: PackageManager): void {
1033
+ const functionsPackageJson = {
1034
+ name: 'functions',
1035
+ version: '1.0.0',
1036
+ description: 'Azure Functions backend',
1037
+ main: 'dist/*.js',
1038
+ scripts: {
1039
+ start: 'func start',
1040
+ build: 'tsc',
1041
+ prestart: getFunctionsPrestart(pm),
1042
+ },
1043
+ dependencies: {
1044
+ '@azure/functions': '~4.5.0',
1045
+ '@azure/cosmos': '^4.0.0',
1046
+ '@azure/identity': '^4.0.0',
1047
+ zod: '>=3.25.0',
1048
+ [`@${path.basename(projectDir)}/shared`]: '*',
1049
+ },
1050
+ devDependencies: {
1051
+ '@types/node': '^20.0.0',
1052
+ typescript: '^5.0.0',
1053
+ },
1054
+ };
1055
+ fs.writeFileSync(path.join(functionsDir, 'package.json'), JSON.stringify(functionsPackageJson, null, 2));
1056
+
1057
+ const sharedPkgName = `@${path.basename(projectDir)}/shared`;
1058
+ const functionsTsConfig = {
1059
+ compilerOptions: {
1060
+ target: 'ES2020',
1061
+ module: 'commonjs',
1062
+ moduleResolution: 'node',
1063
+ lib: ['ES2020'],
1064
+ outDir: 'dist',
1065
+ rootDir: 'src',
1066
+ strict: true,
1067
+ esModuleInterop: true,
1068
+ skipLibCheck: true,
1069
+ forceConsistentCasingInFileNames: true,
1070
+ paths: {
1071
+ [sharedPkgName]: ['../shared'],
1072
+ [`${sharedPkgName}/*`]: ['../shared/*'],
1073
+ },
1074
+ },
1075
+ include: ['src/**/*'],
1076
+ exclude: ['node_modules', 'dist'],
1077
+ };
1078
+ fs.writeFileSync(path.join(functionsDir, 'tsconfig.json'), JSON.stringify(functionsTsConfig, null, 2));
1079
+
1080
+ const srcDir = path.join(functionsDir, 'src');
1081
+ fs.mkdirSync(srcDir, { recursive: true });
1082
+ fs.writeFileSync(path.join(srcDir, 'greet.ts'), `import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
1083
+ import { z } from 'zod/v4';
1084
+
1085
+ const greetRequestSchema = z.object({
1086
+ name: z.string().min(1, 'Name is required').max(50, 'Name must be less than 50 characters'),
1087
+ });
1088
+
1089
+ export async function greet(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
1090
+ context.log('HTTP trigger function processed a request.');
1091
+
1092
+ try {
1093
+ const name = request.query.get('name') || (await request.text());
1094
+ const result = greetRequestSchema.safeParse({ name });
1095
+
1096
+ if (!result.success) {
1097
+ return {
1098
+ status: 400,
1099
+ jsonBody: {
1100
+ error: result.error.issues[0].message
1101
+ }
1102
+ };
1103
+ }
1104
+
1105
+ const greeting = \`Hello, \${result.data.name}! This message is from Azure Functions.\`;
1106
+
1107
+ return {
1108
+ status: 200,
1109
+ jsonBody: {
1110
+ message: greeting,
1111
+ timestamp: new Date().toISOString()
1112
+ }
1113
+ };
1114
+ } catch (error) {
1115
+ context.error('Error processing request:', error);
1116
+ return {
1117
+ status: 500,
1118
+ jsonBody: {
1119
+ error: 'Internal server error'
1120
+ }
1121
+ };
1122
+ }
1123
+ }
1124
+
1125
+ app.http('greet', {
1126
+ methods: ['GET', 'POST'],
1127
+ authLevel: 'anonymous',
1128
+ handler: greet
1129
+ });
1130
+ `);
1131
+ }
1132
+
1133
+ function createCSharpFunctionsProject(projectDir: string, functionsDir: string): void {
1134
+ const projectBaseName = path.basename(projectDir);
1135
+ const projectPascal = projectBaseName.charAt(0).toUpperCase() + projectBaseName.slice(1);
1136
+ const csprojName = `${projectPascal}.Functions.csproj`;
1137
+
1138
+ fs.writeFileSync(path.join(functionsDir, csprojName), buildCSharpFunctionsProjectSource());
1139
+ fs.mkdirSync(path.join(functionsDir, '.config'), { recursive: true });
1140
+ fs.writeFileSync(
1141
+ path.join(functionsDir, '.config', 'dotnet-tools.json'),
1142
+ buildCSharpCodegenToolManifestSource()
1143
+ );
1144
+
1145
+ fs.writeFileSync(path.join(functionsDir, 'Program.cs'), buildCSharpFunctionsProgramSource());
1146
+
1147
+ const crudDir = path.join(functionsDir, 'Crud');
1148
+ fs.mkdirSync(crudDir, { recursive: true });
1149
+ fs.writeFileSync(path.join(crudDir, 'GreetFunction.cs'), `using System.Net;
1150
+ using Microsoft.Azure.Functions.Worker;
1151
+ using Microsoft.Azure.Functions.Worker.Http;
1152
+
1153
+ namespace SwallowKit.Functions;
1154
+
1155
+ public sealed class GreetFunction
1156
+ {
1157
+ [Function("greet")]
1158
+ public async Task<HttpResponseData> Run(
1159
+ [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "greet")] HttpRequestData request)
1160
+ {
1161
+ var query = request.Url.Query.TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries);
1162
+ var name = "SwallowKit";
1163
+ foreach (var segment in query)
1164
+ {
1165
+ var parts = segment.Split('=', 2);
1166
+ if (parts.Length == 2 && parts[0] == "name")
1167
+ {
1168
+ name = Uri.UnescapeDataString(parts[1]);
1169
+ break;
1170
+ }
1171
+ }
1172
+ var response = request.CreateResponse(HttpStatusCode.OK);
1173
+ await response.WriteAsJsonAsync(new
1174
+ {
1175
+ message = $"Hello, {name}! This message is from Azure Functions.",
1176
+ timestamp = DateTimeOffset.UtcNow.ToString("O"),
1177
+ });
1178
+ return response;
1179
+ }
1180
+ }
1181
+ `);
1182
+ }
1183
+
1184
+ function createPythonFunctionsProject(projectDir: string, functionsDir: string): void {
1185
+ fs.writeFileSync(path.join(projectDir, '.python-version'), '3.11\n');
1186
+ ensureProjectGitignoreEntry(projectDir, '.uv');
1187
+
1188
+ fs.writeFileSync(path.join(functionsDir, 'requirements.txt'), `azure-functions>=1.20.0
1189
+ azure-cosmos>=4.9.0
1190
+ azure-identity>=1.19.0
1191
+ `);
1192
+ fs.writeFileSync(
1193
+ path.join(functionsDir, 'requirements.codegen.txt'),
1194
+ buildPythonCodegenRequirementsSource()
1195
+ );
1196
+
1197
+ const blueprintsDir = path.join(functionsDir, 'blueprints');
1198
+ fs.mkdirSync(blueprintsDir, { recursive: true });
1199
+ fs.writeFileSync(path.join(blueprintsDir, '__init__.py'), '');
1200
+
1201
+ fs.writeFileSync(path.join(blueprintsDir, 'greet.py'), `import json
1202
+ from datetime import datetime, timezone
1203
+
1204
+ import azure.functions as func
1205
+
1206
+ bp = func.Blueprint()
1207
+
1208
+
1209
+ @bp.route(route="greet", methods=["GET", "POST"])
1210
+ def greet(req: func.HttpRequest) -> func.HttpResponse:
1211
+ name = req.params.get("name") or "SwallowKit"
1212
+ payload = {
1213
+ "message": f"Hello, {name}! This message is from Azure Functions.",
1214
+ "timestamp": datetime.now(timezone.utc).isoformat(),
1215
+ }
1216
+ return func.HttpResponse(
1217
+ body=json.dumps(payload, ensure_ascii=False),
1218
+ status_code=200,
1219
+ mimetype="application/json",
1220
+ )
1221
+ `);
1222
+
1223
+ fs.writeFileSync(path.join(functionsDir, 'function_app.py'), `import azure.functions as func
1224
+
1225
+ from blueprints.greet import bp as greet_bp
1226
+
1227
+ app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
1228
+
1229
+ app.register_blueprint(greet_bp)
1230
+ # SwallowKit scaffold registrations
1231
+ `);
1232
+ }
1233
+
1234
+ function ensureProjectGitignoreEntry(projectDir: string, entry: string): void {
1235
+ const gitignorePath = path.join(projectDir, '.gitignore');
1236
+ if (!fs.existsSync(gitignorePath)) {
1237
+ return;
1238
+ }
1239
+
1240
+ const current = fs.readFileSync(gitignorePath, 'utf8');
1241
+ const lines = current.split(/\r?\n/);
1242
+ if (lines.includes(entry)) {
1243
+ return;
1244
+ }
1245
+
1246
+ const normalized = current.endsWith('\n') ? current : `${current}\n`;
1247
+ fs.writeFileSync(gitignorePath, `${normalized}${entry}\n`);
1248
+ }
1249
+
1250
+ async function createBffApiRoute(projectDir: string) {
1251
+ console.log('📦 Creating BFF API route...\n');
1252
+
1253
+ const apiDir = path.join(projectDir, 'app', 'api', 'greet');
1254
+ fs.mkdirSync(apiDir, { recursive: true });
1255
+
1256
+ // Create API route that calls Azure Functions using shared utility
1257
+ const apiRoute = `import { NextRequest, NextResponse } from 'next/server';
1258
+ import { api } from '@/lib/api/backend';
1259
+
1260
+ interface GreetResponse {
1261
+ message: string;
1262
+ timestamp: string;
1263
+ }
1264
+
1265
+ export async function GET(request: NextRequest) {
1266
+ try {
1267
+ const { searchParams } = new URL(request.url);
1268
+ const name = searchParams.get('name') || 'World';
1269
+
1270
+ const data = await api.get<GreetResponse>('/api/greet', { name });
1271
+
1272
+ return NextResponse.json(data);
1273
+ } catch (error) {
1274
+ console.error('Error calling Azure Functions:', error);
1275
+ const errorMessage = error instanceof Error ? error.message : 'Failed to call backend function';
1276
+ return NextResponse.json(
1277
+ { error: errorMessage, details: 'Make sure Azure Functions is running on port 7071' },
1278
+ { status: 500 }
1279
+ );
1280
+ }
1281
+ }
1282
+
1283
+ export async function POST(request: NextRequest) {
1284
+ try {
1285
+ const body = await request.json();
1286
+
1287
+ const data = await api.post<GreetResponse>('/api/greet', body);
1288
+
1289
+ return NextResponse.json(data);
1290
+ } catch (error) {
1291
+ console.error('Error calling Azure Functions:', error);
1292
+ const errorMessage = error instanceof Error ? error.message : 'Failed to call backend function';
1293
+ return NextResponse.json(
1294
+ { error: errorMessage, details: 'Make sure Azure Functions is running on port 7071' },
1295
+ { status: 500 }
1296
+ );
1297
+ }
1298
+ }
1299
+ `;
1300
+ fs.writeFileSync(path.join(apiDir, 'route.ts'), apiRoute);
1301
+
1302
+ // Update .env.example to include BACKEND_FUNCTIONS_BASE_URL
1303
+ const envExamplePath = path.join(projectDir, '.env.example');
1304
+ let envExample = fs.readFileSync(envExamplePath, 'utf-8');
1305
+
1306
+ if (!envExample.includes('BACKEND_FUNCTIONS_BASE_URL')) {
1307
+ envExample += `\n# Azure Functions Backend URL\nBACKEND_FUNCTIONS_BASE_URL=http://localhost:7071\n`;
1308
+ fs.writeFileSync(envExamplePath, envExample);
1309
+ }
1310
+
1311
+ // Update .env.local
1312
+ const envLocalPath = path.join(projectDir, '.env.local');
1313
+ let envLocal = fs.readFileSync(envLocalPath, 'utf-8');
1314
+
1315
+ if (!envLocal.includes('BACKEND_FUNCTIONS_BASE_URL')) {
1316
+ envLocal += `\n# Azure Functions Backend URL (Local)\nBACKEND_FUNCTIONS_BASE_URL=http://localhost:7071\n`;
1317
+ fs.writeFileSync(envLocalPath, envLocal);
1318
+ }
1319
+
1320
+ console.log('✅ BFF API route created\n');
1321
+ }
1322
+
1323
+ async function createHomePage(projectDir: string, pm: PackageManager = 'pnpm') {
1324
+ console.log('📦 Creating home page...\n');
1325
+
1326
+ const pmCmd = getCommands(pm);
1327
+
1328
+ const pageContent = `'use client'
1329
+
1330
+ export const dynamic = 'force-dynamic';
1331
+
1332
+ import { useState } from 'react';
1333
+ import { scaffoldConfig } from '@/lib/scaffold-config';
1334
+
1335
+ export default function Home() {
1336
+ const [greetingStatus, setGreetingStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
1337
+ const [message, setMessage] = useState('');
1338
+
1339
+ const testConnection = async () => {
1340
+ setGreetingStatus('loading');
1341
+ try {
1342
+ const response = await fetch('/api/greet?name=SwallowKit');
1343
+ const data = await response.json();
1344
+ if (!response.ok) {
1345
+ throw new Error(data.error || \`Server error: \${response.status}\`);
1346
+ }
1347
+ setMessage(data.message);
1348
+ setGreetingStatus('success');
1349
+ } catch (error) {
1350
+ setMessage(error instanceof Error ? error.message : 'Failed to connect to Azure Functions');
1351
+ setGreetingStatus('error');
1352
+ }
1353
+ };
1354
+
1355
+ return (
1356
+ <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-50 dark:from-gray-900 dark:to-gray-800">
1357
+ <div className="container mx-auto px-4 py-12">
1358
+ <header className="text-center mb-16">
1359
+ <h1 className="text-5xl font-bold text-gray-800 dark:text-white mb-4">
1360
+ Welcome to SwallowKit
1361
+ </h1>
1362
+ <p className="text-xl text-gray-600 dark:text-gray-400">
1363
+ Next.js on Azure Static Web Apps + Functions + Cosmos DB — Zod schema sharing
1364
+ </p>
1365
+ </header>
1366
+
1367
+ {/* Connection Test */}
1368
+ <section className="max-w-2xl mx-auto mb-12">
1369
+ <div className="bg-white dark:bg-gray-800 rounded-xl p-8 border border-gray-200 dark:border-gray-700">
1370
+ <h2 className="text-2xl font-semibold mb-4 text-gray-900 dark:text-gray-100">
1371
+ Test BFF → Functions Connection
1372
+ </h2>
1373
+ <button
1374
+ onClick={testConnection}
1375
+ disabled={greetingStatus === 'loading'}
1376
+ className="px-6 py-3 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-400 text-white rounded-lg font-medium transition-colors"
1377
+ >
1378
+ {greetingStatus === 'loading' ? 'Testing...' : 'Test Connection'}
1379
+ </button>
1380
+ {greetingStatus === 'success' && (
1381
+ <div className="mt-4 p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg">
1382
+ <p className="text-green-800 dark:text-green-200 font-medium">✅ Connection successful!</p>
1383
+ <p className="text-green-700 dark:text-green-300 text-sm mt-1">{message}</p>
1384
+ </div>
1385
+ )}
1386
+ {greetingStatus === 'error' && (
1387
+ <div className="mt-4 p-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg">
1388
+ <p className="text-red-800 dark:text-red-200 font-medium">❌ Connection failed</p>
1389
+ <p className="text-red-700 dark:text-red-300 text-sm mt-1">{message}</p>
1390
+ </div>
1391
+ )}
1392
+ </div>
1393
+ </section>
1394
+
1395
+ {/* Scaffolded Models Menu */}
1396
+ {scaffoldConfig.models.length > 0 ? (
1397
+ <section className="max-w-6xl mx-auto">
1398
+ <h2 className="text-3xl font-bold mb-8 text-gray-900 dark:text-gray-100">Your Models</h2>
1399
+ <div className="grid gap-6 grid-cols-1 md:grid-cols-2 lg:grid-cols-3">
1400
+ {scaffoldConfig.models.map((model) => (
1401
+ <a
1402
+ key={model.name}
1403
+ href={model.path}
1404
+ className="block p-8 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl hover:shadow-lg hover:border-blue-400 dark:hover:border-blue-600 transition-all"
1405
+ >
1406
+ <h3 className="text-2xl font-semibold mb-2 text-gray-900 dark:text-gray-100">{model.label}</h3>
1407
+ <p className="text-gray-600 dark:text-gray-400">Manage {model.label.toLowerCase()}</p>
1408
+ </a>
1409
+ ))}
1410
+ </div>
1411
+ </section>
1412
+ ) : (
1413
+ <section className="max-w-2xl mx-auto text-center">
1414
+ <div className="bg-white dark:bg-gray-800 rounded-xl p-12 border border-gray-200 dark:border-gray-700">
1415
+ <h2 className="text-2xl font-semibold mb-4 text-gray-900 dark:text-gray-100">Get Started</h2>
1416
+ <p className="text-gray-600 dark:text-gray-400 mb-6">
1417
+ Create your first model with Zod and generate CRUD operations automatically.
1418
+ </p>
1419
+ <code className="block bg-gray-100 dark:bg-gray-900 p-4 rounded text-left text-sm">
1420
+ ${pmCmd.dlx} swallowkit scaffold shared/models/your-model.ts
1421
+ </code>
1422
+ </div>
1423
+ </section>
1424
+ )}
1425
+
1426
+ <footer className="mt-16 text-center text-gray-600 dark:text-gray-400 text-sm">
1427
+ <p>Built with SwallowKit</p>
1428
+ </footer>
1429
+ </div>
1430
+ </div>
1431
+ );
1432
+ }
1433
+ `;
1434
+
1435
+ fs.writeFileSync(path.join(projectDir, 'app', 'page.tsx'), pageContent);
1436
+
1437
+ console.log('✅ Home page created\n');
1438
+
1439
+ // Create initial scaffold-config.ts
1440
+ const scaffoldConfigDir = path.join(projectDir, 'lib');
1441
+ if (!fs.existsSync(scaffoldConfigDir)) {
1442
+ fs.mkdirSync(scaffoldConfigDir, { recursive: true });
1443
+ }
1444
+
1445
+ const scaffoldConfigContent = `export interface ScaffoldModel {
1446
+ name: string;
1447
+ path: string;
1448
+ label: string;
1449
+ }
1450
+
1451
+ export const scaffoldConfig = {
1452
+ models: [
1453
+ // Scaffolded models will be added here by 'swallowkit scaffold' command
1454
+ ] as ScaffoldModel[]
1455
+ };
1456
+ `;
1457
+
1458
+ fs.writeFileSync(path.join(scaffoldConfigDir, 'scaffold-config.ts'), scaffoldConfigContent);
1459
+ console.log('✅ Scaffold config created\n');
1460
+ }
1461
+
1462
+ function createReadme(
1463
+ projectDir: string,
1464
+ projectName: string,
1465
+ cicdChoice: string,
1466
+ azureConfig: AzureConfig,
1467
+ pm: PackageManager,
1468
+ backendLanguage: BackendLanguage
1469
+ ) {
1470
+ console.log('📝 Creating README.md...\n');
1471
+
1472
+ const pmCmd = getCommands(pm);
1473
+ const cosmosDbModeLabel = azureConfig.cosmosDbMode === 'freetier' ? 'Free Tier (1000 RU/s)' : 'Serverless';
1474
+ const cicdLabel = cicdChoice === 'github' ? 'GitHub Actions' : cicdChoice === 'azure' ? 'Azure Pipelines' : 'None';
1475
+ const vnetLabel = azureConfig.vnetOption === 'none' ? 'None (public endpoints)' :
1476
+ 'Outbound VNet (Cosmos DB Private Endpoint)';
1477
+ const backendLanguageLabel = getBackendLanguageLabel(backendLanguage);
1478
+ const schemaBridgeDescription = backendLanguage === 'typescript'
1479
+ ? 'Zod (shared between frontend and backend)'
1480
+ : `Zod + OpenAPI export (Zod in shared/, native-generated ${backendLanguageLabel} schemas in functions/generated/)`;
1481
+ const functionsTree = backendLanguage === 'typescript'
1482
+ ? `│ └── src/\n│ └── greet.ts # Sample function`
1483
+ : backendLanguage === 'csharp'
1484
+ ? `│ ├── Crud/\n│ │ └── GreetFunction.cs\n│ └── generated/ # Native-generated C# schema assets`
1485
+ : `│ ├── blueprints/\n│ │ └── greet.py\n│ └── generated/ # Native-generated Python schema assets`;
1486
+ const backendScaffoldNote = backendLanguage === 'typescript'
1487
+ ? '- Azure Functions CRUD endpoints'
1488
+ : `- Azure Functions ${backendLanguageLabel} CRUD handlers\n- OpenAPI export + native-generated ${backendLanguageLabel} schema assets`;
1489
+ const pythonUvPaths = getProjectLocalUvPaths(projectDir);
1490
+ const pythonLocalDevNote = backendLanguage === 'python'
1491
+ ? `\n**Python local dev note**: SwallowKit uses \`uv\` for Python backends and keeps the managed Python runtime under \`${path.relative(projectDir, pythonUvPaths.pythonInstallDir)}\`. Local Azure Functions runs from \`functions/.venv\`, schema generation uses \`functions/.codegen-venv\`, and \`swallowkit dev\` bootstraps a project-local \`uv\` binary automatically when needed. Keep \`functions/requirements.txt\` and \`functions/requirements.codegen.txt\` as the dependency sources of truth.\n`
1492
+ : '';
1493
+
1494
+ const readme = `# ${projectName}
1495
+
1496
+ A full-stack application built with **SwallowKit** - Next.js on Azure Static Web Apps + Functions + Cosmos DB with Zod schema sharing.
1497
+
1498
+ ## 🚀 Tech Stack
1499
+
1500
+ - **Frontend**: Next.js 15 (App Router), React, TypeScript, Tailwind CSS
1501
+ - **BFF (Backend for Frontend)**: Next.js API Routes
1502
+ - **Backend**: Azure Functions (${backendLanguageLabel})
1503
+ - **Database**: Azure Cosmos DB
1504
+ - **Schema Validation**: ${schemaBridgeDescription}
1505
+ - **Infrastructure**: Bicep (Infrastructure as Code)
1506
+ - **CI/CD**: ${cicdLabel}
1507
+
1508
+ ## 📋 Project Configuration
1509
+
1510
+ This project was initialized with the following settings:
1511
+
1512
+ - **Azure Functions Plan**: Flex Consumption
1513
+ - **Cosmos DB Mode**: ${cosmosDbModeLabel}
1514
+ - **Network Security**: ${vnetLabel}
1515
+ - **CI/CD**: ${cicdLabel}
1516
+
1517
+ ## ✅ Prerequisites
1518
+
1519
+ Before you begin, ensure you have the following installed:
1520
+
1521
+ 1. **Node.js 18+**: [Download](https://nodejs.org/)${pm === 'pnpm' ? `\n2. **pnpm**: \`corepack enable\` or \`npm install -g pnpm\`` : ''}
1522
+ ${pm === 'pnpm' ? '3' : '2'}. **Azure CLI**: Required for provisioning Azure resources
1523
+ - Install: \`winget install Microsoft.AzureCLI\` (Windows)
1524
+ - Or: [Download](https://aka.ms/installazurecliwindows)
1525
+ ${pm === 'pnpm' ? '4' : '3'}. **Azure Cosmos DB Emulator**: Required for local development
1526
+ - Windows: \`winget install Microsoft.Azure.CosmosEmulator\`
1527
+ - Or: [Download](https://aka.ms/cosmosdb-emulator)
1528
+ - Docker: \`docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator\`
1529
+ ${pm === 'pnpm' ? '6' : '5'}. **Azure Functions Core Tools**: Automatically installed with project dependencies
1530
+
1531
+ ## 📁 Project Structure
1532
+
1533
+ \`\`\`
1534
+ ${projectName}/
1535
+ ├── app/ # Next.js App Router (frontend)
1536
+ │ ├── api/ # BFF API routes (proxy to Functions)
1537
+ │ └── page.tsx # Home page
1538
+ ├── functions/ # Azure Functions (backend)
1539
+ ${functionsTree}
1540
+ ├── lib/
1541
+ │ └── api/ # API client utilities
1542
+ ├── infra/ # Bicep infrastructure files
1543
+ │ ├── main.bicep
1544
+ │ └── modules/ # Bicep modules for each resource
1545
+ └── .github/workflows/ # CI/CD workflows
1546
+ \`\`\`
1547
+
1548
+ ## 🏗️ Getting Started
1549
+
1550
+ ### 1. Create Your First Model
1551
+
1552
+ Define your data model with Zod schema:
1553
+
1554
+ \`\`\`bash
1555
+ ${pmCmd.dlx} swallowkit create-model <model-name>
1556
+ \`\`\`
1557
+
1558
+ This creates a model file in \`shared/models/<model-name>.ts\`. Edit it to define your schema.
1559
+
1560
+ ### 2. Generate CRUD Code
1561
+
1562
+ Generate complete CRUD operations (Functions, API routes, UI):
1563
+
1564
+ \`\`\`bash
1565
+ ${pmCmd.dlx} swallowkit scaffold shared/models/<model-name>.ts
1566
+ \`\`\`
1567
+
1568
+ This generates:
1569
+ ${backendScaffoldNote}
1570
+ - Next.js BFF API routes
1571
+ - React UI components (list, detail, create, edit)
1572
+ - Navigation menu integration
1573
+
1574
+ ### 3. Start Development Servers
1575
+
1576
+ \`\`\`bash
1577
+ ${pmCmd.dlx} swallowkit dev
1578
+ \`\`\`
1579
+
1580
+ This starts:
1581
+ - Next.js dev server (http://localhost:3000)
1582
+ - Azure Functions (http://localhost:7071)
1583
+ - Cosmos DB Emulator check (must be running separately)
1584
+
1585
+ **Note**: You need to start Cosmos DB Emulator manually before running \`swallowkit dev\`.
1586
+ ${pythonLocalDevNote}
1587
+
1588
+ ## ☁️ Deploy to Azure
1589
+
1590
+ ### Provision Azure Resources
1591
+
1592
+ Create all required Azure resources using Bicep:
1593
+
1594
+ \`\`\`bash
1595
+ ${pmCmd.dlx} swallowkit provision --resource-group <rg-name>
1596
+ \`\`\`
1597
+
1598
+ This creates:
1599
+ - Static Web App (\`swa-${projectName}\`)
1600
+ - Azure Functions (\`func-${projectName}\`)
1601
+ - Cosmos DB (\`cosmos-${projectName}\`)
1602
+ - Storage Account
1603
+
1604
+ You will be prompted to select Azure regions:
1605
+ 1. **Primary location**: For Functions and Cosmos DB (default: Japan East)
1606
+ 2. **Static Web App location**: Limited availability (default: East Asia)
1607
+
1608
+ ### CI/CD Setup
1609
+
1610
+ ${cicdChoice === 'github' ? `#### GitHub Actions
1611
+
1612
+ 1. Get Static Web App deployment token:
1613
+ \`\`\`bash
1614
+ az staticwebapp secrets list --name swa-${projectName} --resource-group <rg-name> --query "properties.apiKey" -o tsv
1615
+ \`\`\`
1616
+
1617
+ 2. Get Function App publish profile:
1618
+ \`\`\`bash
1619
+ az webapp deployment list-publishing-profiles --name func-${projectName} --resource-group <rg-name> --xml
1620
+ \`\`\`
1621
+
1622
+ 3. Add secrets to GitHub repository:
1623
+ - \`AZURE_STATIC_WEB_APPS_API_TOKEN\`: SWA deployment token (from step 1)
1624
+ - \`AZURE_FUNCTIONAPP_NAME\`: \`func-${projectName}\`
1625
+ - \`AZURE_FUNCTIONAPP_PUBLISH_PROFILE\`: Functions publish profile (from step 2)
1626
+
1627
+ 4. Push to \`main\` branch to trigger deployment (or use **Actions** → **Run workflow** for manual deployment)` : cicdChoice === 'azure' ? `#### Azure Pipelines
1628
+
1629
+ 1. Set up service connection in Azure DevOps
1630
+ 2. Update \`azure-pipelines.yml\` with your resource names
1631
+ 3. Configure pipeline variables:
1632
+ - \`azureSubscription\`: Service connection name
1633
+ - \`resourceGroupName\`: Resource group name
1634
+ 4. Run pipeline to deploy` : `CI/CD is not configured. You can manually deploy:
1635
+
1636
+ **Deploy Static Web App:**
1637
+ \`\`\`bash
1638
+ ${pmCmd.run} build
1639
+ az staticwebapp deploy --name swa-${projectName} --resource-group <rg-name> --app-location ./
1640
+ \`\`\`
1641
+
1642
+ **Deploy Functions:**
1643
+ \`\`\`bash
1644
+ cd functions
1645
+ ${pmCmd.run} build
1646
+ func azure functionapp publish func-${projectName}
1647
+ \`\`\``}
1648
+
1649
+ ## 🔧 Available Commands
1650
+
1651
+ - \`${pmCmd.dlx} swallowkit create-model <name>\` - Create a new data model
1652
+ - \`${pmCmd.dlx} swallowkit scaffold <model-file>\` - Generate CRUD code
1653
+ - \`${pmCmd.dlx} swallowkit dev\` - Start development servers
1654
+ - \`${pmCmd.dlx} swallowkit provision -g <rg-name>\` - Provision Azure resources
1655
+ ${azureConfig.vnetOption !== 'none' ? `
1656
+ ## 🔒 Network Security (VNet Configuration)
1657
+
1658
+ This project is configured with **${vnetLabel}**.
1659
+
1660
+ ### Architecture
1661
+
1662
+ \`\`\`
1663
+ Static Web App ──(public)──> Azure Functions ──(VNet/PE)──> Cosmos DB
1664
+
1665
+ VNet Integration
1666
+ (outbound only)
1667
+ \`\`\`
1668
+
1669
+ - **Functions → Cosmos DB**: Connected via Private Endpoint (private connection)
1670
+ - **SWA → Functions**: Connected via public endpoint (secured with CORS + IP restrictions)
1671
+
1672
+ ### VNet Resources
1673
+
1674
+ | Resource | Purpose |
1675
+ |----------|---------|
1676
+ | \`vnet-${projectName}\` | Virtual Network (10.0.0.0/16) |
1677
+ | \`snet-functions\` | Functions subnet (10.0.1.0/24) |
1678
+ | \`snet-private-endpoints\` | Private Endpoints subnet (10.0.2.0/24) |
1679
+ | \`pe-cosmos-${projectName}\` | Cosmos DB Private Endpoint |
1680
+
1681
+ ### Private DNS Zones
1682
+
1683
+ - \`privatelink.documents.azure.com\` (Cosmos DB)
1684
+ ` : ''}
1685
+ ## 📚 Learn More
1686
+
1687
+ - [SwallowKit Documentation](https://github.com/himanago/swallowkit)
1688
+ - [Azure Static Web Apps](https://learn.microsoft.com/en-us/azure/static-web-apps/)
1689
+ - [Azure Functions](https://learn.microsoft.com/en-us/azure/azure-functions/)
1690
+ - [Azure Cosmos DB](https://learn.microsoft.com/en-us/azure/cosmos-db/)
1691
+ - [Next.js](https://nextjs.org/)
1692
+ - [Zod](https://zod.dev/)
1693
+
1694
+ ## 💭 Feedback
1695
+
1696
+ This project was generated by SwallowKit. If you encounter any issues or have suggestions for improvements, please open an issue on the [SwallowKit repository](https://github.com/himanago/swallowkit).
1697
+ `;
1698
+
1699
+ fs.writeFileSync(path.join(projectDir, 'README.md'), readme);
1700
+ console.log('✅ README.md created\n');
1701
+ }
1702
+
1703
+ function createAiAgentFiles(projectDir: string, projectName: string, backendLanguage: BackendLanguage) {
1704
+ console.log('🤖 Creating AI agent instruction files...\n');
1705
+ const backendLanguageLabel = getBackendLanguageLabel(backendLanguage);
1706
+ const projectMcpConfigSource = buildSwallowKitMcpProjectConfigSource();
1707
+ const functionsStructureLine = backendLanguage === 'typescript'
1708
+ ? `│ └── src/ # HTTP trigger handlers with Cosmos DB bindings`
1709
+ : backendLanguage === 'csharp'
1710
+ ? `│ ├── Crud/ # C# HTTP trigger handlers\n│ └── generated/ # Native-generated C# schema assets`
1711
+ : `│ ├── blueprints/ # Python HTTP trigger handlers\n│ └── generated/ # Native-generated Python schema assets`;
1712
+ const backendSchemaNote = backendLanguage === 'typescript'
1713
+ ? `- The shared package (\`@${projectName}/shared\`) is consumed by both Next.js and Azure Functions as a workspace dependency.`
1714
+ : `- The frontend/BFF source of truth stays in \`shared/models/\` as Zod schemas.\n- \`swallowkit scaffold\` exports OpenAPI into \`functions/openapi/\` and generates ${backendLanguageLabel} schema assets into \`functions/generated/\` with native ${backendLanguageLabel} tooling.`;
1715
+ const backendRulesNote = backendLanguage === 'typescript'
1716
+ ? `- All CRUD operations and business logic live in \`functions/src/\`.\n- Use Azure Functions Cosmos DB **input/output bindings** (\`extraInputs\`/\`extraOutputs\`) for reads and writes.\n- Use the Cosmos DB SDK client directly **only** for delete operations (bindings do not support delete).\n- Validate all data against Zod schemas before writing to Cosmos DB.\n- The backend auto-generates \`id\` (UUID), \`createdAt\`, and \`updatedAt\` — never trust client-sent values for these fields.`
1717
+ : `- All business logic lives in \`functions/\` and the generated handlers perform real Cosmos DB CRUD.\n- Keep Zod schemas in \`shared/models/\` as the source of truth.\n- Regenerate backend contracts with \`swallowkit scaffold shared/models/<name>.ts\` whenever a schema changes.\n- Use the native-generated schema assets in \`functions/generated/\` to keep backend contracts aligned.\n- The backend should still own \`id\`, \`createdAt\`, and \`updatedAt\`.`;
1718
+
1719
+ // ── 1. AGENTS.md (Codex / generic agents) ──────────────────────────
1720
+
1721
+ const agentsMd = `# AGENTS.md
1722
+
1723
+ This project was generated by **SwallowKit**.
1724
+ All coding agents **must** follow the architecture and conventions described below.
1725
+
1726
+ ## Architecture Overview
1727
+
1728
+ This is a full-stack application deployed on Azure with a TypeScript frontend/BFF and an Azure Functions backend in ${backendLanguageLabel}.
1729
+
1730
+ \`\`\`
1731
+ Frontend (React / Next.js App Router)
1732
+ ↓ fetch('/api/{model}', ...)
1733
+ BFF Layer (Next.js API Routes)
1734
+ ↓ HTTP → Azure Functions
1735
+ Backend (Azure Functions)
1736
+
1737
+ Azure Cosmos DB (Document Database)
1738
+ \`\`\`
1739
+
1740
+ ### Project Structure
1741
+
1742
+ \`\`\`
1743
+ ${projectName}/
1744
+ ├── app/ # Next.js App Router
1745
+ │ ├── api/ # BFF API routes (proxy to Azure Functions)
1746
+ │ └── {model}/ # UI pages per model (list, detail, create, edit)
1747
+ ├── functions/ # Azure Functions (backend)
1748
+ ${functionsStructureLine}
1749
+ ├── shared/ # Shared workspace package
1750
+ │ ├── models/ # Zod schema definitions (single source of truth)
1751
+ │ └── index.ts # Re-exports all models
1752
+ ├── lib/
1753
+ │ └── api/ # API client utilities (backend.ts, call-function.ts)
1754
+ ├── components/ # Shared React components
1755
+ ├── infra/ # Bicep infrastructure-as-code files
1756
+ │ ├── main.bicep
1757
+ │ └── modules/
1758
+ ├── .mcp.json # Project-scoped MCP bootstrap using local installed SwallowKit
1759
+ └── .github/workflows/ # CI/CD workflows (if configured)
1760
+ \`\`\`
1761
+
1762
+ ## SwallowKit MCP / Machine Workflow
1763
+
1764
+ - This repository includes a project-scoped \`.mcp.json\` file that starts the locally installed SwallowKit MCP server on runtimes that auto-load project MCP configurations.
1765
+ - Prefer the \`swallowkit_*\` MCP tools for framework-owned inspection, validation, and generation when they are available.
1766
+ - If MCP is unavailable in your runtime, fall back to the machine CLI:
1767
+ - \`npx swallowkit machine inspect project\`
1768
+ - \`npx swallowkit machine validate project\`
1769
+ - \`npx swallowkit machine generate scaffold <name> --api-only\`
1770
+ - Do not hand-edit framework-owned artifacts when the MCP or machine interface can generate or validate them for you.
1771
+ - The local MCP bootstrap depends on project dependencies already being installed.
1772
+
1773
+ ## Critical Design Principles
1774
+
1775
+ ### 1. Next.js API Routes Are Strictly a BFF (Backend for Frontend)
1776
+
1777
+ - \`app/api/\` routes exist **only** to proxy requests to Azure Functions.
1778
+ - **Never** place business logic, database access, or direct Cosmos DB calls in Next.js API routes.
1779
+ - The BFF layer may validate input/output with Zod schemas before forwarding to Functions.
1780
+ - Use the \`callFunction\` helper (\`lib/api/call-function.ts\`) or the \`api\` client (\`lib/api/backend.ts\`) to call Azure Functions.
1781
+
1782
+ Example BFF route pattern:
1783
+
1784
+ \`\`\`typescript
1785
+ // app/api/{model}/route.ts
1786
+ import { callFunction } from '@/lib/api/call-function';
1787
+ import { ModelSchema } from '@${projectName}/shared';
1788
+ import { z } from 'zod/v4';
1789
+
1790
+ export async function GET() {
1791
+ return callFunction({
1792
+ method: 'GET',
1793
+ path: '/api/{model}',
1794
+ responseSchema: z.array(ModelSchema),
1795
+ });
1796
+ }
1797
+
1798
+ export async function POST(request: NextRequest) {
1799
+ const body = await request.json();
1800
+ return callFunction({
1801
+ method: 'POST',
1802
+ path: '/api/{model}',
1803
+ body,
1804
+ inputSchema: ModelSchema.omit({ id: true, createdAt: true, updatedAt: true }),
1805
+ responseSchema: ModelSchema,
1806
+ successStatus: 201,
1807
+ });
1808
+ }
1809
+ \`\`\`
1810
+
1811
+ ### 2. Zod Schemas Are the Single Source of Truth
1812
+
1813
+ - All data models are defined **once** as Zod schemas in \`shared/models/\`.
1814
+ - TypeScript types are derived with \`z.infer<typeof Schema>\` — never define types separately.
1815
+ - ${backendSchemaNote}
1816
+
1817
+ Model definition pattern:
1818
+
1819
+ \`\`\`typescript
1820
+ // shared/models/{model}.ts
1821
+ import { z } from 'zod/v4';
1822
+
1823
+ export const Todo = z.object({
1824
+ id: z.string(),
1825
+ name: z.string().min(1),
1826
+ // ... your fields
1827
+ createdAt: z.string().optional(),
1828
+ updatedAt: z.string().optional(),
1829
+ });
1830
+
1831
+ export type Todo = z.infer<typeof Todo>;
1832
+ export const displayName = 'Todo';
1833
+ \`\`\`
1834
+
1835
+ Key rules:
1836
+ - Use the **Zod official pattern**: the schema constant and the TypeScript type share the same name.
1837
+ - \`id\`, \`createdAt\`, and \`updatedAt\` are auto-managed by the backend. Mark them as \`optional()\` in the schema.
1838
+ - Always re-export models from \`shared/index.ts\`.
1839
+
1840
+ ### 3. Azure Functions Own All Business Logic and Data Access
1841
+
1842
+ - ${backendRulesNote}
1843
+
1844
+ ${backendLanguage === 'typescript' ? 'Azure Functions handler pattern:' : `Generated ${backendLanguageLabel} handlers live under \`functions/\`. Re-run \`swallowkit scaffold shared/models/<name>.ts\` after schema changes to keep generated CRUD handlers and the native schema assets under \`functions/generated/\` in sync.`}
1845
+
1846
+ ${backendLanguage === 'typescript' ? `\`\`\`typescript
1847
+ // functions/src/{model}.ts
1848
+ import { app } from '@azure/functions';
1849
+ import { ModelSchema } from '@${projectName}/shared';
1850
+
1851
+ const containerName = 'Models'; // PascalCase + 's'
1852
+
1853
+ app.http('{model}-get-all', {
1854
+ methods: ['GET'],
1855
+ route: '{model}',
1856
+ authLevel: 'anonymous',
1857
+ extraInputs: [{ type: 'cosmosDB', name: 'cosmosInput', containerName, ... }],
1858
+ handler: async (request, context) => {
1859
+ const documents = context.extraInputs.get('cosmosInput');
1860
+ const validated = z.array(ModelSchema).parse(documents);
1861
+ return { status: 200, jsonBody: validated };
1862
+ },
1863
+ });
1864
+ \`\`\`` : ''}
1865
+
1866
+ ## Naming Conventions
1867
+
1868
+ | Item | Convention | Example |
1869
+ |------|-----------|---------|
1870
+ | Model schema file | \`shared/models/{kebab-case}.ts\` | \`shared/models/todo.ts\` |
1871
+ | Schema/type name | PascalCase (same name for both) | \`export const Todo = z.object({...}); export type Todo = z.infer<typeof Todo>;\` |
1872
+ | Functions handler file | backend-language specific under \`functions/\` | \`${backendLanguage === 'typescript' ? 'functions/src/todo.ts' : backendLanguage === 'csharp' ? 'functions/Crud/TodoFunctions.cs' : 'functions/blueprints/todo.py'}\` |
1873
+ | Functions handler name | \`{camelCase}-{operation}\` | \`todo-get-all\`, \`todo-create\` |
1874
+ | API route path | \`/api/{camelCase}\` | \`/api/todo\`, \`/api/todo/{id}\` |
1875
+ | BFF route file | \`app/api/{kebab-case}/route.ts\` | \`app/api/todo/route.ts\` |
1876
+ | BFF detail route | \`app/api/{kebab-case}/[id]/route.ts\` | \`app/api/todo/[id]/route.ts\` |
1877
+ | UI page directory | \`app/{kebab-case}/\` | \`app/todo/page.tsx\` |
1878
+ | React component | PascalCase | \`TodoForm.tsx\` |
1879
+ | Cosmos DB container | PascalCase + 's' | \`Todos\` |
1880
+ | Cosmos DB partition key | \`/id\` (default) | Custom: \`export const partitionKey = '/field'\` |
1881
+ | Bicep container file | \`infra/containers/{kebab-case}-container.bicep\` | \`infra/containers/todo-container.bicep\` |
1882
+
1883
+ ## Adding New Models (SwallowKit CLI Skills)
1884
+
1885
+ Use the SwallowKit CLI — do **not** manually create model files or CRUD boilerplate.
1886
+
1887
+ ### Skill: Create a new data model
1888
+
1889
+ \`\`\`bash
1890
+ npx swallowkit create-model <name>
1891
+ # Multiple models at once:
1892
+ npx swallowkit create-model user post comment
1893
+ \`\`\`
1894
+
1895
+ Creates \`shared/models/<name>.ts\` with a Zod schema template including \`id\`, \`createdAt\`, \`updatedAt\`.
1896
+ Edit the generated file to add your domain-specific fields, then run scaffold.
1897
+
1898
+ ### Skill: Generate full CRUD from a model
1899
+
1900
+ \`\`\`bash
1901
+ npx swallowkit scaffold shared/models/<name>.ts
1902
+ \`\`\`
1903
+
1904
+ Generates:
1905
+ - Azure Functions handlers (${backendLanguage === 'typescript' ? '`functions/src/<name>.ts`' : '`functions/` language-specific CRUD files + `functions/generated/` schema assets'})
1906
+ - BFF API routes (\`app/api/<name>/route.ts\`, \`app/api/<name>/[id]/route.ts\`)
1907
+ - UI pages (\`app/<name>/page.tsx\`, detail, create, edit pages)
1908
+ - Cosmos DB Bicep container config (\`infra/containers/<name>-container.bicep\`)
1909
+
1910
+ ### Skill: Start development servers
1911
+
1912
+ \`\`\`bash
1913
+ npx swallowkit dev
1914
+ \`\`\`
1915
+
1916
+ Runs Next.js (http://localhost:3000) and Azure Functions (http://localhost:7071) concurrently.
1917
+ Checks for Cosmos DB Emulator availability.
1918
+
1919
+ ### Skill: Provision Azure resources
1920
+
1921
+ \`\`\`bash
1922
+ npx swallowkit provision --resource-group <name> --location <region>
1923
+ \`\`\`
1924
+
1925
+ Deploys Bicep infrastructure: Static Web Apps, Functions, Cosmos DB, Storage, Managed Identity.
1926
+
1927
+ ### Typical workflow for "add a new feature/model"
1928
+
1929
+ 1. \`npx swallowkit create-model <name>\`
1930
+ 2. Edit \`shared/models/<name>.ts\` — add fields
1931
+ 3. \`npx swallowkit scaffold shared/models/<name>.ts\`
1932
+ 4. \`npx swallowkit dev\` — verify at http://localhost:3000/<name>
1933
+
1934
+ ## Do NOT
1935
+
1936
+ - **Do not** put business logic or database calls in \`app/api/\` routes. They are BFF only.
1937
+ - **Do not** define TypeScript interfaces/types separately from Zod schemas. Always derive types with \`z.infer<>\`.
1938
+ - **Do not** manually duplicate model definitions across layers. Use the shared package.
1939
+ - **Do not** manually create CRUD boilerplate. Use \`swallowkit scaffold\`.
1940
+ - **Do not** hardcode Cosmos DB connection strings. Use Managed Identity (\`CosmosDBConnection__accountEndpoint\`) in production and emulator settings locally.
1941
+ - By default, all containers use \`/id\` as the partition key. To use a custom partition key, add \`export const partitionKey = '/yourField'\` to the model file. The scaffold command will apply it across all layers.
1942
+
1943
+ ## Technology Stack
1944
+
1945
+ - **Frontend**: Next.js (App Router), React, TypeScript, Tailwind CSS
1946
+ - **BFF**: Next.js API Routes (proxy only)
1947
+ - **Backend**: Azure Functions (${backendLanguageLabel})
1948
+ - **Database**: Azure Cosmos DB (NoSQL)
1949
+ - **Schema**: Zod (shared across all layers via workspace package)
1950
+ - **Infrastructure**: Bicep (IaC)
1951
+ - **Hosting**: Azure Static Web Apps (frontend) + Azure Functions Flex Consumption (backend)
1952
+ - **Auth**: Azure Managed Identity (no connection strings in production)
1953
+ - **Monitoring**: Application Insights
1954
+ `;
1955
+
1956
+ fs.writeFileSync(path.join(projectDir, 'AGENTS.md'), agentsMd);
1957
+ console.log(' ✅ AGENTS.md (Codex / generic agents)');
1958
+
1959
+ // ── 2. CLAUDE.md (Claude Code) ─────────────────────────────────────
1960
+
1961
+ const claudeMd = `# CLAUDE.md
1962
+
1963
+ This file is for Claude Code. Read AGENTS.md in the project root for the full architecture, conventions, and rules.
1964
+
1965
+ ## Quick Reference
1966
+
1967
+ - **Architecture**: Next.js (frontend) → BFF (API routes, proxy only) → Azure Functions (backend) → Cosmos DB
1968
+ - **Schema**: Zod schemas in \`shared/models/\` are the single source of truth. Never define types separately.
1969
+ - **BFF rule**: \`app/api/\` routes must ONLY proxy to Azure Functions via \`callFunction()\`. No business logic.
1970
+ - **Backend language**: ${backendLanguageLabel}
1971
+ - **Backend rule**: Regenerate backend contracts with \`swallowkit scaffold\` after schema changes and keep \`functions/generated/\` in sync.
1972
+
1973
+ ## SwallowKit MCP
1974
+
1975
+ - This repository includes a project-scoped \`.mcp.json\` that registers the locally installed SwallowKit MCP server for runtimes that support project MCP files.
1976
+ - When the \`swallowkit_*\` tools are available, prefer them for inspect / validate / generate tasks.
1977
+ - If MCP is unavailable, use \`npx swallowkit machine ...\` instead.
1978
+
1979
+ ## SwallowKit CLI Commands
1980
+
1981
+ | Task | Command |
1982
+ |------|---------|
1983
+ | Create model | \`npx swallowkit create-model <name>\` |
1984
+ | Generate CRUD | \`npx swallowkit scaffold shared/models/<name>.ts\` |
1985
+ | Dev servers | \`npx swallowkit dev\` |
1986
+ | Provision Azure | \`npx swallowkit provision --resource-group <rg> --location <region>\` |
1987
+
1988
+ ## Workflow: Add a new model
1989
+
1990
+ 1. \`npx swallowkit create-model <name>\`
1991
+ 2. Edit \`shared/models/<name>.ts\` — add your fields
1992
+ 3. \`npx swallowkit scaffold shared/models/<name>.ts\`
1993
+ 4. \`npx swallowkit dev\` — verify at http://localhost:3000/<name>
1994
+ `;
1995
+
1996
+ fs.writeFileSync(path.join(projectDir, 'CLAUDE.md'), claudeMd);
1997
+ console.log(' ✅ CLAUDE.md (Claude Code)');
1998
+
1999
+ fs.writeFileSync(path.join(projectDir, '.mcp.json'), projectMcpConfigSource);
2000
+ console.log(' ✅ .mcp.json (project-scoped MCP bootstrap)');
2001
+
2002
+ // ── 3. .github/copilot-instructions.md (GitHub Copilot) ────────────
2003
+
2004
+ const ghDir = path.join(projectDir, '.github');
2005
+ fs.mkdirSync(ghDir, { recursive: true });
2006
+
2007
+ const copilotInstructions = `# Copilot Instructions
2008
+
2009
+ This project was generated by **SwallowKit**. See \`AGENTS.md\` in the project root for the full specification.
2010
+
2011
+ ## Architecture (3-layer)
2012
+
2013
+ \`\`\`
2014
+ Frontend (Next.js App Router) → BFF (Next.js API Routes) → Backend (Azure Functions) → Cosmos DB
2015
+ \`\`\`
2016
+
2017
+ ## Key Rules
2018
+
2019
+ 1. **BFF is proxy only** — \`app/api/\` routes call Azure Functions via \`callFunction()\`. No business logic, no direct DB access.
2020
+ 2. **Zod = single source of truth** — Models live in \`shared/models/\`. Types are derived with \`z.infer<>\`. Never define types separately.
2021
+ 3. **Backend owns data** — All CRUD and business logic stay in \`functions/\`, and generated contract assets under \`functions/generated/\` must stay aligned with \`shared/models/\`.
2022
+ 4. **Use the CLI** — Run \`npx swallowkit create-model <name>\` then \`npx swallowkit scaffold shared/models/<name>.ts\` to add models. Do not create boilerplate manually.
2023
+
2024
+ ## SwallowKit Framework Operations
2025
+
2026
+ - Prefer the SwallowKit MCP or machine interface for framework-owned inspection, validation, and generation instead of hand-editing generated files.
2027
+ - If your runtime loads project-scoped MCP config from \`.mcp.json\`, use the \`swallowkit_*\` tools.
2028
+ - Otherwise use \`npx swallowkit machine inspect project\`, \`npx swallowkit machine validate project\`, and \`npx swallowkit machine generate scaffold <name> --api-only\`.
2029
+
2030
+ ## Naming
2031
+
2032
+ - Schema/type: PascalCase, same name for both (\`export const Todo = z.object({...}); export type Todo = z.infer<typeof Todo>;\`)
2033
+ - Files: kebab-case (\`shared/models/todo.ts\`, backend handlers under \`functions/\`)
2034
+ - Cosmos DB containers: PascalCase + 's' (\`Todos\`), partition key default \`/id\` (customizable via \`export const partitionKey\`)
2035
+
2036
+ ## Managed Fields
2037
+
2038
+ \`id\`, \`createdAt\`, \`updatedAt\` are auto-managed by the backend. Define them as \`optional()\` in schemas. Never trust client-sent values.
2039
+ `;
2040
+
2041
+ fs.writeFileSync(path.join(ghDir, 'copilot-instructions.md'), copilotInstructions);
2042
+ console.log(' ✅ .github/copilot-instructions.md (GitHub Copilot)');
2043
+
2044
+ // ── 4. .github/instructions/*.instructions.md (Copilot layer-specific) ──
2045
+
2046
+ const instructionsDir = path.join(ghDir, 'instructions');
2047
+ fs.mkdirSync(instructionsDir, { recursive: true });
2048
+
2049
+ // 4a. shared/models — Zod schema layer
2050
+ const sharedModelsInstructions = `---
2051
+ applyTo: "shared/models/**"
2052
+ ---
2053
+
2054
+ # Shared Models — Zod Schema Rules
2055
+
2056
+ Files in this directory are the **single source of truth** for data models across the entire application.
2057
+
2058
+ ## Rules
2059
+
2060
+ - Define Zod schemas using \`zod/v4\` (\`import { z } from 'zod/v4'\`).
2061
+ - Use the **Zod official pattern**: the schema constant and the TypeScript type share the same name.
2062
+ \`\`\`typescript
2063
+ export const Todo = z.object({ ... });
2064
+ export type Todo = z.infer<typeof Todo>;
2065
+ \`\`\`
2066
+ - Always include \`id: z.string()\`, \`createdAt: z.string().optional()\`, \`updatedAt: z.string().optional()\`. These are managed by the backend.
2067
+ - Export a \`displayName\` string constant for UI display.
2068
+ - Re-export every model from \`shared/index.ts\`.
2069
+ - For relationships, use **nested schemas** (import and embed the related schema), not ID references.
2070
+ - After editing a model, run \`npx swallowkit scaffold shared/models/<name>.ts\` to regenerate CRUD code.
2071
+ `;
2072
+
2073
+ fs.writeFileSync(
2074
+ path.join(instructionsDir, 'shared-models.instructions.md'),
2075
+ sharedModelsInstructions
2076
+ );
2077
+
2078
+ // 4b. app/api — BFF layer
2079
+ const bffInstructions = `---
2080
+ applyTo: "app/api/**"
2081
+ ---
2082
+
2083
+ # BFF API Routes — Rules
2084
+
2085
+ Files in \`app/api/\` are the **BFF (Backend for Frontend)** layer. They exist solely to proxy requests to Azure Functions.
2086
+
2087
+ ## Rules
2088
+
2089
+ - **Never** put business logic, database access, or direct Cosmos DB calls here.
2090
+ - Use \`callFunction()\` from \`@/lib/api/call-function\` to forward requests to Azure Functions.
2091
+ - You may validate input/output with Zod schemas before forwarding.
2092
+ - Import schemas from \`@${projectName}/shared\`.
2093
+
2094
+ ## Pattern
2095
+
2096
+ \`\`\`typescript
2097
+ import { callFunction } from '@/lib/api/call-function';
2098
+ import { ModelSchema } from '@${projectName}/shared';
2099
+ import { z } from 'zod/v4';
2100
+
2101
+ export async function GET() {
2102
+ return callFunction({
2103
+ method: 'GET',
2104
+ path: '/api/{model}',
2105
+ responseSchema: z.array(ModelSchema),
2106
+ });
2107
+ }
2108
+ \`\`\`
2109
+ `;
2110
+
2111
+ fs.writeFileSync(
2112
+ path.join(instructionsDir, 'bff-routes.instructions.md'),
2113
+ bffInstructions
2114
+ );
2115
+
2116
+ // 4c. functions — Azure Functions backend layer
2117
+ const functionsInstructions = `---
2118
+ applyTo: "functions/**"
2119
+ ---
2120
+
2121
+ # Azure Functions — Backend Rules
2122
+
2123
+ Files in \`functions/\` contain all business logic and data access for this application.
2124
+
2125
+ ## Rules
2126
+
2127
+ - Keep backend contracts aligned with \`shared/models/\` by rerunning \`swallowkit scaffold\` after schema changes.
2128
+ - For TypeScript backends, use Cosmos DB **input/output bindings** (\`extraInputs\`/\`extraOutputs\`) for reads and writes.
2129
+ - For C#/Python backends, consume the native-generated assets in \`functions/generated/\`.
2130
+ - Auto-generate \`id\` (UUID), \`createdAt\`, and \`updatedAt\` on the backend. Never trust client-sent values.
2131
+ - Container names are PascalCase + 's' (e.g., \`Todos\`). Partition key defaults to \`/id\` but can be customized per model.
2132
+
2133
+ ## Handler Pattern
2134
+
2135
+ \`\`\`typescript
2136
+ import { app } from '@azure/functions';
2137
+ import { ModelSchema } from '@${projectName}/shared';
2138
+
2139
+ app.http('{model}-get-all', {
2140
+ methods: ['GET'],
2141
+ route: '{model}',
2142
+ authLevel: 'anonymous',
2143
+ extraInputs: [cosmosInput],
2144
+ handler: async (request, context) => {
2145
+ const documents = context.extraInputs.get(cosmosInput);
2146
+ const validated = z.array(ModelSchema).parse(documents);
2147
+ return { status: 200, jsonBody: validated };
2148
+ },
2149
+ });
2150
+ \`\`\`
2151
+ `;
2152
+
2153
+ fs.writeFileSync(
2154
+ path.join(instructionsDir, 'azure-functions.instructions.md'),
2155
+ functionsInstructions
2156
+ );
2157
+
2158
+ console.log(' ✅ .github/instructions/ (Copilot layer-specific instructions)');
2159
+ console.log(' - shared-models.instructions.md');
2160
+ console.log(' - bff-routes.instructions.md');
2161
+ console.log(' - azure-functions.instructions.md');
2162
+
2163
+ console.log('\n✅ AI agent files created\n');
2164
+ console.log(' Supported agents:');
2165
+ console.log(' - OpenAI Codex → AGENTS.md');
2166
+ console.log(' - Claude Code → CLAUDE.md (+ AGENTS.md)');
2167
+ console.log(' - Project MCP runtimes → .mcp.json');
2168
+ console.log(' - GitHub Copilot → .github/copilot-instructions.md');
2169
+ console.log(' - GitHub Copilot (edit) → .github/instructions/*.instructions.md');
2170
+ console.log('');
2171
+ }
2172
+
2173
+ async function createInfrastructure(
2174
+ projectDir: string,
2175
+ projectName: string,
2176
+ azureConfig: AzureConfig,
2177
+ backendLanguage: BackendLanguage
2178
+ ) {
2179
+ console.log('📦 Creating infrastructure files (Bicep)...\n');
2180
+
2181
+ const infraDir = path.join(projectDir, 'infra');
2182
+ const modulesDir = path.join(infraDir, 'modules');
2183
+ fs.mkdirSync(modulesDir, { recursive: true });
2184
+
2185
+ const enableVNet = azureConfig.vnetOption !== 'none';
2186
+ const functionsRuntime = getFunctionsRuntimeConfig(backendLanguage);
2187
+
2188
+ // main.bicep
2189
+ const mainBicep = `targetScope = 'resourceGroup'
2190
+
2191
+ @description('Project name')
2192
+ param projectName string
2193
+
2194
+ @description('Location for Functions and Cosmos DB')
2195
+ param location string = resourceGroup().location
2196
+
2197
+ @description('Location for Static Web App (must be explicitly provided)')
2198
+ param swaLocation string
2199
+
2200
+ @description('Cosmos DB mode')
2201
+ @allowed(['freetier', 'serverless'])
2202
+ param cosmosDbMode string = '${azureConfig.cosmosDbMode}'
2203
+
2204
+ @description('Enable VNet integration')
2205
+ param enableVNet bool = ${enableVNet}
2206
+
2207
+ // Shared Log Analytics Workspace (in Functions region for data residency)
2208
+ module logAnalytics 'modules/loganalytics.bicep' = {
2209
+ name: 'logAnalytics'
2210
+ params: {
2211
+ name: 'log-\${projectName}'
2212
+ location: location
2213
+ }
2214
+ }
2215
+
2216
+ // Application Insights for Static Web App (must be in same region as SWA)
2217
+ module appInsightsSwa 'modules/appinsights.bicep' = {
2218
+ name: 'appInsightsSwa'
2219
+ params: {
2220
+ name: 'appi-\${projectName}-swa'
2221
+ location: swaLocation
2222
+ logAnalyticsWorkspaceId: logAnalytics.outputs.id
2223
+ }
2224
+ }
2225
+
2226
+ // Application Insights for Functions (in same region as Functions)
2227
+ module appInsightsFunctions 'modules/appinsights.bicep' = {
2228
+ name: 'appInsightsFunctions'
2229
+ params: {
2230
+ name: 'appi-\${projectName}-func'
2231
+ location: location
2232
+ logAnalyticsWorkspaceId: logAnalytics.outputs.id
2233
+ }
2234
+ }
2235
+
2236
+ // Static Web App
2237
+ module staticWebApp 'modules/staticwebapp.bicep' = {
2238
+ name: 'staticWebApp'
2239
+ params: {
2240
+ name: 'swa-\${projectName}'
2241
+ location: swaLocation
2242
+ sku: 'Standard'
2243
+ appInsightsConnectionString: appInsightsSwa.outputs.connectionString
2244
+ }
2245
+ }
2246
+
2247
+ // VNet (conditional)
2248
+ module vnet 'modules/vnet.bicep' = if (enableVNet) {
2249
+ name: 'vnet'
2250
+ params: {
2251
+ name: 'vnet-\${projectName}'
2252
+ location: location
2253
+ }
2254
+ }
2255
+
2256
+ // Cosmos DB (conditional based on mode) - Deploy BEFORE Functions
2257
+ module cosmosDbFreeTier 'modules/cosmosdb-freetier.bicep' = if (cosmosDbMode == 'freetier') {
2258
+ name: 'cosmosDb'
2259
+ params: {
2260
+ accountName: 'cosmos-\${projectName}'
2261
+ databaseName: '\${projectName}Database'
2262
+ location: location
2263
+ publicNetworkAccess: enableVNet ? 'Disabled' : 'Enabled'
2264
+ }
2265
+ }
2266
+
2267
+ module cosmosDbServerless 'modules/cosmosdb-serverless.bicep' = if (cosmosDbMode == 'serverless') {
2268
+ name: 'cosmosDb'
2269
+ params: {
2270
+ accountName: 'cosmos-\${projectName}'
2271
+ databaseName: '\${projectName}Database'
2272
+ location: location
2273
+ publicNetworkAccess: enableVNet ? 'Disabled' : 'Enabled'
2274
+ }
2275
+ }
2276
+
2277
+ // Cosmos DB Private Endpoint (conditional)
2278
+ module cosmosPrivateEndpoint 'modules/private-endpoint-cosmos.bicep' = if (enableVNet) {
2279
+ name: 'cosmosPrivateEndpoint'
2280
+ params: {
2281
+ name: 'pe-cosmos-\${projectName}'
2282
+ location: location
2283
+ cosmosAccountId: cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.id : cosmosDbServerless.outputs.id
2284
+ cosmosAccountName: cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.accountName : cosmosDbServerless.outputs.accountName
2285
+ subnetId: vnet.outputs.privateEndpointSubnetId
2286
+ vnetId: vnet.outputs.id
2287
+ }
2288
+ dependsOn: [
2289
+ cosmosDbFreeTier
2290
+ cosmosDbServerless
2291
+ vnet
2292
+ ]
2293
+ }
2294
+
2295
+ // Azure Functions (Flex Consumption) - Deploy AFTER Cosmos DB
2296
+ module functionsFlex 'modules/functions-flex.bicep' = {
2297
+ name: 'functionsApp'
2298
+ params: {
2299
+ name: 'func-\${projectName}'
2300
+ location: location
2301
+ storageAccountName: 'stg\${uniqueString(resourceGroup().id, projectName)}'
2302
+ appInsightsConnectionString: appInsightsFunctions.outputs.connectionString
2303
+ swaDefaultHostname: staticWebApp.outputs.defaultHostname
2304
+ cosmosDbEndpoint: cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.endpoint : cosmosDbServerless.outputs.endpoint
2305
+ cosmosDbDatabaseName: cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.databaseName : cosmosDbServerless.outputs.databaseName
2306
+ functionsRuntimeName: '${functionsRuntime.name}'
2307
+ functionsRuntimeVersion: '${functionsRuntime.version}'
2308
+ enableVNet: enableVNet
2309
+ vnetSubnetId: enableVNet ? vnet.outputs.functionsSubnetId : ''
2310
+ }
2311
+ dependsOn: [
2312
+ cosmosDbFreeTier
2313
+ cosmosDbServerless
2314
+ cosmosPrivateEndpoint
2315
+ ]
2316
+ }
2317
+
2318
+ // Cosmos DB role assignment for Functions (after Functions is created)
2319
+ module cosmosDbRoleAssignmentFreeTier 'modules/cosmosdb-role-assignment.bicep' = if (cosmosDbMode == 'freetier') {
2320
+ name: 'cosmosDbRoleAssignment'
2321
+ params: {
2322
+ cosmosAccountName: cosmosDbFreeTier.outputs.accountName
2323
+ functionsPrincipalId: functionsFlex.outputs.principalId
2324
+ }
2325
+ dependsOn: [
2326
+ functionsFlex
2327
+ ]
2328
+ }
2329
+
2330
+ module cosmosDbRoleAssignmentServerless 'modules/cosmosdb-role-assignment.bicep' = if (cosmosDbMode == 'serverless') {
2331
+ name: 'cosmosDbRoleAssignment'
2332
+ params: {
2333
+ cosmosAccountName: cosmosDbServerless.outputs.accountName
2334
+ functionsPrincipalId: functionsFlex.outputs.principalId
2335
+ }
2336
+ dependsOn: [
2337
+ functionsFlex
2338
+ ]
2339
+ }
2340
+
2341
+ // Update SWA config with Functions hostname (after Functions deployment)
2342
+ module staticWebAppConfig 'modules/staticwebapp-config.bicep' = {
2343
+ name: 'staticWebAppConfig'
2344
+ params: {
2345
+ staticWebAppName: staticWebApp.outputs.name
2346
+ functionsDefaultHostname: functionsFlex.outputs.defaultHostname
2347
+ appInsightsConnectionString: appInsightsSwa.outputs.connectionString
2348
+ }
2349
+ dependsOn: [
2350
+ functionsFlex
2351
+ ]
2352
+ }
2353
+
2354
+ output staticWebAppName string = staticWebApp.outputs.name
2355
+ output staticWebAppUrl string = staticWebApp.outputs.defaultHostname
2356
+ output functionsAppName string = functionsFlex.outputs.name
2357
+ output functionsAppUrl string = functionsFlex.outputs.defaultHostname
2358
+ output cosmosDbAccountName string = cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.accountName : cosmosDbServerless.outputs.accountName
2359
+ output cosmosDbEndpoint string = cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.endpoint : cosmosDbServerless.outputs.endpoint
2360
+ output cosmosDatabaseName string = cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.databaseName : cosmosDbServerless.outputs.databaseName
2361
+ output logAnalyticsWorkspaceName string = logAnalytics.outputs.name
2362
+ output logAnalyticsWorkspaceId string = logAnalytics.outputs.id
2363
+ output appInsightsSwaName string = appInsightsSwa.outputs.name
2364
+ output appInsightsSwaConnectionString string = appInsightsSwa.outputs.connectionString
2365
+ output appInsightsFunctionsName string = appInsightsFunctions.outputs.name
2366
+ output appInsightsFunctionsConnectionString string = appInsightsFunctions.outputs.connectionString
2367
+ output vnetEnabled bool = enableVNet
2368
+ output vnetName string = enableVNet ? vnet.outputs.name : ''
2369
+ `;
2370
+
2371
+ fs.writeFileSync(path.join(infraDir, 'main.bicep'), mainBicep);
2372
+
2373
+ // main.parameters.json
2374
+ const params = `{
2375
+ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
2376
+ "contentVersion": "1.0.0.0",
2377
+ "parameters": {
2378
+ "projectName": {
2379
+ "value": "${projectName}"
2380
+ },
2381
+ "cosmosDbMode": {
2382
+ "value": "${azureConfig.cosmosDbMode}"
2383
+ },
2384
+ "enableVNet": {
2385
+ "value": ${enableVNet}
2386
+ }
2387
+ }
2388
+ }
2389
+ `;
2390
+ fs.writeFileSync(path.join(infraDir, 'main.parameters.json'), params);
2391
+
2392
+ // modules/staticwebapp.bicep
2393
+ const staticWebAppBicep = `@description('Static Web App name')
2394
+ param name string
2395
+
2396
+ @description('Location for the Static Web App')
2397
+ param location string
2398
+
2399
+ @description('SKU name (Free or Standard)')
2400
+ @allowed([
2401
+ 'Free'
2402
+ 'Standard'
2403
+ ])
2404
+ param sku string = 'Standard'
2405
+
2406
+ @description('Application Insights connection string')
2407
+ param appInsightsConnectionString string
2408
+
2409
+ resource staticWebApp 'Microsoft.Web/staticSites@2023-01-01' = {
2410
+ name: name
2411
+ location: location
2412
+ sku: {
2413
+ name: sku
2414
+ tier: sku
2415
+ }
2416
+ properties: {
2417
+ buildProperties: {
2418
+ skipGithubActionWorkflowGeneration: true
2419
+ }
2420
+ }
2421
+ }
2422
+
2423
+ // Link Application Insights to Static Web App (for both client and server-side telemetry)
2424
+ resource staticWebAppConfig 'Microsoft.Web/staticSites/config@2023-01-01' = {
2425
+ parent: staticWebApp
2426
+ name: 'appsettings'
2427
+ properties: {
2428
+ APPLICATIONINSIGHTS_CONNECTION_STRING: appInsightsConnectionString
2429
+ ApplicationInsightsAgent_EXTENSION_VERSION: '~3'
2430
+ }
2431
+ }
2432
+
2433
+ output id string = staticWebApp.id
2434
+ output name string = staticWebApp.name
2435
+ output defaultHostname string = staticWebApp.properties.defaultHostname
2436
+ `;
2437
+ fs.writeFileSync(path.join(modulesDir, 'staticwebapp.bicep'), staticWebAppBicep);
2438
+
2439
+ // modules/staticwebapp-config.bicep (for updating config after Functions deployment)
2440
+ const staticWebAppConfigBicep = `@description('Static Web App name')
2441
+ param staticWebAppName string
2442
+
2443
+ @description('Functions App default hostname for backend API calls')
2444
+ param functionsDefaultHostname string
2445
+
2446
+ @description('Application Insights connection string for SWA')
2447
+ param appInsightsConnectionString string
2448
+
2449
+ resource staticWebApp 'Microsoft.Web/staticSites@2023-01-01' existing = {
2450
+ name: staticWebAppName
2451
+ }
2452
+
2453
+ resource staticWebAppConfig 'Microsoft.Web/staticSites/config@2023-01-01' = {
2454
+ parent: staticWebApp
2455
+ name: 'appsettings'
2456
+ properties: {
2457
+ APPLICATIONINSIGHTS_CONNECTION_STRING: appInsightsConnectionString
2458
+ ApplicationInsightsAgent_EXTENSION_VERSION: '~3'
2459
+ BACKEND_FUNCTIONS_BASE_URL: 'https://\${functionsDefaultHostname}'
2460
+ }
2461
+ }
2462
+
2463
+ output configName string = staticWebAppConfig.name
2464
+ `;
2465
+ fs.writeFileSync(path.join(modulesDir, 'staticwebapp-config.bicep'), staticWebAppConfigBicep);
2466
+
2467
+ // modules/loganalytics.bicep (Shared Log Analytics Workspace)
2468
+ const logAnalyticsBicep = `@description('Log Analytics workspace name')
2469
+ param name string
2470
+
2471
+ @description('Location for Log Analytics workspace')
2472
+ param location string
2473
+
2474
+ resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
2475
+ name: name
2476
+ location: location
2477
+ properties: {
2478
+ sku: {
2479
+ name: 'PerGB2018'
2480
+ }
2481
+ retentionInDays: 30
2482
+ }
2483
+ }
2484
+
2485
+ output id string = logAnalytics.id
2486
+ output name string = logAnalytics.name
2487
+ `;
2488
+ fs.writeFileSync(path.join(modulesDir, 'loganalytics.bicep'), logAnalyticsBicep);
2489
+
2490
+ // modules/appinsights.bicep (Application Insights only, connects to shared Log Analytics)
2491
+ const appInsightsBicep = `@description('Application Insights name')
2492
+ param name string
2493
+
2494
+ @description('Location for Application Insights')
2495
+ param location string
2496
+
2497
+ @description('Log Analytics workspace resource ID')
2498
+ param logAnalyticsWorkspaceId string
2499
+
2500
+ // Application Insights
2501
+ resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
2502
+ name: name
2503
+ location: location
2504
+ kind: 'web'
2505
+ properties: {
2506
+ Application_Type: 'web'
2507
+ WorkspaceResourceId: logAnalyticsWorkspaceId
2508
+ RetentionInDays: 30
2509
+ }
2510
+ }
2511
+
2512
+ output id string = appInsights.id
2513
+ output name string = appInsights.name
2514
+ output connectionString string = appInsights.properties.ConnectionString
2515
+ output instrumentationKey string = appInsights.properties.InstrumentationKey
2516
+ `;
2517
+ fs.writeFileSync(path.join(modulesDir, 'appinsights.bicep'), appInsightsBicep);
2518
+
2519
+ // modules/functions-flex.bicep (Flex Consumption)
2520
+ const functionsFlexBicep = `@description('Functions App name')
2521
+ param name string
2522
+
2523
+ @description('Location for the Functions App')
2524
+ param location string
2525
+
2526
+ @description('Storage account name for Functions')
2527
+ param storageAccountName string
2528
+
2529
+ @description('Application Insights connection string')
2530
+ param appInsightsConnectionString string
2531
+
2532
+ @description('Static Web App default hostname for CORS')
2533
+ param swaDefaultHostname string
2534
+
2535
+ @description('Cosmos DB endpoint')
2536
+ param cosmosDbEndpoint string
2537
+
2538
+ @description('Cosmos DB database name')
2539
+ param cosmosDbDatabaseName string
2540
+
2541
+ @description('Enable VNet integration')
2542
+ param enableVNet bool = false
2543
+
2544
+ @description('VNet subnet ID for Functions (required if enableVNet is true)')
2545
+ param vnetSubnetId string = ''
2546
+
2547
+ @description('Functions runtime name')
2548
+ param functionsRuntimeName string
2549
+
2550
+ @description('Functions runtime version')
2551
+ param functionsRuntimeVersion string
2552
+
2553
+ // Storage Account for Functions
2554
+ resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
2555
+ name: storageAccountName
2556
+ location: location
2557
+ sku: {
2558
+ name: 'Standard_LRS'
2559
+ }
2560
+ kind: 'StorageV2'
2561
+ properties: {
2562
+ supportsHttpsTrafficOnly: true
2563
+ minimumTlsVersion: 'TLS1_2'
2564
+ }
2565
+ }
2566
+
2567
+ // Blob Service for deployment package container
2568
+ resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2023-01-01' = {
2569
+ parent: storageAccount
2570
+ name: 'default'
2571
+ }
2572
+
2573
+ // Deployment package container
2574
+ resource deploymentContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' = {
2575
+ parent: blobService
2576
+ name: 'deploymentpackage'
2577
+ properties: {
2578
+ publicAccess: 'None'
2579
+ }
2580
+ }
2581
+
2582
+ // App Service Plan (Flex Consumption)
2583
+ resource hostingPlan 'Microsoft.Web/serverfarms@2023-12-01' = {
2584
+ name: '\${name}-plan'
2585
+ location: location
2586
+ sku: {
2587
+ name: 'FC1'
2588
+ tier: 'FlexConsumption'
2589
+ }
2590
+ properties: {
2591
+ reserved: true // Required for Linux
2592
+ }
2593
+ }
2594
+
2595
+ // Azure Functions App
2596
+ resource functionApp 'Microsoft.Web/sites@2023-12-01' = {
2597
+ name: name
2598
+ location: location
2599
+ kind: 'functionapp,linux'
2600
+ identity: {
2601
+ type: 'SystemAssigned'
2602
+ }
2603
+ properties: {
2604
+ serverFarmId: hostingPlan.id
2605
+ reserved: true
2606
+ virtualNetworkSubnetId: enableVNet ? vnetSubnetId : null
2607
+ vnetContentShareEnabled: enableVNet
2608
+ functionAppConfig: {
2609
+ deployment: {
2610
+ storage: {
2611
+ type: 'blobContainer'
2612
+ value: '\${storageAccount.properties.primaryEndpoints.blob}deploymentpackage'
2613
+ authentication: {
2614
+ type: 'SystemAssignedIdentity'
2615
+ }
2616
+ }
2617
+ }
2618
+ scaleAndConcurrency: {
2619
+ maximumInstanceCount: 100
2620
+ instanceMemoryMB: 2048
2621
+ }
2622
+ runtime: {
2623
+ name: functionsRuntimeName
2624
+ version: functionsRuntimeVersion
2625
+ }
2626
+ }
2627
+ siteConfig: {
2628
+ appSettings: [
2629
+ {
2630
+ name: 'AzureWebJobsStorage__accountName'
2631
+ value: storageAccount.name
2632
+ }
2633
+ {
2634
+ name: 'FUNCTIONS_EXTENSION_VERSION'
2635
+ value: '~4'
2636
+ }
2637
+ {
2638
+ name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
2639
+ value: appInsightsConnectionString
2640
+ }
2641
+ {
2642
+ name: 'CosmosDBConnection__accountEndpoint'
2643
+ value: cosmosDbEndpoint
2644
+ }
2645
+ {
2646
+ name: 'COSMOS_DB_DATABASE_NAME'
2647
+ value: cosmosDbDatabaseName
2648
+ }
2649
+ ]
2650
+ cors: {
2651
+ allowedOrigins: [
2652
+ 'https://\${swaDefaultHostname}'
2653
+ ]
2654
+ }
2655
+ ipSecurityRestrictions: [
2656
+ {
2657
+ action: 'Allow'
2658
+ ipAddress: 'AzureCloud'
2659
+ tag: 'ServiceTag'
2660
+ priority: 100
2661
+ }
2662
+ ]
2663
+ }
2664
+ httpsOnly: true
2665
+ }
2666
+ }
2667
+
2668
+ // Role Assignment: Storage Blob Data Contributor
2669
+ resource blobDataContributorRole 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
2670
+ name: guid(functionApp.id, storageAccount.id, 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
2671
+ scope: storageAccount
2672
+ properties: {
2673
+ roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')
2674
+ principalId: functionApp.identity.principalId
2675
+ principalType: 'ServicePrincipal'
2676
+ }
2677
+ }
2678
+
2679
+ output id string = functionApp.id
2680
+ output name string = functionApp.name
2681
+ output defaultHostname string = functionApp.properties.defaultHostName
2682
+ output principalId string = functionApp.identity.principalId
2683
+ `;
2684
+ fs.writeFileSync(path.join(modulesDir, 'functions-flex.bicep'), functionsFlexBicep);
2685
+
2686
+ // modules/cosmosdb-freetier.bicep (Free Tier)
2687
+ const cosmosDbFreeTierBicep = `@description('Cosmos DB account name')
2688
+ param accountName string
2689
+
2690
+ @description('Database name')
2691
+ param databaseName string
2692
+
2693
+ @description('Location for Cosmos DB')
2694
+ param location string
2695
+
2696
+ @description('Public network access')
2697
+ @allowed(['Enabled', 'Disabled'])
2698
+ param publicNetworkAccess string = 'Enabled'
2699
+
2700
+ // Cosmos DB Account (Free Tier)
2701
+ resource cosmosAccount 'Microsoft.DocumentDB/databaseAccounts@2023-11-15' = {
2702
+ name: accountName
2703
+ location: location
2704
+ kind: 'GlobalDocumentDB'
2705
+ properties: {
2706
+ databaseAccountOfferType: 'Standard'
2707
+ enableAutomaticFailover: false
2708
+ enableFreeTier: true
2709
+ publicNetworkAccess: publicNetworkAccess
2710
+ disableLocalAuth: true
2711
+ consistencyPolicy: {
2712
+ defaultConsistencyLevel: 'Session'
2713
+ }
2714
+ locations: [
2715
+ {
2716
+ locationName: location
2717
+ failoverPriority: 0
2718
+ isZoneRedundant: false
2719
+ }
2720
+ ]
2721
+ disableKeyBasedMetadataWriteAccess: true
2722
+ }
2723
+ }
2724
+
2725
+ // Cosmos DB Database
2726
+ resource database 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2023-11-15' = {
2727
+ parent: cosmosAccount
2728
+ name: databaseName
2729
+ properties: {
2730
+ resource: {
2731
+ id: databaseName
2732
+ }
2733
+ options: {
2734
+ throughput: 1000
2735
+ }
2736
+ }
2737
+ }
2738
+
2739
+ output id string = cosmosAccount.id
2740
+ output accountName string = cosmosAccount.name
2741
+ output endpoint string = cosmosAccount.properties.documentEndpoint
2742
+ output databaseName string = database.name
2743
+ `;
2744
+ fs.writeFileSync(path.join(modulesDir, 'cosmosdb-freetier.bicep'), cosmosDbFreeTierBicep);
2745
+
2746
+ // modules/cosmosdb-serverless.bicep (Serverless)
2747
+ const cosmosDbServerlessBicep = `@description('Cosmos DB account name')
2748
+ param accountName string
2749
+
2750
+ @description('Database name')
2751
+ param databaseName string
2752
+
2753
+ @description('Location for Cosmos DB')
2754
+ param location string
2755
+
2756
+ @description('Public network access')
2757
+ @allowed(['Enabled', 'Disabled'])
2758
+ param publicNetworkAccess string = 'Enabled'
2759
+
2760
+ // Cosmos DB Account (Serverless)
2761
+ resource cosmosAccount 'Microsoft.DocumentDB/databaseAccounts@2023-11-15' = {
2762
+ name: accountName
2763
+ location: location
2764
+ kind: 'GlobalDocumentDB'
2765
+ properties: {
2766
+ databaseAccountOfferType: 'Standard'
2767
+ enableAutomaticFailover: false
2768
+ publicNetworkAccess: publicNetworkAccess
2769
+ disableLocalAuth: true
2770
+ consistencyPolicy: {
2771
+ defaultConsistencyLevel: 'Session'
2772
+ }
2773
+ locations: [
2774
+ {
2775
+ locationName: location
2776
+ failoverPriority: 0
2777
+ isZoneRedundant: false
2778
+ }
2779
+ ]
2780
+ capabilities: [
2781
+ {
2782
+ name: 'EnableServerless'
2783
+ }
2784
+ ]
2785
+ disableKeyBasedMetadataWriteAccess: true
2786
+ }
2787
+ }
2788
+
2789
+ // Cosmos DB Database
2790
+ resource database 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2023-11-15' = {
2791
+ parent: cosmosAccount
2792
+ name: databaseName
2793
+ properties: {
2794
+ resource: {
2795
+ id: databaseName
2796
+ }
2797
+ }
2798
+ }
2799
+
2800
+ output id string = cosmosAccount.id
2801
+ output accountName string = cosmosAccount.name
2802
+ output endpoint string = cosmosAccount.properties.documentEndpoint
2803
+ output databaseName string = database.name
2804
+ `;
2805
+ fs.writeFileSync(path.join(modulesDir, 'cosmosdb-serverless.bicep'), cosmosDbServerlessBicep);
2806
+
2807
+ // modules/cosmosdb-role-assignment.bicep (Role Assignment Module)
2808
+ const cosmosDbRoleAssignmentBicep = `@description('Cosmos DB account name')
2809
+ param cosmosAccountName string
2810
+
2811
+ @description('Functions App Managed Identity Principal ID')
2812
+ param functionsPrincipalId string
2813
+
2814
+ resource cosmosAccount 'Microsoft.DocumentDB/databaseAccounts@2023-11-15' existing = {
2815
+ name: cosmosAccountName
2816
+ }
2817
+
2818
+ // Built-in Cosmos DB Data Contributor role definition
2819
+ var cosmosDbDataContributorRoleId = '00000000-0000-0000-0000-000000000002'
2820
+
2821
+ // Role assignment for Functions to access Cosmos DB
2822
+ resource roleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2023-11-15' = {
2823
+ parent: cosmosAccount
2824
+ name: guid(cosmosAccount.id, functionsPrincipalId, cosmosDbDataContributorRoleId)
2825
+ properties: {
2826
+ roleDefinitionId: '\${cosmosAccount.id}/sqlRoleDefinitions/\${cosmosDbDataContributorRoleId}'
2827
+ principalId: functionsPrincipalId
2828
+ scope: cosmosAccount.id
2829
+ }
2830
+ }
2831
+
2832
+ output roleAssignmentId string = roleAssignment.id
2833
+ `;
2834
+ fs.writeFileSync(path.join(modulesDir, 'cosmosdb-role-assignment.bicep'), cosmosDbRoleAssignmentBicep);
2835
+
2836
+ // VNet modules (only generate if VNet is enabled)
2837
+ if (enableVNet) {
2838
+ // modules/vnet.bicep
2839
+ const vnetBicep = `@description('VNet name')
2840
+ param name string
2841
+
2842
+ @description('Location for VNet')
2843
+ param location string
2844
+
2845
+ resource vnet 'Microsoft.Network/virtualNetworks@2023-09-01' = {
2846
+ name: name
2847
+ location: location
2848
+ properties: {
2849
+ addressSpace: {
2850
+ addressPrefixes: [
2851
+ '10.0.0.0/16'
2852
+ ]
2853
+ }
2854
+ subnets: [
2855
+ {
2856
+ name: 'snet-functions'
2857
+ properties: {
2858
+ addressPrefix: '10.0.1.0/24'
2859
+ delegations: [
2860
+ {
2861
+ name: 'delegation'
2862
+ properties: {
2863
+ serviceName: 'Microsoft.App/environments'
2864
+ }
2865
+ }
2866
+ ]
2867
+ }
2868
+ }
2869
+ {
2870
+ name: 'snet-private-endpoints'
2871
+ properties: {
2872
+ addressPrefix: '10.0.2.0/24'
2873
+ privateEndpointNetworkPolicies: 'Disabled'
2874
+ }
2875
+ }
2876
+ ]
2877
+ }
2878
+ }
2879
+
2880
+ output id string = vnet.id
2881
+ output name string = vnet.name
2882
+ output functionsSubnetId string = vnet.properties.subnets[0].id
2883
+ output privateEndpointSubnetId string = vnet.properties.subnets[1].id
2884
+ `;
2885
+ fs.writeFileSync(path.join(modulesDir, 'vnet.bicep'), vnetBicep);
2886
+
2887
+ // modules/private-endpoint-cosmos.bicep
2888
+ const cosmosPrivateEndpointBicep = `@description('Private endpoint name')
2889
+ param name string
2890
+
2891
+ @description('Location')
2892
+ param location string
2893
+
2894
+ @description('Cosmos DB account resource ID')
2895
+ param cosmosAccountId string
2896
+
2897
+ @description('Cosmos DB account name')
2898
+ param cosmosAccountName string
2899
+
2900
+ @description('Subnet ID for private endpoint')
2901
+ param subnetId string
2902
+
2903
+ @description('VNet ID for DNS zone link')
2904
+ param vnetId string
2905
+
2906
+ // Private DNS Zone for Cosmos DB
2907
+ resource privateDnsZone 'Microsoft.Network/privateDnsZones@2020-06-01' = {
2908
+ name: 'privatelink.documents.azure.com'
2909
+ location: 'global'
2910
+ }
2911
+
2912
+ // Link DNS Zone to VNet
2913
+ resource privateDnsZoneVnetLink 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2020-06-01' = {
2914
+ parent: privateDnsZone
2915
+ name: '\${cosmosAccountName}-vnet-link'
2916
+ location: 'global'
2917
+ properties: {
2918
+ virtualNetwork: {
2919
+ id: vnetId
2920
+ }
2921
+ registrationEnabled: false
2922
+ }
2923
+ }
2924
+
2925
+ // Private Endpoint for Cosmos DB
2926
+ resource privateEndpoint 'Microsoft.Network/privateEndpoints@2023-09-01' = {
2927
+ name: name
2928
+ location: location
2929
+ properties: {
2930
+ subnet: {
2931
+ id: subnetId
2932
+ }
2933
+ privateLinkServiceConnections: [
2934
+ {
2935
+ name: '\${cosmosAccountName}-connection'
2936
+ properties: {
2937
+ privateLinkServiceId: cosmosAccountId
2938
+ groupIds: [
2939
+ 'Sql'
2940
+ ]
2941
+ }
2942
+ }
2943
+ ]
2944
+ }
2945
+ }
2946
+
2947
+ // DNS Zone Group
2948
+ resource privateDnsZoneGroup 'Microsoft.Network/privateEndpoints/privateDnsZoneGroups@2023-09-01' = {
2949
+ parent: privateEndpoint
2950
+ name: 'default'
2951
+ properties: {
2952
+ privateDnsZoneConfigs: [
2953
+ {
2954
+ name: 'cosmos-dns-config'
2955
+ properties: {
2956
+ privateDnsZoneId: privateDnsZone.id
2957
+ }
2958
+ }
2959
+ ]
2960
+ }
2961
+ }
2962
+
2963
+ output privateEndpointId string = privateEndpoint.id
2964
+ output privateDnsZoneId string = privateDnsZone.id
2965
+ `;
2966
+ fs.writeFileSync(path.join(modulesDir, 'private-endpoint-cosmos.bicep'), cosmosPrivateEndpointBicep);
2967
+
2968
+ console.log('✅ VNet modules created\n');
2969
+ }
2970
+
2971
+ console.log('✅ Infrastructure files created\n');
2972
+ }
2973
+
2974
+ function getGitHubFunctionsWorkflow(pm: PackageManager, backendLanguage: BackendLanguage): string {
2975
+ const pmCmd = getCommands(pm);
2976
+ const pnpmSetupStep = getCiSetupStep(pm);
2977
+
2978
+ const commonSetup = ` - uses: actions/checkout@v4
2979
+
2980
+ - name: Setup Node.js
2981
+ uses: actions/setup-node@v4
2982
+ with:
2983
+ node-version: '22'
2984
+ ${pnpmSetupStep ? `\n${pnpmSetupStep}\n` : ''}
2985
+ - name: Install dependencies
2986
+ run: |
2987
+ ${pmCmd.ci}
2988
+
2989
+ - name: Build shared package
2990
+ run: |
2991
+ ${pmCmd.runFilter('shared')} build
2992
+ `;
2993
+
2994
+ if (backendLanguage === 'typescript') {
2995
+ return `name: Deploy Azure Functions
2996
+
2997
+ on:
2998
+ push:
2999
+ branches:
3000
+ - main
3001
+ paths:
3002
+ - 'functions/**'
3003
+ - 'shared/**'
3004
+ pull_request:
3005
+ branches:
3006
+ - main
3007
+ paths:
3008
+ - 'functions/**'
3009
+ - 'shared/**'
3010
+ workflow_dispatch:
3011
+
3012
+ jobs:
3013
+ build-and-deploy:
3014
+ runs-on: ubuntu-latest
3015
+ name: Build and Deploy Functions
3016
+
3017
+ steps:
3018
+ ${commonSetup} - name: Build Functions
3019
+ run: |
3020
+ ${pmCmd.runFilter('functions')} build
3021
+
3022
+ - name: Prepare functions for deployment
3023
+ run: |
3024
+ SHARED_PKG_NAME=$(node -p "require('./shared/package.json').name")
3025
+ mkdir -p /tmp/fn-deps
3026
+ node -e "const p=JSON.parse(require('fs').readFileSync('./functions/package.json','utf8'));Object.keys(p.dependencies).filter(k=>k.endsWith('/shared')).forEach(k=>delete p.dependencies[k]);require('fs').writeFileSync('/tmp/fn-deps/package.json',JSON.stringify(p,null,2));"
3027
+ cd /tmp/fn-deps && ${pmCmd.installProd} && cd -
3028
+ rm -rf ./functions/node_modules
3029
+ mv /tmp/fn-deps/node_modules ./functions/node_modules
3030
+ SHARED_DEST="./functions/node_modules/$SHARED_PKG_NAME"
3031
+ mkdir -p "$SHARED_DEST"
3032
+ cp -r ./shared/dist "$SHARED_DEST/dist"
3033
+ cp ./shared/package.json "$SHARED_DEST/package.json"
3034
+
3035
+ - name: Deploy to Azure Functions
3036
+ if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
3037
+ uses: Azure/functions-action@v1
3038
+ with:
3039
+ app-name: \${{ secrets.AZURE_FUNCTIONAPP_NAME }}
3040
+ package: './functions'
3041
+ publish-profile: \${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}
3042
+ sku: flexconsumption
3043
+ `;
3044
+ }
3045
+
3046
+ if (backendLanguage === 'csharp') {
3047
+ return `name: Deploy Azure Functions
3048
+
3049
+ on:
3050
+ push:
3051
+ branches:
3052
+ - main
3053
+ paths:
3054
+ - 'functions/**'
3055
+ - 'shared/**'
3056
+ pull_request:
3057
+ branches:
3058
+ - main
3059
+ paths:
3060
+ - 'functions/**'
3061
+ - 'shared/**'
3062
+ workflow_dispatch:
3063
+
3064
+ jobs:
3065
+ build-and-deploy:
3066
+ runs-on: ubuntu-latest
3067
+ name: Build and Deploy Functions
3068
+
3069
+ steps:
3070
+ ${commonSetup} - name: Setup .NET
3071
+ uses: actions/setup-dotnet@v4
3072
+ with:
3073
+ dotnet-version: '10.0.x'
3074
+
3075
+ - name: Publish Functions
3076
+ run: |
3077
+ dotnet publish ./functions -c Release -o ./functions/publish
3078
+
3079
+ - name: Deploy to Azure Functions
3080
+ if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
3081
+ uses: Azure/functions-action@v1
3082
+ with:
3083
+ app-name: \${{ secrets.AZURE_FUNCTIONAPP_NAME }}
3084
+ package: './functions/publish'
3085
+ publish-profile: \${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}
3086
+ sku: flexconsumption
3087
+ `;
3088
+ }
3089
+
3090
+ return `name: Deploy Azure Functions
3091
+
3092
+ on:
3093
+ push:
3094
+ branches:
3095
+ - main
3096
+ paths:
3097
+ - 'functions/**'
3098
+ - 'shared/**'
3099
+ pull_request:
3100
+ branches:
3101
+ - main
3102
+ paths:
3103
+ - 'functions/**'
3104
+ - 'shared/**'
3105
+ workflow_dispatch:
3106
+
3107
+ jobs:
3108
+ build-and-deploy:
3109
+ runs-on: ubuntu-latest
3110
+ name: Build and Deploy Functions
3111
+
3112
+ steps:
3113
+ ${commonSetup} - name: Setup Python
3114
+ uses: actions/setup-python@v5
3115
+ with:
3116
+ python-version: '3.11'
3117
+
3118
+ - name: Install Functions dependencies
3119
+ run: |
3120
+ python -m pip install --upgrade pip
3121
+ python -m pip install -r ./functions/requirements.txt --target "./functions/.python_packages/lib/site-packages"
3122
+
3123
+ - name: Deploy to Azure Functions
3124
+ if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
3125
+ uses: Azure/functions-action@v1
3126
+ with:
3127
+ app-name: \${{ secrets.AZURE_FUNCTIONAPP_NAME }}
3128
+ package: './functions'
3129
+ publish-profile: \${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}
3130
+ sku: flexconsumption
3131
+ `;
3132
+ }
3133
+
3134
+ function getAzureFunctionsPipeline(pm: PackageManager, backendLanguage: BackendLanguage): string {
3135
+ const pmCmd = getCommands(pm);
3136
+ const azPipelinesSetup = getAzurePipelinesSetup(pm);
3137
+ const commonSetup = ` - task: NodeTool@0
3138
+ inputs:
3139
+ versionSpec: '22.x'
3140
+ displayName: 'Install Node.js'
3141
+ ${azPipelinesSetup ? `\n${azPipelinesSetup}\n` : ''}
3142
+ - script: |
3143
+ ${pmCmd.ci}
3144
+ displayName: 'Install workspace dependencies'
3145
+
3146
+ - script: |
3147
+ ${pmCmd.runFilter('shared')} build
3148
+ displayName: 'Build shared package'
3149
+ `;
3150
+
3151
+ if (backendLanguage === 'typescript') {
3152
+ return `trigger:
3153
+ branches:
3154
+ include:
3155
+ - main
3156
+ paths:
3157
+ include:
3158
+ - functions/**
3159
+ - shared/**
3160
+
3161
+ pr:
3162
+ branches:
3163
+ include:
3164
+ - main
3165
+ paths:
3166
+ include:
3167
+ - functions/**
3168
+ - shared/**
3169
+
3170
+ pool:
3171
+ vmImage: 'ubuntu-latest'
3172
+
3173
+ variables:
3174
+ - group: azure-deployment
3175
+
3176
+ steps:
3177
+ ${commonSetup} - script: |
3178
+ ${pmCmd.runFilter('functions')} build
3179
+ displayName: 'Build Functions'
3180
+
3181
+ - script: |
3182
+ SHARED_PKG_NAME=$(node -p "require('./shared/package.json').name")
3183
+ mkdir -p /tmp/fn-deps
3184
+ node -e "const p=JSON.parse(require('fs').readFileSync('./functions/package.json','utf8'));Object.keys(p.dependencies).filter(k=>k.endsWith('/shared')).forEach(k=>delete p.dependencies[k]);require('fs').writeFileSync('/tmp/fn-deps/package.json',JSON.stringify(p,null,2));"
3185
+ cd /tmp/fn-deps && ${pmCmd.installProd} && cd -
3186
+ rm -rf ./functions/node_modules
3187
+ mv /tmp/fn-deps/node_modules ./functions/node_modules
3188
+ SHARED_DEST="./functions/node_modules/$SHARED_PKG_NAME"
3189
+ mkdir -p "$SHARED_DEST"
3190
+ cp -r ./shared/dist "$SHARED_DEST/dist"
3191
+ cp ./shared/package.json "$SHARED_DEST/package.json"
3192
+ displayName: 'Prepare functions for deployment'
3193
+
3194
+ - task: ArchiveFiles@2
3195
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3196
+ inputs:
3197
+ rootFolderOrFile: '$(System.DefaultWorkingDirectory)/functions'
3198
+ includeRootFolder: false
3199
+ archiveType: 'zip'
3200
+ archiveFile: '$(Build.ArtifactStagingDirectory)/functions.zip'
3201
+ displayName: 'Archive Functions'
3202
+
3203
+ - task: PublishBuildArtifacts@1
3204
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3205
+ inputs:
3206
+ PathtoPublish: '$(Build.ArtifactStagingDirectory)/functions.zip'
3207
+ ArtifactName: 'functions'
3208
+ displayName: 'Publish Functions artifact'
3209
+
3210
+ - task: AzureFunctionApp@2
3211
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3212
+ inputs:
3213
+ azureSubscription: '$(AZURE_SUBSCRIPTION)'
3214
+ appType: 'functionAppLinux'
3215
+ appName: '$(AZURE_FUNCTIONAPP_NAME)'
3216
+ package: '$(Build.ArtifactStagingDirectory)/functions.zip'
3217
+ displayName: 'Deploy to Azure Functions'
3218
+ `;
3219
+ }
3220
+
3221
+ if (backendLanguage === 'csharp') {
3222
+ return `trigger:
3223
+ branches:
3224
+ include:
3225
+ - main
3226
+ paths:
3227
+ include:
3228
+ - functions/**
3229
+ - shared/**
3230
+
3231
+ pr:
3232
+ branches:
3233
+ include:
3234
+ - main
3235
+ paths:
3236
+ include:
3237
+ - functions/**
3238
+ - shared/**
3239
+
3240
+ pool:
3241
+ vmImage: 'ubuntu-latest'
3242
+
3243
+ variables:
3244
+ - group: azure-deployment
3245
+
3246
+ steps:
3247
+ ${commonSetup} - task: UseDotNet@2
3248
+ inputs:
3249
+ version: '10.0.x'
3250
+ displayName: 'Install .NET SDK'
3251
+
3252
+ - script: |
3253
+ dotnet publish ./functions -c Release -o ./functions/publish
3254
+ displayName: 'Publish Functions'
3255
+
3256
+ - task: ArchiveFiles@2
3257
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3258
+ inputs:
3259
+ rootFolderOrFile: '$(System.DefaultWorkingDirectory)/functions/publish'
3260
+ includeRootFolder: false
3261
+ archiveType: 'zip'
3262
+ archiveFile: '$(Build.ArtifactStagingDirectory)/functions.zip'
3263
+ displayName: 'Archive Functions'
3264
+
3265
+ - task: AzureFunctionApp@2
3266
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3267
+ inputs:
3268
+ azureSubscription: '$(AZURE_SUBSCRIPTION)'
3269
+ appType: 'functionAppLinux'
3270
+ appName: '$(AZURE_FUNCTIONAPP_NAME)'
3271
+ package: '$(Build.ArtifactStagingDirectory)/functions.zip'
3272
+ displayName: 'Deploy to Azure Functions'
3273
+ `;
3274
+ }
3275
+
3276
+ return `trigger:
3277
+ branches:
3278
+ include:
3279
+ - main
3280
+ paths:
3281
+ include:
3282
+ - functions/**
3283
+ - shared/**
3284
+
3285
+ pr:
3286
+ branches:
3287
+ include:
3288
+ - main
3289
+ paths:
3290
+ include:
3291
+ - functions/**
3292
+ - shared/**
3293
+
3294
+ pool:
3295
+ vmImage: 'ubuntu-latest'
3296
+
3297
+ variables:
3298
+ - group: azure-deployment
3299
+
3300
+ steps:
3301
+ ${commonSetup} - task: UsePythonVersion@0
3302
+ inputs:
3303
+ versionSpec: '3.11'
3304
+ displayName: 'Install Python'
3305
+
3306
+ - script: |
3307
+ python -m pip install --upgrade pip
3308
+ python -m pip install -r ./functions/requirements.txt --target "./functions/.python_packages/lib/site-packages"
3309
+ displayName: 'Install Functions dependencies'
3310
+
3311
+ - task: ArchiveFiles@2
3312
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3313
+ inputs:
3314
+ rootFolderOrFile: '$(System.DefaultWorkingDirectory)/functions'
3315
+ includeRootFolder: false
3316
+ archiveType: 'zip'
3317
+ archiveFile: '$(Build.ArtifactStagingDirectory)/functions.zip'
3318
+ displayName: 'Archive Functions'
3319
+
3320
+ - task: AzureFunctionApp@2
3321
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3322
+ inputs:
3323
+ azureSubscription: '$(AZURE_SUBSCRIPTION)'
3324
+ appType: 'functionAppLinux'
3325
+ appName: '$(AZURE_FUNCTIONAPP_NAME)'
3326
+ package: '$(Build.ArtifactStagingDirectory)/functions.zip'
3327
+ displayName: 'Deploy to Azure Functions'
3328
+ `;
3329
+ }
3330
+
3331
+ async function createGitHubActionsWorkflows(
3332
+ projectDir: string,
3333
+ azureConfig: AzureConfig,
3334
+ pm: PackageManager,
3335
+ backendLanguage: BackendLanguage
3336
+ ) {
3337
+ console.log('📦 Creating GitHub Actions workflows...\n');
3338
+ const workflowsDir = path.join(projectDir, '.github', 'workflows');
3339
+ fs.mkdirSync(workflowsDir, { recursive: true });
3340
+
3341
+ // deploy-swa.yml
3342
+ const swaWorkflow = `name: Deploy Static Web App
3343
+
3344
+ on:
3345
+ push:
3346
+ branches:
3347
+ - main
3348
+ paths:
3349
+ - 'app/**'
3350
+ - 'components/**'
3351
+ - 'lib/**'
3352
+ - 'shared/**'
3353
+ - 'public/**'
3354
+ - 'package.json'
3355
+ - 'next.config.js'
3356
+ - 'next.config.ts'
3357
+ workflow_dispatch:
3358
+ pull_request:
3359
+ branches:
3360
+ - main
3361
+ paths:
3362
+ - 'app/**'
3363
+ - 'components/**'
3364
+ - 'lib/**'
3365
+ - 'shared/**'
3366
+ - 'public/**'
3367
+ - 'package.json'
3368
+ - 'next.config.js'
3369
+ - 'next.config.ts'
3370
+
3371
+ jobs:
3372
+ build-and-deploy:
3373
+ runs-on: ubuntu-latest
3374
+ name: Build and Deploy Static Web App
3375
+
3376
+ steps:
3377
+ - uses: actions/checkout@v4
3378
+ with:
3379
+ submodules: true
3380
+
3381
+ - name: Deploy to Azure Static Web Apps
3382
+ if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
3383
+ uses: Azure/static-web-apps-deploy@v1
3384
+ with:
3385
+ azure_static_web_apps_api_token: \${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
3386
+ repo_token: \${{ secrets.GITHUB_TOKEN }}
3387
+ action: 'upload'
3388
+ app_location: '/'
3389
+ api_location: ''
3390
+ output_location: ''
3391
+ env:
3392
+ NEXT_TURBOPACK_EXPERIMENTAL_USE_SYSTEM_TLS_CERTS: '1'
3393
+ `;
3394
+ fs.writeFileSync(path.join(workflowsDir, 'deploy-swa.yml'), swaWorkflow);
3395
+
3396
+ // deploy-functions.yml
3397
+ const functionsWorkflow = getGitHubFunctionsWorkflow(pm, backendLanguage);
3398
+ fs.writeFileSync(path.join(workflowsDir, 'deploy-functions.yml'), functionsWorkflow);
3399
+
3400
+ console.log('✅ GitHub Actions workflows created\n');
3401
+ }
3402
+
3403
+ async function createAzurePipelines(projectDir: string, pm: PackageManager, backendLanguage: BackendLanguage) {
3404
+ console.log('📦 Creating Azure Pipelines...\n');
3405
+
3406
+ const pmCmd = getCommands(pm);
3407
+ const azPipelinesSetup = getAzurePipelinesSetup(pm);
3408
+ const pipelinesDir = path.join(projectDir, 'pipelines');
3409
+ fs.mkdirSync(pipelinesDir, { recursive: true });
3410
+
3411
+ // swa.yml
3412
+ const swaPipeline = `trigger:
3413
+ branches:
3414
+ include:
3415
+ - main
3416
+ paths:
3417
+ include:
3418
+ - app/**
3419
+ - components/**
3420
+ - lib/**
3421
+ - shared/**
3422
+ - public/**
3423
+ - package.json
3424
+ - next.config.js
3425
+
3426
+ pr:
3427
+ branches:
3428
+ include:
3429
+ - main
3430
+ paths:
3431
+ include:
3432
+ - app/**
3433
+ - components/**
3434
+ - lib/**
3435
+ - shared/**
3436
+ - public/**
3437
+ - package.json
3438
+ - next.config.js
3439
+
3440
+ pool:
3441
+ vmImage: 'ubuntu-latest'
3442
+
3443
+ variables:
3444
+ - group: azure-deployment
3445
+
3446
+ steps:
3447
+ - task: NodeTool@0
3448
+ inputs:
3449
+ versionSpec: '22.x'
3450
+ displayName: 'Install Node.js'
3451
+ ${azPipelinesSetup ? `\n${azPipelinesSetup}\n` : ''}
3452
+ - script: |
3453
+ ${pmCmd.ci}
3454
+ displayName: 'Install dependencies'
3455
+
3456
+ - script: |
3457
+ ${pmCmd.run} build
3458
+ env:
3459
+ NODE_ENV: production
3460
+ displayName: 'Build Next.js app'
3461
+
3462
+ - task: AzureStaticWebApp@0
3463
+ condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
3464
+ inputs:
3465
+ app_location: '.'
3466
+ output_location: '.next/standalone'
3467
+ skip_app_build: true
3468
+ azure_static_web_apps_api_token: $(AZURE_STATIC_WEB_APPS_API_TOKEN)
3469
+ displayName: 'Deploy to Azure Static Web Apps'
3470
+ `;
3471
+ fs.writeFileSync(path.join(pipelinesDir, 'swa.yml'), swaPipeline);
3472
+
3473
+ // functions.yml
3474
+ const functionsPipeline = getAzureFunctionsPipeline(pm, backendLanguage);
3475
+ fs.writeFileSync(path.join(pipelinesDir, 'functions.yml'), functionsPipeline);
3476
+
3477
+ console.log('✅ Azure Pipelines created\n');
3478
+ }
3479
+
3480
+