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,1001 @@
1
+ /**
2
+ * SwallowKit Scaffold ć‚³ćƒžćƒ³ćƒ‰
3
+ * Zod ćƒ¢ćƒ‡ćƒ«ć‹ć‚‰ Azure Functions と Next.js BFF 恮 CRUD ć‚³ćƒ¼ćƒ‰ć‚’ē”Ÿęˆ
4
+ */
5
+
6
+ import * as fs from "fs";
7
+ import * as path from "path";
8
+ import { spawnSync } from "child_process";
9
+ import { getBackendLanguage, getConnectorDefinition, getAuthConfig, ensureSwallowKitProject } from "../../core/config";
10
+ import { ModelInfo, parseModelFile, toKebabCase, toPascalCase, toCamelCase } from "../../core/scaffold/model-parser";
11
+ import {
12
+ generateCSharpAzureFunctionsCRUD,
13
+ generateCompactAzureFunctionsCRUD,
14
+ generatePythonAzureFunctionsCRUD,
15
+ } from "../../core/scaffold/functions-generator";
16
+ import {
17
+ generateRdbConnectorFunctionTS,
18
+ generateApiConnectorFunctionTS,
19
+ generateRdbConnectorFunctionCSharp,
20
+ generateApiConnectorFunctionCSharp,
21
+ generateRdbConnectorFunctionPython,
22
+ generateApiConnectorFunctionPython,
23
+ } from "../../core/scaffold/connector-functions-generator";
24
+ import { generateCompactBFFRoutes, generateBFFCallFunction, generateConnectorBFFRoutes } from "../../core/scaffold/nextjs-generator";
25
+ import {
26
+ generateListPage,
27
+ generateDetailPage,
28
+ generateFormComponent,
29
+ generateNewPage,
30
+ generateEditPage,
31
+ UIAuthOptions,
32
+ } from "../../core/scaffold/ui-generator";
33
+ import { generateLanguageSchemaArtifacts } from "../../core/scaffold/native-schema-generator";
34
+ import { detectFromProject, getCommands } from "../../utils/package-manager";
35
+ import {
36
+ BackendLanguage,
37
+ RdbConnectorConfig,
38
+ ApiConnectorConfig,
39
+ RdbModelConnectorConfig,
40
+ ApiModelConnectorConfig,
41
+ ModelAuthPolicy,
42
+ AuthConfig,
43
+ } from "../../types";
44
+ import { syncProjectManifest } from "../../core/project/manifest";
45
+
46
+ interface ScaffoldOptions {
47
+ model: string; // ćƒ¢ćƒ‡ćƒ«ćƒ•ć‚”ć‚¤ćƒ«ć®ćƒ‘ć‚¹ļ¼ˆä¾‹: "lib/models/todo.ts" or "todo")
48
+ functionsDir?: string; // Azure Functions ć®ćƒ‡ć‚£ćƒ¬ć‚ÆćƒˆćƒŖļ¼ˆćƒ‡ćƒ•ć‚©ćƒ«ćƒˆ: "functions")
49
+ apiDir?: string; // Next.js API routes ć®ćƒ‡ć‚£ćƒ¬ć‚ÆćƒˆćƒŖļ¼ˆćƒ‡ćƒ•ć‚©ćƒ«ćƒˆ: "app/api")
50
+ apiOnly?: boolean; // true ć®å “åˆć€UI ć‚’ē”Ÿęˆć—ćŖć„ļ¼ˆćƒ‡ćƒ•ć‚©ćƒ«ćƒˆ: false)
51
+ }
52
+
53
+ function getMachineAwareStdio(): "inherit" | "pipe" {
54
+ return process.env.SWALLOWKIT_MACHINE_OUTPUT === "1" ? "pipe" : "inherit";
55
+ }
56
+
57
+ function runSpawnSyncCommand(command: string, args: string[], cwd: string): void {
58
+ const result = spawnSync(command, args, {
59
+ cwd,
60
+ stdio: getMachineAwareStdio(),
61
+ shell: true,
62
+ });
63
+
64
+ if (typeof result.status === "number" && result.status !== 0) {
65
+ throw new Error(`${command} ${args.join(" ")} exited with code ${result.status}`);
66
+ }
67
+
68
+ if (result.error) {
69
+ throw result.error;
70
+ }
71
+ }
72
+
73
+ export async function scaffoldCommand(options: ScaffoldOptions) {
74
+ // SwallowKit ćƒ—ćƒ­ć‚øć‚§ć‚Æćƒˆćƒ‡ć‚£ćƒ¬ć‚ÆćƒˆćƒŖć‹ć©ć†ć‹ć‚’ę¤œčØ¼
75
+ ensureSwallowKitProject("scaffold");
76
+
77
+ console.log("šŸ—ļø SwallowKit Scaffold: Generating CRUD operations...\n");
78
+
79
+ try {
80
+ // 1. Resolve model file path
81
+ const modelPath = resolveModelPath(options.model);
82
+ console.log(`šŸ“„ Model file: ${modelPath}`);
83
+
84
+ // 2. Parse model file
85
+ console.log("šŸ” Parsing model file...");
86
+ const modelInfo = await parseModelFile(modelPath);
87
+ console.log(`āœ… Model parsed: ${modelInfo.name} (${modelInfo.schemaName})`);
88
+
89
+ const backendLanguage = getBackendLanguage();
90
+ console.log(`🧠 Backend language: ${backendLanguage}`);
91
+
92
+ // ć‚³ćƒć‚Æć‚æćƒ¢ćƒ‡ćƒ«ć‹ć©ć†ć‹ć‚’åˆ¤å®š
93
+ const isConnectorModel = !!modelInfo.connectorConfig;
94
+ if (isConnectorModel) {
95
+ console.log(`šŸ”Œ Connector model detected: ${modelInfo.connectorConfig!.connector}`);
96
+ console.log(` Operations: ${modelInfo.connectorConfig!.operations.join(", ")}`);
97
+ }
98
+
99
+ // ćƒć‚¹ćƒˆć‚¹ć‚­ćƒ¼ćƒžå‚ē…§ćŒć‚ć‚Œć°č”Øē¤ŗ
100
+ if (modelInfo.nestedSchemaRefs.length > 0) {
101
+ console.log(`šŸ”— Nested schema references detected:`);
102
+ for (const ref of modelInfo.nestedSchemaRefs) {
103
+ const relType = ref.isArray ? 'array' : 'single';
104
+ const optional = ref.isOptional ? ' (optional)' : '';
105
+ console.log(` - ${ref.fieldName}: ${ref.modelName} [${relType}]${optional}`);
106
+ }
107
+ }
108
+
109
+ // 3. Check for ID field
110
+ if (!modelInfo.hasId) {
111
+ console.warn(
112
+ "āš ļø Warning: Model does not have an 'id' field. CRUD operations may not work correctly."
113
+ );
114
+ }
115
+
116
+ // 4. Read shared package name
117
+ const functionsDir = options.functionsDir || "functions";
118
+ const sharedPackageName = readSharedPackageName();
119
+ const relatedModels = backendLanguage === "typescript"
120
+ ? [modelInfo]
121
+ : await collectModelGraph(modelPath);
122
+
123
+ // 5. Generate BFF callFunction helper
124
+ await generateCallFunctionHelper();
125
+
126
+ // 6. Generate Azure Functions code
127
+ if (isConnectorModel) {
128
+ await generateConnectorFunctionsCode(modelInfo, functionsDir, sharedPackageName, backendLanguage);
129
+ // 6b. Install connector driver dependencies
130
+ await installConnectorDriverDependencies(modelInfo, functionsDir, backendLanguage);
131
+ } else {
132
+ await generateFunctionsCode(modelInfo, functionsDir, sharedPackageName, backendLanguage);
133
+
134
+ if (backendLanguage !== "typescript") {
135
+ await generateLanguageSchemaArtifacts(relatedModels, modelInfo, functionsDir, backendLanguage);
136
+ }
137
+ }
138
+
139
+ // 7. Generate Next.js BFF API Routes
140
+ const apiDir = options.apiDir || "app/api";
141
+ if (isConnectorModel) {
142
+ await generateConnectorBFFRoutesFiles(modelInfo, apiDir, sharedPackageName);
143
+ } else {
144
+ await generateBFFRoutes(modelInfo, apiDir, sharedPackageName);
145
+ }
146
+
147
+ // 8. Generate Cosmos DB container Bicep file (skip for connector models)
148
+ if (!isConnectorModel) {
149
+ await generateCosmosContainer(modelInfo);
150
+ }
151
+
152
+ // 9. Generate UI components (unless --api-only)
153
+ if (!options.apiOnly) {
154
+ const uiAuthConfig = getAuthConfig();
155
+ const uiAuthPolicy = resolveAuthPolicy(modelInfo, uiAuthConfig);
156
+ const uiAuthOptions: UIAuthOptions | undefined =
157
+ uiAuthPolicy && uiAuthConfig && uiAuthConfig.provider !== 'none'
158
+ ? { authPolicy: uiAuthPolicy }
159
+ : undefined;
160
+ await generateUIComponents(modelInfo, sharedPackageName, uiAuthOptions);
161
+ await updateNavigationMenu(modelInfo);
162
+ }
163
+
164
+ await syncProjectManifest();
165
+
166
+ console.log("\nāœ… Scaffold completed successfully!");
167
+ console.log("\nšŸ“ Next steps:");
168
+ console.log(` 1. Review generated files in ${describeFunctionsOutputPath(functionsDir, backendLanguage)} and ${apiDir}/`);
169
+ if (!options.apiOnly) {
170
+ console.log(` 2. Check the generated UI pages in app/${toKebabCase(modelInfo.name)}/`);
171
+ console.log(" 3. Navigate to the model from the homepage menu");
172
+ }
173
+ if (backendLanguage !== "typescript") {
174
+ console.log(` ${options.apiOnly ? "2" : "4"}. Review generated OpenAPI export and native schema assets in ${functionsDir}/openapi/ and ${functionsDir}/generated/`);
175
+ }
176
+ console.log(
177
+ ` ${options.apiOnly ? (backendLanguage === "typescript" ? "2" : "3") : (backendLanguage === "typescript" ? "4" : "5")}. Ensure BACKEND_FUNCTIONS_BASE_URL is set in your .env.local file`
178
+ );
179
+ console.log(
180
+ ` ${options.apiOnly ? (backendLanguage === "typescript" ? "3" : "4") : (backendLanguage === "typescript" ? "5" : "6")}. Configure CosmosDBConnection in functions/local.settings.json`
181
+ );
182
+ console.log(` ${options.apiOnly ? (backendLanguage === "typescript" ? "4" : "5") : (backendLanguage === "typescript" ? "6" : "7")}. Run '${getCommands(detectFromProject()).dlx} swallowkit dev' to test the generated code`);
183
+ } catch (error: any) {
184
+ console.error("\nāŒ Scaffold failed:", error.message);
185
+ process.exit(1);
186
+ }
187
+ }
188
+
189
+ /**
190
+ * ćƒ¢ćƒ‡ćƒ«ć® authPolicy と auth config ć‹ć‚‰å®ŸåŠ¹ēš„ćŖčŖåÆćƒćƒŖć‚·ćƒ¼ć‚’č§£ę±ŗ
191
+ * - ćƒ¢ćƒ‡ćƒ«ć« authPolicy ćŒć‚ć‚Œć°ćć‚Œć‚’ä½æē”Ø
192
+ * - ćŖć‘ć‚Œć° auth.authorization.defaultPolicy に従う
193
+ * - auth čØ­å®šćŒćŖć‘ć‚Œć° undefinedļ¼ˆć‚¬ćƒ¼ćƒ‰ćŖć—ļ¼‰
194
+ */
195
+ function resolveAuthPolicy(modelInfo: ModelInfo, authConfig: AuthConfig | undefined): ModelAuthPolicy | undefined {
196
+ // ćƒ¢ćƒ‡ćƒ«ć«ę˜Žē¤ŗēš„ćŖ authPolicy ćŒć‚ć‚‹å “åˆćÆćć‚Œć‚’ä½æē”Ø
197
+ if (modelInfo.authPolicy) {
198
+ return modelInfo.authPolicy;
199
+ }
200
+
201
+ // auth čØ­å®šćŒćŖć„å “åˆćÆć‚¬ćƒ¼ćƒ‰ćŖć—
202
+ if (!authConfig || authConfig.provider === 'none') {
203
+ return undefined;
204
+ }
205
+
206
+ // defaultPolicy が 'authenticated' ćŖć‚‰čŖčØ¼ć®ćæļ¼ˆćƒ­ćƒ¼ćƒ«ęŒ‡å®šćŖć—ļ¼‰ć®ćƒćƒŖć‚·ćƒ¼ć‚’čæ”ć™
207
+ const defaultPolicy = authConfig.authorization?.defaultPolicy ?? 'authenticated';
208
+ if (defaultPolicy === 'authenticated') {
209
+ return {}; // ē©ŗć®ćƒćƒŖć‚·ćƒ¼ = čŖčØ¼ć®ćæć€ćƒ­ćƒ¼ćƒ«åˆ¶é™ćŖć—
210
+ }
211
+
212
+ // defaultPolicy が 'anonymous' ćŖć‚‰čŖčØ¼äøč¦
213
+ return undefined;
214
+ }
215
+
216
+ /**
217
+ * ćƒ¢ćƒ‡ćƒ«ćƒ•ć‚”ć‚¤ćƒ«ć®ćƒ‘ć‚¹ć‚’č§£ę±ŗ
218
+ */
219
+ function resolveModelPath(modelInput: string): string {
220
+ const cwd = process.cwd();
221
+
222
+ // ēµ¶åÆ¾ćƒ‘ć‚¹ć®å “åˆ
223
+ if (path.isAbsolute(modelInput)) {
224
+ return modelInput;
225
+ }
226
+
227
+ // ę‹”å¼µå­ćŒć‚ć‚‹å “åˆļ¼ˆē›øåÆ¾ćƒ‘ć‚¹ļ¼‰
228
+ if (modelInput.endsWith(".ts")) {
229
+ const fullPath = path.join(cwd, modelInput);
230
+ if (fs.existsSync(fullPath)) {
231
+ return fullPath;
232
+ }
233
+ }
234
+
235
+ // ę‹”å¼µå­ćŒćŖć„å “åˆć€shared/models/ ć§ęŽ¢ć™
236
+ const defaultPath = path.join(cwd, "shared", "models", `${modelInput}.ts`);
237
+ if (fs.existsSync(defaultPath)) {
238
+ return defaultPath;
239
+ }
240
+
241
+ // lib/models/ ć§ć‚‚ęŽ¢ć™ļ¼ˆå¾Œę–¹äŗ’ę›ę€§ļ¼‰
242
+ const libPath = path.join(cwd, "lib", "models", `${modelInput}.ts`);
243
+ if (fs.existsSync(libPath)) {
244
+ return libPath;
245
+ }
246
+
247
+ // src/models/ ć§ć‚‚ęŽ¢ć™
248
+ const srcPath = path.join(cwd, "src", "models", `${modelInput}.ts`);
249
+ if (fs.existsSync(srcPath)) {
250
+ return srcPath;
251
+ }
252
+
253
+ // č¦‹ć¤ć‹ć‚‰ćŖć„å “åˆćÆć‚Øćƒ©ćƒ¼
254
+ throw new Error(
255
+ `Model file not found: ${modelInput}\n` +
256
+ ` Tried:\n` +
257
+ ` - ${modelInput}\n` +
258
+ ` - ${defaultPath}\n` +
259
+ ` - ${libPath}\n` +
260
+ ` - ${srcPath}\n` +
261
+ ` Please specify a valid model file path.`
262
+ );
263
+ }
264
+
265
+ /**
266
+ * Read shared package name from shared/package.json
267
+ */
268
+ function readSharedPackageName(): string {
269
+ const cwd = process.cwd();
270
+ const sharedPkgPath = path.join(cwd, "shared", "package.json");
271
+
272
+ if (!fs.existsSync(sharedPkgPath)) {
273
+ throw new Error(
274
+ "shared/package.json not found.\n" +
275
+ "The shared package is required for model imports.\n" +
276
+ `Run "${getCommands(detectFromProject()).dlx} swallowkit init" to set up your project.`
277
+ );
278
+ }
279
+
280
+ const pkg = JSON.parse(fs.readFileSync(sharedPkgPath, "utf-8"));
281
+ return pkg.name;
282
+ }
283
+
284
+ /**
285
+ * Generate BFF callFunction helper (lib/api/call-function.ts)
286
+ */
287
+ async function generateCallFunctionHelper(): Promise<void> {
288
+ console.log("\nšŸ“¦ Generating BFF callFunction helper...");
289
+
290
+ const cwd = process.cwd();
291
+
292
+ const helperDir = path.join(cwd, "lib", "api");
293
+ const helperPath = path.join(helperDir, "call-function.ts");
294
+
295
+ if (!fs.existsSync(helperDir)) {
296
+ fs.mkdirSync(helperDir, { recursive: true });
297
+ }
298
+
299
+ // auth čØ­å®šćŒć‚ć‚‹å “åˆćÆ Authorization ćƒ˜ćƒƒćƒ€ćƒ¼č»¢é€ē‰ˆć‚’ē”Ÿęˆ
300
+ const authConfig = getAuthConfig();
301
+ const hasAuth = authConfig && authConfig.provider !== 'none';
302
+ let helperCode: string;
303
+ if (hasAuth) {
304
+ const { generateBFFCallFunctionWithAuth } = await import("../../core/scaffold/auth-generator");
305
+ helperCode = generateBFFCallFunctionWithAuth();
306
+ } else {
307
+ helperCode = generateBFFCallFunction();
308
+ }
309
+
310
+ fs.writeFileSync(helperPath, helperCode, "utf-8");
311
+ console.log(`āœ… Created: ${helperPath}`);
312
+ }
313
+
314
+ /**
315
+ * Generate Azure Functions CRUD code
316
+ */
317
+ async function generateFunctionsCode(
318
+ modelInfo: ModelInfo,
319
+ functionsDir: string,
320
+ sharedPackageName: string,
321
+ backendLanguage: BackendLanguage
322
+ ): Promise<void> {
323
+ console.log("\nšŸ”Ø Generating Azure Functions CRUD code...");
324
+
325
+ // Resolve auth policy: model-level authPolicy or global defaultPolicy
326
+ const authConfig = getAuthConfig();
327
+ const authPolicy = resolveAuthPolicy(modelInfo, authConfig);
328
+ if (authPolicy) {
329
+ console.log(`šŸ” Auth policy detected: ${JSON.stringify(authPolicy)}`);
330
+ }
331
+
332
+ const modelKebab = toKebabCase(modelInfo.name);
333
+ if (backendLanguage === "typescript") {
334
+ const functionFilePath = path.join(
335
+ process.cwd(),
336
+ functionsDir,
337
+ "src",
338
+ `${modelKebab}.ts`
339
+ );
340
+
341
+ const functionDir = path.dirname(functionFilePath);
342
+ if (!fs.existsSync(functionDir)) {
343
+ fs.mkdirSync(functionDir, { recursive: true });
344
+ }
345
+
346
+ const code = generateCompactAzureFunctionsCRUD(modelInfo, sharedPackageName, authPolicy);
347
+ fs.writeFileSync(functionFilePath, code, "utf-8");
348
+ console.log(`āœ… Created: ${functionFilePath}`);
349
+ return;
350
+ }
351
+
352
+ if (backendLanguage === "csharp") {
353
+ const crudDir = path.join(process.cwd(), functionsDir, "Crud");
354
+ const functionFilePath = path.join(crudDir, `${modelInfo.name}Functions.cs`);
355
+ fs.mkdirSync(crudDir, { recursive: true });
356
+
357
+ // Remove init-generated template (singular) to avoid route conflicts
358
+ const templatePath = path.join(crudDir, `${modelInfo.name}Function.cs`);
359
+ if (fs.existsSync(templatePath)) {
360
+ fs.unlinkSync(templatePath);
361
+ console.log(`šŸ—‘ļø Removed template: ${templatePath}`);
362
+ }
363
+
364
+ fs.writeFileSync(functionFilePath, generateCSharpAzureFunctionsCRUD(modelInfo, authPolicy), "utf-8");
365
+ console.log(`āœ… Created: ${functionFilePath}`);
366
+ return;
367
+ }
368
+
369
+ const blueprintsDir = path.join(process.cwd(), functionsDir, "blueprints");
370
+ const blueprintPath = path.join(blueprintsDir, `${modelKebab.replace(/-/g, "_")}.py`);
371
+ fs.mkdirSync(blueprintsDir, { recursive: true });
372
+
373
+ const { blueprint, registration } = generatePythonAzureFunctionsCRUD(modelInfo, authPolicy);
374
+ fs.writeFileSync(blueprintPath, blueprint, "utf-8");
375
+ updatePythonFunctionRegistrations(path.join(process.cwd(), functionsDir, "function_app.py"), registration);
376
+ console.log(`āœ… Created: ${blueprintPath}`);
377
+ }
378
+
379
+ /**
380
+ * ć‚³ćƒć‚Æć‚æćƒ¢ćƒ‡ćƒ«ē”Ø Azure Functions ć‚³ćƒ¼ćƒ‰ē”Ÿęˆ
381
+ */
382
+ async function generateConnectorFunctionsCode(
383
+ modelInfo: ModelInfo,
384
+ functionsDir: string,
385
+ sharedPackageName: string,
386
+ backendLanguage: BackendLanguage
387
+ ): Promise<void> {
388
+ console.log("\nšŸ”Œ Generating Connector Azure Functions code...");
389
+
390
+ const connectorConfig = modelInfo.connectorConfig!;
391
+ const connectorDef = getConnectorDefinition(connectorConfig.connector);
392
+
393
+ if (!connectorDef) {
394
+ throw new Error(
395
+ `Connector '${connectorConfig.connector}' not found in swallowkit.config.js.\n` +
396
+ ` Please add it to the 'connectors' section of your configuration.`
397
+ );
398
+ }
399
+
400
+ // Resolve auth policy (same logic as Cosmos model scaffolding)
401
+ const authConfig = getAuthConfig();
402
+ const authPolicy = resolveAuthPolicy(modelInfo, authConfig);
403
+ if (authPolicy) {
404
+ console.log(`šŸ” Auth policy detected: ${JSON.stringify(authPolicy)}`);
405
+ }
406
+
407
+ const modelKebab = toKebabCase(modelInfo.name);
408
+
409
+ if (backendLanguage === "typescript") {
410
+ const functionFilePath = path.join(
411
+ process.cwd(),
412
+ functionsDir,
413
+ "src",
414
+ `${modelKebab}.ts`
415
+ );
416
+ fs.mkdirSync(path.dirname(functionFilePath), { recursive: true });
417
+
418
+ let code: string;
419
+ if (connectorDef.type === "rdb") {
420
+ code = generateRdbConnectorFunctionTS(
421
+ modelInfo, sharedPackageName,
422
+ connectorDef as RdbConnectorConfig,
423
+ connectorConfig as RdbModelConnectorConfig,
424
+ authPolicy
425
+ );
426
+ } else {
427
+ code = generateApiConnectorFunctionTS(
428
+ modelInfo, sharedPackageName,
429
+ connectorDef as ApiConnectorConfig,
430
+ connectorConfig as ApiModelConnectorConfig,
431
+ authPolicy
432
+ );
433
+ }
434
+
435
+ fs.writeFileSync(functionFilePath, code, "utf-8");
436
+ console.log(`āœ… Created: ${functionFilePath}`);
437
+ return;
438
+ }
439
+
440
+ if (backendLanguage === "csharp") {
441
+ const functionFilePath = path.join(
442
+ process.cwd(),
443
+ functionsDir,
444
+ "Connectors",
445
+ `${modelInfo.name}ConnectorFunctions.cs`
446
+ );
447
+ fs.mkdirSync(path.dirname(functionFilePath), { recursive: true });
448
+
449
+ let code: string;
450
+ if (connectorDef.type === "rdb") {
451
+ code = generateRdbConnectorFunctionCSharp(
452
+ modelInfo,
453
+ connectorDef as RdbConnectorConfig,
454
+ connectorConfig as RdbModelConnectorConfig
455
+ );
456
+ } else {
457
+ code = generateApiConnectorFunctionCSharp(
458
+ modelInfo,
459
+ connectorDef as ApiConnectorConfig,
460
+ connectorConfig as ApiModelConnectorConfig
461
+ );
462
+ }
463
+
464
+ fs.writeFileSync(functionFilePath, code, "utf-8");
465
+ console.log(`āœ… Created: ${functionFilePath}`);
466
+ return;
467
+ }
468
+
469
+ // Python
470
+ const blueprintsDir = path.join(process.cwd(), functionsDir, "blueprints");
471
+ const blueprintPath = path.join(blueprintsDir, `${modelKebab.replace(/-/g, "_")}.py`);
472
+ fs.mkdirSync(blueprintsDir, { recursive: true });
473
+
474
+ let result: { blueprint: string; registration: string };
475
+ if (connectorDef.type === "rdb") {
476
+ result = generateRdbConnectorFunctionPython(
477
+ modelInfo,
478
+ connectorDef as RdbConnectorConfig,
479
+ connectorConfig as RdbModelConnectorConfig
480
+ );
481
+ } else {
482
+ result = generateApiConnectorFunctionPython(
483
+ modelInfo,
484
+ connectorDef as ApiConnectorConfig,
485
+ connectorConfig as ApiModelConnectorConfig
486
+ );
487
+ }
488
+
489
+ fs.writeFileSync(blueprintPath, result.blueprint, "utf-8");
490
+ updatePythonFunctionRegistrations(path.join(process.cwd(), functionsDir, "function_app.py"), result.registration);
491
+ console.log(`āœ… Created: ${blueprintPath}`);
492
+ }
493
+
494
+ /**
495
+ * ć‚³ćƒć‚Æć‚æćƒ¢ćƒ‡ćƒ«ć® scaffold å¾Œć«ć€ē”Ÿęˆć‚³ćƒ¼ćƒ‰ćŒåæ…č¦ćØć™ć‚‹ RDB/API ćƒ‰ćƒ©ć‚¤ćƒć‚’
496
+ * functions ćƒ‡ć‚£ćƒ¬ć‚ÆćƒˆćƒŖć«ć‚¤ćƒ³ć‚¹ćƒˆćƒ¼ćƒ«ć™ć‚‹ć€‚
497
+ */
498
+ async function installConnectorDriverDependencies(
499
+ modelInfo: ModelInfo,
500
+ functionsDir: string,
501
+ backendLanguage: BackendLanguage
502
+ ): Promise<void> {
503
+ const connectorConfig = modelInfo.connectorConfig!;
504
+ const connectorDef = getConnectorDefinition(connectorConfig.connector);
505
+ if (!connectorDef || connectorDef.type !== "rdb") return;
506
+
507
+ const rdbDef = connectorDef as RdbConnectorConfig;
508
+ const functionsPath = path.join(process.cwd(), functionsDir);
509
+
510
+ if (backendLanguage === "typescript") {
511
+ const driverMap: Record<string, { deps: string[]; devDeps: string[] }> = {
512
+ mysql: { deps: ["mysql2"], devDeps: [] },
513
+ postgres: { deps: ["pg"], devDeps: ["@types/pg"] },
514
+ sqlserver: { deps: ["mssql"], devDeps: [] },
515
+ };
516
+ const entry = driverMap[rdbDef.provider];
517
+ if (!entry) return;
518
+
519
+ // package.json ć‚’čŖ­ć‚“ć§ć€ę—¢ć«ć‚¤ćƒ³ć‚¹ćƒˆćƒ¼ćƒ«ęøˆćæćŖć‚‰ć‚¹ć‚­ćƒƒćƒ—
520
+ const pkgJsonPath = path.join(functionsPath, "package.json");
521
+ if (fs.existsSync(pkgJsonPath)) {
522
+ const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
523
+ const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
524
+ const missingDeps = entry.deps.filter(d => !allDeps[d]);
525
+ const missingDevDeps = entry.devDeps.filter(d => !allDeps[d]);
526
+ if (missingDeps.length === 0 && missingDevDeps.length === 0) return;
527
+ }
528
+
529
+ console.log(`\nšŸ“¦ Installing ${rdbDef.provider} driver dependencies...`);
530
+ const pm = detectFromProject(functionsPath);
531
+ const cmds = getCommands(pm);
532
+
533
+ if (entry.deps.length > 0) {
534
+ runSpawnSyncCommand(cmds.name, [pm === "pnpm" ? "add" : "install", ...entry.deps], functionsPath);
535
+ }
536
+ if (entry.devDeps.length > 0) {
537
+ runSpawnSyncCommand(cmds.name, [pm === "pnpm" ? "add" : "install", "-D", ...entry.devDeps], functionsPath);
538
+ }
539
+ console.log(`āœ… ${rdbDef.provider} driver installed`);
540
+ return;
541
+ }
542
+
543
+ if (backendLanguage === "csharp") {
544
+ const nugetMap: Record<string, string> = {
545
+ mysql: "MySqlConnector",
546
+ postgres: "Npgsql",
547
+ sqlserver: "Microsoft.Data.SqlClient",
548
+ };
549
+ const pkg = nugetMap[rdbDef.provider];
550
+ if (!pkg) return;
551
+ console.log(`\nšŸ“¦ Installing ${rdbDef.provider} NuGet package...`);
552
+ runSpawnSyncCommand("dotnet", ["add", path.join(functionsPath, "functions.csproj"), "package", pkg], functionsPath);
553
+ console.log(`āœ… ${pkg} installed`);
554
+ return;
555
+ }
556
+
557
+ // Python — requirements.txt に追記
558
+ const pipMap: Record<string, string> = {
559
+ mysql: "mysql-connector-python",
560
+ postgres: "psycopg2-binary",
561
+ sqlserver: "pymssql",
562
+ };
563
+ const pipPkg = pipMap[rdbDef.provider];
564
+ if (!pipPkg) return;
565
+ const reqPath = path.join(functionsPath, "requirements.txt");
566
+ if (fs.existsSync(reqPath)) {
567
+ const existing = fs.readFileSync(reqPath, "utf-8");
568
+ if (!existing.includes(pipPkg)) {
569
+ console.log(`\nšŸ“¦ Adding ${pipPkg} to requirements.txt...`);
570
+ fs.appendFileSync(reqPath, `\n${pipPkg}\n`);
571
+ console.log(`āœ… ${pipPkg} added`);
572
+ }
573
+ }
574
+ }
575
+
576
+ /**
577
+ * ć‚³ćƒć‚Æć‚æćƒ¢ćƒ‡ćƒ«ē”Ø BFF ćƒ«ćƒ¼ćƒˆē”Ÿęˆļ¼ˆę“ä½œåˆ¶é™ć«åÆ¾åæœļ¼‰
578
+ */
579
+ async function generateConnectorBFFRoutesFiles(
580
+ modelInfo: ModelInfo,
581
+ apiDir: string,
582
+ sharedPackageName: string
583
+ ): Promise<void> {
584
+ console.log("\nšŸ”Œ Generating Connector BFF API routes...");
585
+
586
+ const modelCamel = toCamelCase(modelInfo.name);
587
+ const operations = modelInfo.connectorConfig!.operations;
588
+
589
+ const listRoutePath = path.join(process.cwd(), apiDir, modelCamel, "route.ts");
590
+ const detailRoutePath = path.join(process.cwd(), apiDir, modelCamel, "[id]", "route.ts");
591
+
592
+ fs.mkdirSync(path.dirname(listRoutePath), { recursive: true });
593
+ fs.mkdirSync(path.dirname(detailRoutePath), { recursive: true });
594
+
595
+ const routes = generateConnectorBFFRoutes(modelInfo, sharedPackageName, operations);
596
+
597
+ fs.writeFileSync(listRoutePath, routes.listRoute, "utf-8");
598
+ fs.writeFileSync(detailRoutePath, routes.detailRoute, "utf-8");
599
+
600
+ console.log(`āœ… Created: ${listRoutePath}`);
601
+ console.log(`āœ… Created: ${detailRoutePath}`);
602
+ }
603
+
604
+ async function collectModelGraph(modelPath: string, seen = new Map<string, ModelInfo>()): Promise<ModelInfo[]> {
605
+ const resolvedPath = path.resolve(modelPath);
606
+ if (seen.has(resolvedPath)) {
607
+ return Array.from(seen.values());
608
+ }
609
+
610
+ const modelInfo = await parseModelFile(resolvedPath);
611
+ seen.set(resolvedPath, modelInfo);
612
+
613
+ for (const ref of modelInfo.nestedSchemaRefs) {
614
+ const nestedPath = resolveNestedModelPath(resolvedPath, ref.importPath);
615
+ await collectModelGraph(nestedPath, seen);
616
+ }
617
+
618
+ return Array.from(seen.values());
619
+ }
620
+
621
+ function resolveNestedModelPath(modelPath: string, importPath: string): string {
622
+ let resolvedPath = path.resolve(path.dirname(modelPath), importPath);
623
+ if (!resolvedPath.endsWith(".ts")) {
624
+ resolvedPath += ".ts";
625
+ }
626
+ return resolvedPath;
627
+ }
628
+
629
+ function describeFunctionsOutputPath(functionsDir: string, backendLanguage: BackendLanguage): string {
630
+ if (backendLanguage === "typescript") {
631
+ return `${functionsDir}/src/`;
632
+ }
633
+ if (backendLanguage === "csharp") {
634
+ return `${functionsDir}/Crud/`;
635
+ }
636
+ return `${functionsDir}/blueprints/`;
637
+ }
638
+
639
+ function updatePythonFunctionRegistrations(functionAppPath: string, registration: string): void {
640
+ if (!fs.existsSync(functionAppPath)) {
641
+ throw new Error(`Python Functions entrypoint not found: ${functionAppPath}`);
642
+ }
643
+
644
+ const content = fs.readFileSync(functionAppPath, "utf-8");
645
+
646
+ // Check if import line already exists (handles init-generated layout)
647
+ const importLine = registration.split("\n").find((l) => l.startsWith("from ") || l.startsWith("import "));
648
+ if (importLine && content.includes(importLine)) {
649
+ return;
650
+ }
651
+ if (content.includes(registration)) {
652
+ return;
653
+ }
654
+
655
+ const marker = "# SwallowKit scaffold registrations";
656
+ if (!content.includes(marker)) {
657
+ throw new Error(`Could not find scaffold registration marker in ${functionAppPath}`);
658
+ }
659
+
660
+ const updated = content.replace(marker, `${registration}\n${marker}`);
661
+ fs.writeFileSync(functionAppPath, updated, "utf-8");
662
+ }
663
+
664
+ /**
665
+ * Next.js BFF API Routes ć‚’ē”Ÿęˆ
666
+ */
667
+ async function generateBFFRoutes(
668
+ modelInfo: any,
669
+ apiDir: string,
670
+ sharedPackageName: string
671
+ ): Promise<void> {
672
+ console.log("\nšŸ”Ø Generating Next.js BFF API routes...");
673
+
674
+ const modelCamel = modelInfo.name.charAt(0).toLowerCase() + modelInfo.name.slice(1);
675
+
676
+ // List route: app/api/[model]/route.ts
677
+ const listRoutePath = path.join(
678
+ process.cwd(),
679
+ apiDir,
680
+ modelCamel,
681
+ "route.ts"
682
+ );
683
+
684
+ // Detail route: app/api/[model]/[id]/route.ts
685
+ const detailRoutePath = path.join(
686
+ process.cwd(),
687
+ apiDir,
688
+ modelCamel,
689
+ "[id]",
690
+ "route.ts"
691
+ );
692
+
693
+ // ćƒ‡ć‚£ćƒ¬ć‚ÆćƒˆćƒŖć‚’ä½œęˆ
694
+ const listRouteDir = path.dirname(listRoutePath);
695
+ const detailRouteDir = path.dirname(detailRoutePath);
696
+
697
+ if (!fs.existsSync(listRouteDir)) {
698
+ fs.mkdirSync(listRouteDir, { recursive: true });
699
+ }
700
+
701
+ if (!fs.existsSync(detailRouteDir)) {
702
+ fs.mkdirSync(detailRouteDir, { recursive: true });
703
+ }
704
+
705
+ // ć‚³ćƒ¼ćƒ‰ć‚’ē”Ÿęˆ
706
+ const routes = generateCompactBFFRoutes(modelInfo, sharedPackageName);
707
+
708
+ // ćƒ•ć‚”ć‚¤ćƒ«ć«ę›øćč¾¼ćæ
709
+ fs.writeFileSync(listRoutePath, routes.listRoute, "utf-8");
710
+ fs.writeFileSync(detailRoutePath, routes.detailRoute, "utf-8");
711
+
712
+ console.log(`āœ… Created: ${listRoutePath}`);
713
+ console.log(`āœ… Created: ${detailRoutePath}`);
714
+ }
715
+
716
+ /**
717
+ * Generate Cosmos DB container Bicep file
718
+ */
719
+ async function generateCosmosContainer(modelInfo: any): Promise<void> {
720
+ console.log("\nšŸ—„ļø Generating Cosmos DB container Bicep file...");
721
+
722
+ const modelKebab = toKebabCase(modelInfo.name);
723
+ const modelPascal = toPascalCase(modelInfo.name);
724
+ const cwd = process.cwd();
725
+
726
+ // Check if infra directory exists
727
+ const infraDir = path.join(cwd, "infra");
728
+ if (!fs.existsSync(infraDir)) {
729
+ console.log("ā„¹ļø infra directory not found. Skipping Cosmos DB container generation.");
730
+ return;
731
+ }
732
+
733
+ // Check if containers directory exists, if not create it
734
+ const containersDir = path.join(infraDir, "containers");
735
+ if (!fs.existsSync(containersDir)) {
736
+ fs.mkdirSync(containersDir, { recursive: true });
737
+ }
738
+
739
+ // Generate container Bicep file
740
+ const containerFileName = `${modelKebab}-container.bicep`;
741
+ const containerFilePath = path.join(containersDir, containerFileName);
742
+
743
+ const bicepContent = `@description('Cosmos DB account name')
744
+ param cosmosAccountName string
745
+
746
+ @description('Database name')
747
+ param databaseName string
748
+
749
+ @description('Container name')
750
+ param containerName string = '${modelPascal}s'
751
+
752
+ @description('Partition key path')
753
+ param partitionKeyPath string = '${modelInfo.partitionKey}'
754
+
755
+ @description('Throughput (RU/s) - only used for Free Tier')
756
+ param throughput int = 400
757
+
758
+ @description('Cosmos DB mode: freetier or serverless')
759
+ param cosmosDbMode string
760
+
761
+ // Reference existing Cosmos DB account
762
+ resource cosmosAccount 'Microsoft.DocumentDB/databaseAccounts@2023-11-15' existing = {
763
+ name: cosmosAccountName
764
+ }
765
+
766
+ // Reference existing database
767
+ resource database 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2023-11-15' existing = {
768
+ parent: cosmosAccount
769
+ name: databaseName
770
+ }
771
+
772
+ // Container for ${modelPascal}
773
+ resource container 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2023-11-15' = {
774
+ parent: database
775
+ name: containerName
776
+ properties: {
777
+ resource: {
778
+ id: containerName
779
+ partitionKey: {
780
+ paths: [
781
+ partitionKeyPath
782
+ ]
783
+ kind: 'Hash'
784
+ }
785
+ indexingPolicy: {
786
+ automatic: true
787
+ indexingMode: 'consistent'
788
+ includedPaths: [
789
+ {
790
+ path: '/*'
791
+ }
792
+ ]
793
+ excludedPaths: [
794
+ {
795
+ path: '/_etag/?'
796
+ }
797
+ ]
798
+ }
799
+ }
800
+ options: cosmosDbMode == 'freetier' ? {
801
+ throughput: throughput
802
+ } : {}
803
+ }
804
+ }
805
+
806
+ output containerName string = container.name
807
+ `;
808
+
809
+ // Write Bicep file (overwrite if exists)
810
+ fs.writeFileSync(containerFilePath, bicepContent, "utf-8");
811
+ console.log(`āœ… Created: ${containerFilePath}`);
812
+
813
+ // Update main.bicep to include this container module
814
+ await updateMainBicepWithContainer(modelKebab, modelPascal);
815
+ }
816
+
817
+ /**
818
+ * Update main.bicep to include new container module
819
+ */
820
+ async function updateMainBicepWithContainer(modelKebab: string, modelPascal: string): Promise<void> {
821
+ const cwd = process.cwd();
822
+ const mainBicepPath = path.join(cwd, "infra", "main.bicep");
823
+
824
+ if (!fs.existsSync(mainBicepPath)) {
825
+ console.log("ā„¹ļø main.bicep not found. Please manually add the container module.");
826
+ return;
827
+ }
828
+
829
+ let mainBicepContent = fs.readFileSync(mainBicepPath, "utf-8");
830
+
831
+ // Check if container module already exists
832
+ const containerModuleName = `${modelKebab.replace(/-/g, '')}Container`;
833
+ if (mainBicepContent.includes(`module ${containerModuleName}`)) {
834
+ console.log(`ā„¹ļø Container module '${containerModuleName}' already exists in main.bicep`);
835
+ return;
836
+ }
837
+
838
+ // Find the position to insert the container module (after cosmosDb modules)
839
+ // Look for the end of both cosmosDb modules (FreeTier and Serverless)
840
+ const cosmosModulePattern = /module cosmosDbServerless ['"]modules\/cosmosdb-serverless\.bicep['"] = if \(cosmosDbMode == 'serverless'\) \{[\s\S]*?\n\}/;
841
+ const cosmosModuleMatch = mainBicepContent.match(cosmosModulePattern);
842
+
843
+ if (!cosmosModuleMatch) {
844
+ console.log("āš ļø Could not find Cosmos DB Serverless module in main.bicep. Please manually add the container module:");
845
+ console.log(`\nmodule ${containerModuleName} 'containers/${modelKebab}-container.bicep' = {
846
+ name: '${modelKebab}-container'
847
+ params: {
848
+ cosmosAccountName: cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.accountName : cosmosDbServerless.outputs.accountName
849
+ databaseName: databaseName
850
+ cosmosDbMode: cosmosDbMode
851
+ }
852
+ dependsOn: [
853
+ cosmosDbFreeTier
854
+ cosmosDbServerless
855
+ ]
856
+ }\n`);
857
+ return;
858
+ }
859
+
860
+ // Find the end of the cosmosDbServerless module
861
+ const insertPosition = cosmosModuleMatch.index! + cosmosModuleMatch[0].length;
862
+
863
+ // Create the container module declaration
864
+ const containerModule = `
865
+
866
+ // ${modelPascal} Container
867
+ module ${containerModuleName} 'containers/${modelKebab}-container.bicep' = {
868
+ name: '${modelKebab}-container'
869
+ params: {
870
+ cosmosAccountName: cosmosDbMode == 'freetier' ? cosmosDbFreeTier.outputs.accountName : cosmosDbServerless.outputs.accountName
871
+ databaseName: '$` + `{projectName}Database'
872
+ cosmosDbMode: cosmosDbMode
873
+ }
874
+ dependsOn: [
875
+ cosmosDbFreeTier
876
+ cosmosDbServerless
877
+ ]
878
+ }`;
879
+
880
+ // Insert the module
881
+ mainBicepContent =
882
+ mainBicepContent.slice(0, insertPosition) +
883
+ containerModule +
884
+ mainBicepContent.slice(insertPosition);
885
+
886
+ fs.writeFileSync(mainBicepPath, mainBicepContent, "utf-8");
887
+ console.log(`āœ… Added container module to main.bicep`);
888
+ }
889
+
890
+ /**
891
+ * Generate UI components (list, detail, form, create, edit pages)
892
+ */
893
+ async function generateUIComponents(modelInfo: any, sharedPackageName: string, authOptions?: UIAuthOptions): Promise<void> {
894
+ console.log("\nšŸŽØ Generating UI components...");
895
+
896
+ const modelKebab = toKebabCase(modelInfo.name);
897
+ const modelName = modelInfo.name;
898
+ const cwd = process.cwd();
899
+
900
+ // Create directory structure: app/[model]/
901
+ const modelDir = path.join(cwd, "app", modelKebab);
902
+ const componentsDir = path.join(modelDir, "_components");
903
+ const newDir = path.join(modelDir, "new");
904
+ const idDir = path.join(modelDir, "[id]");
905
+ const editDir = path.join(idDir, "edit");
906
+
907
+ // Create directories
908
+ [componentsDir, newDir, editDir].forEach(dir => {
909
+ if (!fs.existsSync(dir)) {
910
+ fs.mkdirSync(dir, { recursive: true });
911
+ }
912
+ });
913
+
914
+ // Generate and write files
915
+ const listPage = generateListPage(modelInfo, sharedPackageName, authOptions);
916
+ const detailPage = generateDetailPage(modelInfo, sharedPackageName, authOptions);
917
+ const formComponent = generateFormComponent(modelInfo, sharedPackageName);
918
+ const newPage = generateNewPage(modelInfo, authOptions);
919
+ const editPage = generateEditPage(modelInfo, sharedPackageName, authOptions);
920
+
921
+ fs.writeFileSync(path.join(modelDir, "page.tsx"), listPage, "utf-8");
922
+ fs.writeFileSync(path.join(idDir, "page.tsx"), detailPage, "utf-8");
923
+ fs.writeFileSync(path.join(componentsDir, `${modelName}Form.tsx`), formComponent, "utf-8");
924
+ fs.writeFileSync(path.join(newDir, "page.tsx"), newPage, "utf-8");
925
+ fs.writeFileSync(path.join(editDir, "page.tsx"), editPage, "utf-8");
926
+
927
+ console.log(`āœ… Created: ${path.join(modelDir, "page.tsx")}`);
928
+ console.log(`āœ… Created: ${path.join(idDir, "page.tsx")}`);
929
+ console.log(`āœ… Created: ${path.join(componentsDir, `${modelName}Form.tsx`)}`);
930
+ console.log(`āœ… Created: ${path.join(newDir, "page.tsx")}`);
931
+ console.log(`āœ… Created: ${path.join(editDir, "page.tsx")}`);
932
+ }
933
+
934
+ /**
935
+ * Update navigation menu on the homepage
936
+ */
937
+ async function updateNavigationMenu(modelInfo: any): Promise<void> {
938
+ console.log("\nšŸ“‹ Updating navigation menu...");
939
+
940
+ const modelKebab = toKebabCase(modelInfo.name);
941
+ const cwd = process.cwd();
942
+
943
+ // Update scaffold config
944
+ const configPath = path.join(cwd, "lib", "scaffold-config.ts");
945
+
946
+ if (!fs.existsSync(configPath)) {
947
+ console.log("āš ļø scaffold-config.ts not found. Skipping navigation menu update.");
948
+ return;
949
+ }
950
+
951
+ // Read existing config
952
+ const configContent = fs.readFileSync(configPath, "utf-8");
953
+
954
+ // Parse models array - extract each model entry
955
+ const models: Array<{ name: string; path: string; label: string }> = [];
956
+ const modelsMatch = configContent.match(/models:\s*\[([\s\S]*?)\]\s*as\s*ScaffoldModel\[\]/);
957
+
958
+ if (modelsMatch) {
959
+ const modelsArrayContent = modelsMatch[1];
960
+ // Match each object in the array: { name: '...', path: '...', label: '...' }
961
+ const modelPattern = /\{\s*name:\s*['"]([^'"]+)['"]\s*,\s*path:\s*['"]([^'"]+)['"]\s*,\s*label:\s*['"]([^'"]+)['"]\s*\}/g;
962
+ let match;
963
+ while ((match = modelPattern.exec(modelsArrayContent)) !== null) {
964
+ models.push({
965
+ name: match[1],
966
+ path: match[2],
967
+ label: match[3]
968
+ });
969
+ }
970
+ }
971
+
972
+ // Check if model already exists
973
+ if (models.find(m => m.name === modelInfo.name)) {
974
+ console.log(`ā„¹ļø ${modelInfo.name} already in navigation menu`);
975
+ return;
976
+ }
977
+
978
+ // Add new model
979
+ models.push({
980
+ name: modelInfo.name,
981
+ path: `/${modelKebab}`,
982
+ label: modelInfo.displayName
983
+ });
984
+
985
+ // Generate new scaffold-config.ts content
986
+ const newConfigContent = `export interface ScaffoldModel {
987
+ name: string;
988
+ path: string;
989
+ label: string;
990
+ }
991
+
992
+ export const scaffoldConfig = {
993
+ models: [
994
+ ${models.map(m => ` { name: '${m.name}', path: '${m.path}', label: '${m.label}' },`).join('\n')}
995
+ ] as ScaffoldModel[]
996
+ };
997
+ `;
998
+
999
+ fs.writeFileSync(configPath, newConfigContent, "utf-8");
1000
+ console.log(`āœ… Added ${modelInfo.name} to navigation menu`);
1001
+ }