swallowkit 1.0.0-beta.55 → 1.0.0-beta.57

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 (141) hide show
  1. package/dist/cli/commands/add-auth.d.ts.map +1 -1
  2. package/dist/cli/commands/add-auth.js +154 -157
  3. package/dist/cli/commands/add-auth.js.map +1 -1
  4. package/dist/cli/commands/init.d.ts +5 -0
  5. package/dist/cli/commands/init.d.ts.map +1 -1
  6. package/dist/cli/commands/init.js +178 -25
  7. package/dist/cli/commands/init.js.map +1 -1
  8. package/dist/cli/commands/scaffold.d.ts +7 -2
  9. package/dist/cli/commands/scaffold.d.ts.map +1 -1
  10. package/dist/cli/commands/scaffold.js +113 -123
  11. package/dist/cli/commands/scaffold.js.map +1 -1
  12. package/dist/cli/index.d.ts.map +1 -1
  13. package/dist/cli/index.js +127 -2
  14. package/dist/cli/index.js.map +1 -1
  15. package/dist/core/config.d.ts +1 -1
  16. package/dist/core/config.d.ts.map +1 -1
  17. package/dist/core/config.js +8 -4
  18. package/dist/core/config.js.map +1 -1
  19. package/dist/core/operations/auth-operations.d.ts +57 -0
  20. package/dist/core/operations/auth-operations.d.ts.map +1 -0
  21. package/dist/core/operations/auth-operations.js +269 -0
  22. package/dist/core/operations/auth-operations.js.map +1 -0
  23. package/dist/core/operations/create-model.d.ts.map +1 -1
  24. package/dist/core/operations/create-model.js +23 -13
  25. package/dist/core/operations/create-model.js.map +1 -1
  26. package/dist/core/operations/file-session.d.ts +56 -0
  27. package/dist/core/operations/file-session.d.ts.map +1 -0
  28. package/dist/core/operations/file-session.js +203 -0
  29. package/dist/core/operations/file-session.js.map +1 -0
  30. package/dist/core/operations/provision-operations.d.ts +52 -0
  31. package/dist/core/operations/provision-operations.d.ts.map +1 -0
  32. package/dist/core/operations/provision-operations.js +211 -0
  33. package/dist/core/operations/provision-operations.js.map +1 -0
  34. package/dist/core/operations/runtime.d.ts +5 -0
  35. package/dist/core/operations/runtime.d.ts.map +1 -1
  36. package/dist/core/operations/runtime.js +23 -0
  37. package/dist/core/operations/runtime.js.map +1 -1
  38. package/dist/core/operations/scaffold-apply.d.ts +21 -0
  39. package/dist/core/operations/scaffold-apply.d.ts.map +1 -0
  40. package/dist/core/operations/scaffold-apply.js +57 -0
  41. package/dist/core/operations/scaffold-apply.js.map +1 -0
  42. package/dist/core/operations/scaffold-machine.d.ts.map +1 -1
  43. package/dist/core/operations/scaffold-machine.js +1 -13
  44. package/dist/core/operations/scaffold-machine.js.map +1 -1
  45. package/dist/core/operations/scaffold-plan.d.ts +43 -0
  46. package/dist/core/operations/scaffold-plan.d.ts.map +1 -0
  47. package/dist/core/operations/scaffold-plan.js +187 -0
  48. package/dist/core/operations/scaffold-plan.js.map +1 -0
  49. package/dist/core/project/artifacts.d.ts +58 -0
  50. package/dist/core/project/artifacts.d.ts.map +1 -0
  51. package/dist/core/project/artifacts.js +179 -0
  52. package/dist/core/project/artifacts.js.map +1 -0
  53. package/dist/core/project/boundaries.d.ts +50 -0
  54. package/dist/core/project/boundaries.d.ts.map +1 -0
  55. package/dist/core/project/boundaries.js +117 -0
  56. package/dist/core/project/boundaries.js.map +1 -0
  57. package/dist/core/project/drift.d.ts +29 -0
  58. package/dist/core/project/drift.d.ts.map +1 -0
  59. package/dist/core/project/drift.js +188 -0
  60. package/dist/core/project/drift.js.map +1 -0
  61. package/dist/core/project/infra.d.ts +49 -0
  62. package/dist/core/project/infra.d.ts.map +1 -0
  63. package/dist/core/project/infra.js +167 -0
  64. package/dist/core/project/infra.js.map +1 -0
  65. package/dist/core/project/state.d.ts +15 -0
  66. package/dist/core/project/state.d.ts.map +1 -0
  67. package/dist/core/project/state.js +106 -0
  68. package/dist/core/project/state.js.map +1 -0
  69. package/dist/core/project/workflows.d.ts +43 -0
  70. package/dist/core/project/workflows.d.ts.map +1 -0
  71. package/dist/core/project/workflows.js +255 -0
  72. package/dist/core/project/workflows.js.map +1 -0
  73. package/dist/core/scaffold/auth-generator.d.ts.map +1 -1
  74. package/dist/core/scaffold/auth-generator.js +24 -27
  75. package/dist/core/scaffold/auth-generator.js.map +1 -1
  76. package/dist/core/scaffold/functions-generator.js +9 -9
  77. package/dist/core/scaffold/native-schema-generator.d.ts +7 -1
  78. package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -1
  79. package/dist/core/scaffold/native-schema-generator.js +104 -47
  80. package/dist/core/scaffold/native-schema-generator.js.map +1 -1
  81. package/dist/core/scaffold/nextjs-generator.js +6 -6
  82. package/dist/core/scaffold/ui-generator.js +19 -19
  83. package/dist/core/scaffold/ui-generator.js.map +1 -1
  84. package/dist/core/verify/index.d.ts +62 -0
  85. package/dist/core/verify/index.d.ts.map +1 -0
  86. package/dist/core/verify/index.js +343 -0
  87. package/dist/core/verify/index.js.map +1 -0
  88. package/dist/machine/contracts.d.ts +8 -0
  89. package/dist/machine/contracts.d.ts.map +1 -1
  90. package/dist/machine/errors.d.ts +4 -1
  91. package/dist/machine/errors.d.ts.map +1 -1
  92. package/dist/machine/errors.js +9 -1
  93. package/dist/machine/errors.js.map +1 -1
  94. package/dist/machine/index.d.ts.map +1 -1
  95. package/dist/machine/index.js +350 -16
  96. package/dist/machine/index.js.map +1 -1
  97. package/dist/mcp/index.d.ts.map +1 -1
  98. package/dist/mcp/index.js +202 -1
  99. package/dist/mcp/index.js.map +1 -1
  100. package/dist/types/index.d.ts +14 -0
  101. package/dist/types/index.d.ts.map +1 -1
  102. package/package.json +1 -1
  103. package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +17 -17
  104. package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +6 -6
  105. package/src/__tests__/__snapshots__/ui-generator.test.ts.snap +11 -11
  106. package/src/__tests__/agent-loop-phase34.test.ts +449 -0
  107. package/src/__tests__/file-session.test.ts +98 -0
  108. package/src/__tests__/init.test.ts +50 -0
  109. package/src/__tests__/mcp.test.ts +113 -5
  110. package/src/__tests__/plan-apply.test.ts +461 -0
  111. package/src/__tests__/scaffold.test.ts +2 -2
  112. package/src/cli/commands/add-auth.ts +164 -164
  113. package/src/cli/commands/init.ts +186 -24
  114. package/src/cli/commands/scaffold.ts +136 -130
  115. package/src/cli/index.ts +148 -2
  116. package/src/core/config.ts +5 -4
  117. package/src/core/operations/auth-operations.ts +353 -0
  118. package/src/core/operations/create-model.ts +25 -13
  119. package/src/core/operations/file-session.ts +222 -0
  120. package/src/core/operations/provision-operations.ts +297 -0
  121. package/src/core/operations/runtime.ts +23 -0
  122. package/src/core/operations/scaffold-apply.ts +106 -0
  123. package/src/core/operations/scaffold-machine.ts +2 -16
  124. package/src/core/operations/scaffold-plan.ts +215 -0
  125. package/src/core/project/artifacts.ts +205 -0
  126. package/src/core/project/boundaries.ts +170 -0
  127. package/src/core/project/drift.ts +202 -0
  128. package/src/core/project/infra.ts +196 -0
  129. package/src/core/project/state.ts +77 -0
  130. package/src/core/project/workflows.ts +249 -0
  131. package/src/core/scaffold/auth-generator.ts +24 -27
  132. package/src/core/scaffold/functions-generator.ts +9 -9
  133. package/src/core/scaffold/native-schema-generator.ts +152 -70
  134. package/src/core/scaffold/nextjs-generator.ts +6 -6
  135. package/src/core/scaffold/ui-generator.ts +19 -19
  136. package/src/core/verify/index.ts +392 -0
  137. package/src/machine/contracts.ts +16 -0
  138. package/src/machine/errors.ts +12 -1
  139. package/src/machine/index.ts +444 -15
  140. package/src/mcp/index.ts +198 -2
  141. package/src/types/index.ts +17 -0
@@ -45,12 +45,17 @@ const path = __importStar(require("path"));
45
45
  const config_1 = require("../../core/config");
46
46
  const auth_generator_1 = require("../../core/scaffold/auth-generator");
47
47
  const manifest_1 = require("../../core/project/manifest");
48
+ const file_session_1 = require("../../core/operations/file-session");
49
+ const artifacts_1 = require("../../core/project/artifacts");
48
50
  const init_1 = require("./init");
49
- function writeIfMissing(filePath, content) {
50
- if (fs.existsSync(filePath))
51
+ const AUTH_GENERATOR = "add-auth";
52
+ const managedMeta = { ownership: "managed", generator: AUTH_GENERATOR };
53
+ const extensionMeta = { ownership: "extension-point", generator: AUTH_GENERATOR };
54
+ const onceMeta = { ownership: "generated-once", generator: AUTH_GENERATOR };
55
+ function writeIfMissing(session, filePath, content) {
56
+ if (session.fileExists(filePath))
51
57
  return;
52
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
53
- fs.writeFileSync(filePath, content, "utf-8");
58
+ session.writeFile(filePath, content, onceMeta);
54
59
  }
55
60
  function defaultCustomJwtConfig() {
56
61
  return {
@@ -63,78 +68,71 @@ function defaultCustomJwtConfig() {
63
68
  tokenExpiry: "24h",
64
69
  };
65
70
  }
66
- function setupSwaAuth(cwd, backendLanguage) {
71
+ function setupSwaAuth(cwd, backendLanguage, session) {
67
72
  const helperPath = backendLanguage === "typescript"
68
73
  ? path.join(cwd, "functions", "src", "auth", "jwt-helper.ts")
69
74
  : backendLanguage === "csharp"
70
75
  ? path.join(cwd, "functions", "Auth", "JwtHelper.cs")
71
76
  : path.join(cwd, "functions", "auth", "jwt_helper.py");
72
- fs.mkdirSync(path.dirname(helperPath), { recursive: true });
73
77
  const helper = backendLanguage === "typescript"
74
78
  ? (0, auth_generator_1.generateSwaAuthHelperTS)()
75
79
  : backendLanguage === "csharp"
76
80
  ? (0, auth_generator_1.generateSwaAuthHelperCSharp)()
77
81
  : (0, auth_generator_1.generateSwaAuthHelperPython)();
78
- fs.writeFileSync(helperPath, helper, "utf-8");
82
+ session.writeFile(helperPath, helper, managedMeta);
79
83
  const callFnPath = path.join(cwd, "lib", "api", "call-function.ts");
80
- fs.mkdirSync(path.dirname(callFnPath), { recursive: true });
81
- fs.writeFileSync(callFnPath, (0, auth_generator_1.generateBFFCallFunctionWithSwaAuth)(), "utf-8");
84
+ session.writeFile(callFnPath, (0, auth_generator_1.generateBFFCallFunctionWithSwaAuth)(), managedMeta);
82
85
  const authDir = path.join(cwd, "lib", "auth");
83
- fs.mkdirSync(authDir, { recursive: true });
84
- fs.writeFileSync(path.join(authDir, "auth-context.tsx"), generateSwaAuthContext(), "utf-8");
85
- updateRootLayoutWithAuthProvider(cwd);
86
+ session.writeFile(path.join(authDir, "auth-context.tsx"), generateSwaAuthContext(), managedMeta);
87
+ updateRootLayoutWithAuthProvider(cwd, session);
86
88
  const loginDir = path.join(cwd, "app", "login");
87
- fs.mkdirSync(loginDir, { recursive: true });
88
- fs.writeFileSync(path.join(loginDir, "page.tsx"), generateSwaLoginPage(), "utf-8");
89
- updateSwaRouteConfig(cwd);
89
+ session.writeFile(path.join(loginDir, "page.tsx"), generateSwaLoginPage(), managedMeta);
90
+ updateSwaRouteConfig(cwd, session);
90
91
  }
91
- function setupExternalTokenAuth(cwd, backendLanguage) {
92
+ function setupExternalTokenAuth(cwd, backendLanguage, session) {
92
93
  const authDir = path.join(cwd, "lib", "auth");
93
- fs.mkdirSync(authDir, { recursive: true });
94
- writeIfMissing(path.join(authDir, "external-token-adapter.ts"), (0, auth_generator_1.generateExternalTokenAdapter)());
95
- fs.writeFileSync(path.join(authDir, "authenticated-fetch.ts"), (0, auth_generator_1.generateAuthenticatedFetch)(), "utf-8");
96
- fs.writeFileSync(path.join(authDir, "auth-context.tsx"), (0, auth_generator_1.generateExternalTokenAuthContext)(), "utf-8");
97
- fs.writeFileSync(path.join(cwd, "lib", "api", "call-function.ts"), (0, auth_generator_1.generateBFFCallFunctionWithAuth)(), "utf-8");
94
+ writeIfMissing(session, path.join(authDir, "external-token-adapter.ts"), (0, auth_generator_1.generateExternalTokenAdapter)());
95
+ session.writeFile(path.join(authDir, "authenticated-fetch.ts"), (0, auth_generator_1.generateAuthenticatedFetch)(), managedMeta);
96
+ session.writeFile(path.join(authDir, "auth-context.tsx"), (0, auth_generator_1.generateExternalTokenAuthContext)(), managedMeta);
97
+ session.writeFile(path.join(cwd, "lib", "api", "call-function.ts"), (0, auth_generator_1.generateBFFCallFunctionWithAuth)(), managedMeta);
98
98
  const meDir = path.join(cwd, "app", "api", "auth", "me");
99
- fs.mkdirSync(meDir, { recursive: true });
100
- fs.writeFileSync(path.join(meDir, "route.ts"), (0, auth_generator_1.generateExternalTokenBFFMeRoute)(), "utf-8");
101
- updateRootLayoutWithAuthProvider(cwd);
99
+ session.writeFile(path.join(meDir, "route.ts"), (0, auth_generator_1.generateExternalTokenBFFMeRoute)(), managedMeta);
100
+ updateRootLayoutWithAuthProvider(cwd, session);
102
101
  if (backendLanguage === "typescript") {
103
102
  const verifier = path.join(cwd, "functions", "src", "auth", "external-token-verifier.ts");
104
- writeIfMissing(verifier, (0, auth_generator_1.generateExternalTokenVerifierTS)());
105
- fs.writeFileSync(path.join(cwd, "functions", "src", "auth", "jwt-helper.ts"), (0, auth_generator_1.generateExternalTokenHelperTS)(), "utf-8");
106
- fs.writeFileSync(path.join(cwd, "functions", "src", "auth-me.ts"), (0, auth_generator_1.generateExternalAuthMeTS)(), "utf-8");
103
+ writeIfMissing(session, verifier, (0, auth_generator_1.generateExternalTokenVerifierTS)());
104
+ session.writeFile(path.join(cwd, "functions", "src", "auth", "jwt-helper.ts"), (0, auth_generator_1.generateExternalTokenHelperTS)(), managedMeta);
105
+ session.writeFile(path.join(cwd, "functions", "src", "auth-me.ts"), (0, auth_generator_1.generateExternalAuthMeTS)(), managedMeta);
107
106
  }
108
107
  else if (backendLanguage === "csharp") {
109
108
  const verifier = path.join(cwd, "functions", "Auth", "ExternalTokenVerifier.cs");
110
- writeIfMissing(verifier, (0, auth_generator_1.generateExternalTokenVerifierCSharp)());
111
- fs.writeFileSync(path.join(cwd, "functions", "Auth", "JwtHelper.cs"), (0, auth_generator_1.generateExternalTokenHelperCSharp)(), "utf-8");
112
- fs.writeFileSync(path.join(cwd, "functions", "Auth", "ExternalAuthMe.cs"), (0, auth_generator_1.generateExternalAuthMeCSharp)(), "utf-8");
109
+ writeIfMissing(session, verifier, (0, auth_generator_1.generateExternalTokenVerifierCSharp)());
110
+ session.writeFile(path.join(cwd, "functions", "Auth", "JwtHelper.cs"), (0, auth_generator_1.generateExternalTokenHelperCSharp)(), managedMeta);
111
+ session.writeFile(path.join(cwd, "functions", "Auth", "ExternalAuthMe.cs"), (0, auth_generator_1.generateExternalAuthMeCSharp)(), managedMeta);
113
112
  }
114
113
  else {
115
114
  const verifier = path.join(cwd, "functions", "auth", "external_token_verifier.py");
116
- writeIfMissing(verifier, (0, auth_generator_1.generateExternalTokenVerifierPython)());
117
- fs.writeFileSync(path.join(cwd, "functions", "auth", "jwt_helper.py"), (0, auth_generator_1.generateExternalTokenHelperPython)(), "utf-8");
115
+ writeIfMissing(session, verifier, (0, auth_generator_1.generateExternalTokenVerifierPython)());
116
+ session.writeFile(path.join(cwd, "functions", "auth", "jwt_helper.py"), (0, auth_generator_1.generateExternalTokenHelperPython)(), managedMeta);
118
117
  const authMePath = path.join(cwd, "functions", "blueprints", "auth_me.py");
119
- fs.mkdirSync(path.dirname(authMePath), { recursive: true });
120
- fs.writeFileSync(authMePath, (0, auth_generator_1.generateExternalAuthMePython)(), "utf-8");
121
- updatePythonAuthMeRegistration(cwd);
118
+ session.writeFile(authMePath, (0, auth_generator_1.generateExternalAuthMePython)(), managedMeta);
119
+ updatePythonAuthMeRegistration(cwd, session);
122
120
  }
123
121
  }
124
- function updatePythonAuthMeRegistration(cwd) {
122
+ function updatePythonAuthMeRegistration(cwd, session) {
125
123
  const appPath = path.join(cwd, "functions", "function_app.py");
126
- if (!fs.existsSync(appPath))
124
+ if (!session.fileExists(appPath))
127
125
  return;
128
126
  const importLine = "from blueprints.auth_me import bp as external_auth_me_bp";
129
127
  const registerLine = "app.register_blueprint(external_auth_me_bp)";
130
- let content = fs.readFileSync(appPath, "utf-8");
128
+ let content = session.readFile(appPath);
131
129
  if (content.includes(importLine))
132
130
  return;
133
131
  const marker = "# SwallowKit scaffold registrations";
134
132
  content = content.includes(marker)
135
133
  ? content.replace(marker, `${importLine}\n${registerLine}\n${marker}`)
136
134
  : `${content.trimEnd()}\n${importLine}\n${registerLine}\n`;
137
- fs.writeFileSync(appPath, content, "utf-8");
135
+ session.writeFile(appPath, content, extensionMeta);
138
136
  }
139
137
  function generateSwaLoginPage() {
140
138
  return `'use client';
@@ -199,25 +197,25 @@ function addAuthProviderToRootLayout(layout) {
199
197
  }
200
198
  return updated;
201
199
  }
202
- function updateRootLayoutWithAuthProvider(cwd) {
200
+ function updateRootLayoutWithAuthProvider(cwd, session) {
203
201
  const layoutPath = path.join(cwd, "app", "layout.tsx");
204
- if (!fs.existsSync(layoutPath)) {
202
+ if (!session.fileExists(layoutPath)) {
205
203
  throw new Error("app/layout.tsx was not found; cannot install AuthProvider");
206
204
  }
207
- const layout = fs.readFileSync(layoutPath, "utf-8");
205
+ const layout = session.readFile(layoutPath);
208
206
  const updated = addAuthProviderToRootLayout(layout);
209
207
  if (updated !== layout)
210
- fs.writeFileSync(layoutPath, updated, "utf-8");
208
+ session.writeFile(layoutPath, updated, extensionMeta);
211
209
  }
212
- function updateSwaRouteConfig(cwd) {
210
+ function updateSwaRouteConfig(cwd, session) {
213
211
  const configPath = path.join(cwd, "staticwebapp.config.json");
214
- const config = fs.existsSync(configPath) ? JSON.parse(fs.readFileSync(configPath, "utf-8")) : {};
212
+ const config = session.fileExists(configPath) ? JSON.parse(session.readFile(configPath)) : {};
215
213
  config.routes = Array.isArray(config.routes)
216
214
  ? config.routes.filter((route) => route.route !== "/api/*")
217
215
  : [];
218
216
  config.routes.unshift({ route: "/api/*", allowedRoles: ["authenticated"] });
219
217
  config.responseOverrides = { ...(config.responseOverrides || {}), "401": { statusCode: 302, redirect: "/.auth/login/aad?post_login_redirect_uri=.referrer" } };
220
- fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
218
+ session.writeFile(configPath, JSON.stringify(config, null, 2), extensionMeta);
221
219
  }
222
220
  async function addAuthCommand(options) {
223
221
  (0, config_1.ensureSwallowKitProject)("add-auth");
@@ -230,13 +228,21 @@ async function addAuthCommand(options) {
230
228
  const backendLanguage = (0, config_1.getBackendLanguage)();
231
229
  const config = (0, config_1.getValidatedFullConfig)();
232
230
  const cwd = process.cwd();
231
+ // Plan(dry-run) や Apply からセッションが供給される。単体実行時は commit セッションを生成。
232
+ const session = (0, file_session_1.getActiveFileSession)() ?? new file_session_1.FileOperationSession("commit");
233
+ const finalize = async () => {
234
+ if (session.mode === "commit") {
235
+ (0, artifacts_1.recordSessionOperations)(session.operations);
236
+ await (0, manifest_1.syncProjectManifest)();
237
+ }
238
+ };
233
239
  if (options.scheme) {
234
240
  if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(options.scheme))
235
241
  throw new Error("--scheme must start with a letter and contain only letters, digits, '_' or '-'");
236
242
  if (provider === "none" || provider === "swa-custom")
237
243
  throw new Error(`Provider '${provider}' cannot be added as a named scheme`);
238
- addNamedScheme(cwd, options.scheme, provider, backendLanguage);
239
- await (0, manifest_1.syncProjectManifest)();
244
+ addNamedScheme(cwd, options.scheme, provider, backendLanguage, session);
245
+ await finalize();
240
246
  console.log(`\n Named authentication scheme '${options.scheme}' added.`);
241
247
  return;
242
248
  }
@@ -244,22 +250,22 @@ async function addAuthCommand(options) {
244
250
  throw new Error("The swa-custom provider is not implemented. Use --provider swa or --provider custom-jwt.");
245
251
  }
246
252
  if (provider === "none") {
247
- updateConfigWithAuth(cwd, provider, config.auth?.customJwt || defaultCustomJwtConfig());
248
- await (0, manifest_1.syncProjectManifest)();
253
+ updateConfigWithAuth(cwd, provider, config.auth?.customJwt || defaultCustomJwtConfig(), session);
254
+ await finalize();
249
255
  console.log(" Authentication disabled in swallowkit.config.js");
250
256
  return;
251
257
  }
252
258
  if (provider === "swa") {
253
- setupSwaAuth(cwd, backendLanguage);
254
- updateConfigWithAuth(cwd, provider, config.auth?.customJwt || defaultCustomJwtConfig());
255
- await (0, manifest_1.syncProjectManifest)();
259
+ setupSwaAuth(cwd, backendLanguage, session);
260
+ updateConfigWithAuth(cwd, provider, config.auth?.customJwt || defaultCustomJwtConfig(), session);
261
+ await finalize();
256
262
  console.log("\n SWA built-in authentication setup complete!");
257
263
  return;
258
264
  }
259
265
  if (provider === "external-token") {
260
- setupExternalTokenAuth(cwd, backendLanguage);
261
- updateConfigWithAuth(cwd, provider, config.auth?.customJwt || defaultCustomJwtConfig());
262
- await (0, manifest_1.syncProjectManifest)();
266
+ setupExternalTokenAuth(cwd, backendLanguage, session);
267
+ updateConfigWithAuth(cwd, provider, config.auth?.customJwt || defaultCustomJwtConfig(), session);
268
+ await finalize();
263
269
  console.log("\n External token authentication setup complete!");
264
270
  console.log(" Implement the generated frontend adapter and backend verifier before use.");
265
271
  return;
@@ -281,57 +287,52 @@ async function addAuthCommand(options) {
281
287
  // 1. Generate shared/models/auth.ts
282
288
  console.log(" Generating auth models...");
283
289
  const modelsDir = path.join(cwd, "shared", "models");
284
- fs.mkdirSync(modelsDir, { recursive: true });
285
290
  const authModelPath = path.join(modelsDir, "auth.ts");
286
- fs.writeFileSync(authModelPath, (0, auth_generator_1.generateAuthModels)(), "utf-8");
291
+ session.writeFile(authModelPath, (0, auth_generator_1.generateAuthModels)(), managedMeta);
287
292
  console.log(` Created: shared/models/auth.ts`);
288
293
  // Ensure shared package has build infrastructure (tsconfig, build script)
289
- ensureSharedBuildInfrastructure(cwd);
294
+ ensureSharedBuildInfrastructure(cwd, session);
290
295
  // Update shared/index.ts to re-export auth
291
- updateSharedIndex(cwd);
296
+ updateSharedIndex(cwd, session);
292
297
  // Resolve RDB provider for dependency installation
293
298
  const connDef = (0, config_1.getConnectorDefinition)(customJwtConfig.userConnector);
294
299
  const rdbProvider = connDef?.provider ?? "mysql";
295
300
  // 2. Generate Functions auth code
296
301
  console.log("\n Generating auth functions...");
297
- generateFunctionsAuth(cwd, backendLanguage, sharedPackageName, customJwtConfig);
302
+ generateFunctionsAuth(cwd, backendLanguage, sharedPackageName, customJwtConfig, session);
298
303
  // 3. Generate BFF auth routes
299
304
  console.log("\n Generating BFF auth routes...");
300
- generateBFFAuth(cwd, projectName, sharedPackageName);
305
+ generateBFFAuth(cwd, projectName, sharedPackageName, session);
301
306
  // 4. Generate proxy
302
307
  console.log("\n Generating proxy...");
303
308
  const proxyPath = path.join(cwd, "proxy.ts");
304
- fs.writeFileSync(proxyPath, (0, auth_generator_1.generateProxy)(projectName), "utf-8");
309
+ session.writeFile(proxyPath, (0, auth_generator_1.generateProxy)(projectName), managedMeta);
305
310
  console.log(` Created: proxy.ts`);
306
311
  // 5. Generate login page
307
312
  console.log("\n Generating login page...");
308
313
  const loginDir = path.join(cwd, "app", "login");
309
- fs.mkdirSync(loginDir, { recursive: true });
310
- fs.writeFileSync(path.join(loginDir, "page.tsx"), (0, auth_generator_1.generateLoginPage)(), "utf-8");
314
+ session.writeFile(path.join(loginDir, "page.tsx"), (0, auth_generator_1.generateLoginPage)(), managedMeta);
311
315
  console.log(` Created: app/login/page.tsx`);
312
316
  // 6. Generate auth context
313
317
  console.log("\n Generating auth context...");
314
318
  const authLibDir = path.join(cwd, "lib", "auth");
315
- fs.mkdirSync(authLibDir, { recursive: true });
316
- fs.writeFileSync(path.join(authLibDir, "auth-context.tsx"), (0, auth_generator_1.generateAuthContext)(), "utf-8");
319
+ session.writeFile(path.join(authLibDir, "auth-context.tsx"), (0, auth_generator_1.generateAuthContext)(), managedMeta);
317
320
  console.log(`✅ Created: lib/auth/auth-context.tsx`);
318
321
  // 7. Update callFunction with auth support
319
322
  console.log("\n Updating callFunction with auth support...");
320
323
  const callFnPath = path.join(cwd, "lib", "api", "call-function.ts");
321
- const callFnDir = path.dirname(callFnPath);
322
- fs.mkdirSync(callFnDir, { recursive: true });
323
- fs.writeFileSync(callFnPath, (0, auth_generator_1.generateBFFCallFunctionWithAuth)(), "utf-8");
324
+ session.writeFile(callFnPath, (0, auth_generator_1.generateBFFCallFunctionWithAuth)(), managedMeta);
324
325
  console.log(` Updated: lib/api/call-function.ts`);
325
326
  // 8. Update swallowkit.config.js
326
327
  console.log("\n Updating configuration...");
327
- updateConfigWithAuth(cwd, provider, customJwtConfig);
328
+ updateConfigWithAuth(cwd, provider, customJwtConfig, session);
328
329
  // 9. Update environment files
329
330
  console.log("\n Updating environment files...");
330
- updateEnvironmentFiles(cwd);
331
+ updateEnvironmentFiles(cwd, session);
331
332
  // 10. Install dependencies
332
333
  console.log("\n Installing auth dependencies...");
333
- await installAuthDependencies(cwd, backendLanguage, rdbProvider);
334
- await (0, manifest_1.syncProjectManifest)();
334
+ await installAuthDependencies(cwd, backendLanguage, rdbProvider, session);
335
+ await finalize();
335
336
  console.log("\n Authentication setup complete!");
336
337
  console.log("\n Next steps:");
337
338
  console.log(" 1. Review the generated files");
@@ -363,25 +364,25 @@ function findObjectEnd(content, open) {
363
364
  }
364
365
  return -1;
365
366
  }
366
- function updateConfigWithNamedScheme(cwd, scheme, provider) {
367
+ function updateConfigWithNamedScheme(cwd, scheme, provider, session) {
367
368
  var _a, _b;
368
369
  const jsPath = path.join(cwd, "swallowkit.config.js");
369
370
  const jsonPath = path.join(cwd, "swallowkit.config.json");
370
- const configPath = fs.existsSync(jsPath) ? jsPath : jsonPath;
371
- if (!fs.existsSync(configPath))
371
+ const configPath = session.fileExists(jsPath) ? jsPath : jsonPath;
372
+ if (!session.fileExists(configPath))
372
373
  throw new Error("swallowkit.config.js or swallowkit.config.json is required");
373
374
  if (configPath.endsWith(".json")) {
374
- const value = JSON.parse(fs.readFileSync(configPath, "utf-8"));
375
+ const value = JSON.parse(session.readFile(configPath));
375
376
  value.auth ?? (value.auth = {});
376
377
  (_a = value.auth).schemes ?? (_a.schemes = {});
377
378
  if (value.auth.schemes[scheme])
378
379
  throw new Error(`auth.schemes.${scheme} already exists; no files were changed`);
379
380
  value.auth.schemes[scheme] = provider === "custom-jwt" ? { provider, customJwt: defaultCustomJwtConfig() } : { provider };
380
381
  (_b = value.auth).authorization ?? (_b.authorization = { defaultPolicy: "anonymous", policies: {} });
381
- fs.writeFileSync(configPath, JSON.stringify(value, null, 2) + "\n", "utf-8");
382
+ session.writeFile(configPath, JSON.stringify(value, null, 2) + "\n", extensionMeta);
382
383
  return;
383
384
  }
384
- let content = fs.readFileSync(configPath, "utf-8");
385
+ let content = session.readFile(configPath);
385
386
  const authMatch = /\bauth\s*:\s*\{/.exec(content);
386
387
  const customJwt = defaultCustomJwtConfig();
387
388
  const entry = provider === "custom-jwt"
@@ -410,59 +411,59 @@ function updateConfigWithNamedScheme(cwd, scheme, provider) {
410
411
  content = content.slice(0, authOpen + 1) + `\n schemes: {${entry}\n },` + content.slice(authOpen + 1);
411
412
  }
412
413
  }
413
- fs.writeFileSync(configPath, content, "utf-8");
414
+ session.writeFile(configPath, content, extensionMeta);
414
415
  }
415
- function addNamedScheme(cwd, scheme, provider, language) {
416
+ function addNamedScheme(cwd, scheme, provider, language, session) {
416
417
  // Validate/update config before creating files, so duplicate names stop safely.
417
- updateConfigWithNamedScheme(cwd, scheme, provider);
418
+ updateConfigWithNamedScheme(cwd, scheme, provider, session);
418
419
  const slug = scheme.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/_/g, "-").toLowerCase();
419
420
  if (language === "typescript") {
420
421
  const dir = path.join(cwd, "functions", "src", "auth", "schemes", slug);
421
422
  if (provider === "external-token")
422
- writeIfMissing(path.join(dir, "verifier.ts"), (0, auth_generator_1.generateExternalTokenVerifierTS)());
423
+ writeIfMissing(session, path.join(dir, "verifier.ts"), (0, auth_generator_1.generateExternalTokenVerifierTS)());
423
424
  else if (provider === "swa")
424
- writeIfMissing(path.join(dir, "adapter.ts"), (0, auth_generator_1.generateSwaAuthHelperTS)());
425
+ writeIfMissing(session, path.join(dir, "adapter.ts"), (0, auth_generator_1.generateSwaAuthHelperTS)());
425
426
  else
426
- writeIfMissing(path.join(dir, "jwt-helper.ts"), (0, auth_generator_1.generateJwtHelperTS)());
427
+ writeIfMissing(session, path.join(dir, "jwt-helper.ts"), (0, auth_generator_1.generateJwtHelperTS)());
427
428
  }
428
429
  else if (language === "csharp") {
429
430
  const dir = path.join(cwd, "functions", "Auth", "Schemes", slug);
430
431
  if (provider === "external-token")
431
- writeIfMissing(path.join(dir, "ExternalTokenVerifier.cs"), (0, auth_generator_1.generateNamedExternalTokenVerifierCSharp)(scheme));
432
+ writeIfMissing(session, path.join(dir, "ExternalTokenVerifier.cs"), (0, auth_generator_1.generateNamedExternalTokenVerifierCSharp)(scheme));
432
433
  else if (provider === "swa")
433
- writeIfMissing(path.join(dir, "SwaAdapter.cs"), (0, auth_generator_1.generateSwaAuthHelperCSharp)());
434
+ writeIfMissing(session, path.join(dir, "SwaAdapter.cs"), (0, auth_generator_1.generateSwaAuthHelperCSharp)());
434
435
  else
435
- writeIfMissing(path.join(dir, "JwtHelper.cs"), (0, auth_generator_1.generateJwtHelperCSharp)());
436
+ writeIfMissing(session, path.join(dir, "JwtHelper.cs"), (0, auth_generator_1.generateJwtHelperCSharp)());
436
437
  }
437
438
  else {
438
439
  const dir = path.join(cwd, "functions", "auth", "schemes", slug.replace(/-/g, "_"));
439
440
  if (provider === "external-token")
440
- writeIfMissing(path.join(dir, "verifier.py"), (0, auth_generator_1.generateNamedExternalTokenVerifierPython)(scheme));
441
+ writeIfMissing(session, path.join(dir, "verifier.py"), (0, auth_generator_1.generateNamedExternalTokenVerifierPython)(scheme));
441
442
  else if (provider === "swa")
442
- writeIfMissing(path.join(dir, "adapter.py"), (0, auth_generator_1.generateSwaAuthHelperPython)());
443
+ writeIfMissing(session, path.join(dir, "adapter.py"), (0, auth_generator_1.generateSwaAuthHelperPython)());
443
444
  else
444
- writeIfMissing(path.join(dir, "jwt_helper.py"), (0, auth_generator_1.generateJwtHelperPython)());
445
+ writeIfMissing(session, path.join(dir, "jwt_helper.py"), (0, auth_generator_1.generateJwtHelperPython)());
445
446
  }
446
447
  const clientDir = path.join(cwd, "lib", "auth", "schemes", slug);
447
448
  if (provider === "external-token") {
448
- writeIfMissing(path.join(clientDir, "token-adapter.ts"), (0, auth_generator_1.generateExternalTokenAdapter)());
449
- writeIfMissing(path.join(clientDir, "authenticated-fetch.ts"), (0, auth_generator_1.generateAuthenticatedFetch)().replace("'./external-token-adapter'", "'./token-adapter'"));
450
- writeIfMissing(path.join(clientDir, "auth-context.tsx"), (0, auth_generator_1.generateExternalTokenAuthContext)().replace(/external-token-adapter/g, "token-adapter"));
449
+ writeIfMissing(session, path.join(clientDir, "token-adapter.ts"), (0, auth_generator_1.generateExternalTokenAdapter)());
450
+ writeIfMissing(session, path.join(clientDir, "authenticated-fetch.ts"), (0, auth_generator_1.generateAuthenticatedFetch)().replace("'./external-token-adapter'", "'./token-adapter'"));
451
+ writeIfMissing(session, path.join(clientDir, "auth-context.tsx"), (0, auth_generator_1.generateExternalTokenAuthContext)().replace(/external-token-adapter/g, "token-adapter"));
451
452
  }
452
453
  else if (provider === "swa") {
453
- writeIfMissing(path.join(clientDir, "auth-context.tsx"), generateSwaAuthContext());
454
+ writeIfMissing(session, path.join(clientDir, "auth-context.tsx"), generateSwaAuthContext());
454
455
  }
455
456
  else if (provider === "custom-jwt") {
456
- writeIfMissing(path.join(clientDir, "auth-context.tsx"), (0, auth_generator_1.generateAuthContext)());
457
+ writeIfMissing(session, path.join(clientDir, "auth-context.tsx"), (0, auth_generator_1.generateAuthContext)());
457
458
  }
458
459
  const callFunctionPath = path.join(cwd, "lib", "api", "call-function.ts");
459
- if (!fs.existsSync(callFunctionPath))
460
- writeIfMissing(callFunctionPath, (0, auth_generator_1.generateBFFCallFunctionWithMultipleAuth)());
460
+ if (!session.fileExists(callFunctionPath))
461
+ writeIfMissing(session, callFunctionPath, (0, auth_generator_1.generateBFFCallFunctionWithMultipleAuth)());
461
462
  else {
462
- const current = fs.readFileSync(callFunctionPath, "utf-8");
463
+ const current = session.readFile(callFunctionPath);
463
464
  if (!current.includes("x-ms-client-principal") || !current.includes("fetchHeaders['Authorization']")) {
464
465
  if (current.includes("SwallowKit BFF Call Function Helper"))
465
- fs.writeFileSync(callFunctionPath, (0, auth_generator_1.generateBFFCallFunctionWithMultipleAuth)(), "utf-8");
466
+ session.writeFile(callFunctionPath, (0, auth_generator_1.generateBFFCallFunctionWithMultipleAuth)(), managedMeta);
466
467
  else
467
468
  console.warn(" Existing customized lib/api/call-function.ts was preserved. Ensure it forwards Bearer and SWA credentials without logging them.");
468
469
  }
@@ -473,12 +474,12 @@ function addNamedScheme(cwd, scheme, provider, language) {
473
474
  * (tsconfig.json, build script, typescript devDependency).
474
475
  * Required for `dev` command which runs `npm run --workspace=shared build`.
475
476
  */
476
- function ensureSharedBuildInfrastructure(cwd) {
477
+ function ensureSharedBuildInfrastructure(cwd, session) {
477
478
  const sharedDir = path.join(cwd, "shared");
478
479
  const pkgPath = path.join(sharedDir, "package.json");
479
- if (!fs.existsSync(pkgPath))
480
+ if (!session.fileExists(pkgPath))
480
481
  return;
481
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
482
+ const pkg = JSON.parse(session.readFile(pkgPath));
482
483
  let updated = false;
483
484
  // Ensure scripts.build exists
484
485
  if (!pkg.scripts?.build) {
@@ -502,32 +503,32 @@ function ensureSharedBuildInfrastructure(cwd) {
502
503
  updated = true;
503
504
  }
504
505
  if (updated) {
505
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), "utf-8");
506
+ session.writeFile(pkgPath, JSON.stringify(pkg, null, 2), extensionMeta);
506
507
  console.log(` Updated: shared/package.json (added build infrastructure)`);
507
508
  }
508
509
  // Ensure tsconfig.json exists
509
510
  const tsconfigPath = path.join(sharedDir, "tsconfig.json");
510
- if (!fs.existsSync(tsconfigPath)) {
511
- fs.writeFileSync(tsconfigPath, JSON.stringify((0, init_1.buildSharedTsConfig)(), null, 2), "utf-8");
511
+ if (!session.fileExists(tsconfigPath)) {
512
+ session.writeFile(tsconfigPath, JSON.stringify((0, init_1.buildSharedTsConfig)(), null, 2), onceMeta);
512
513
  console.log(` Created: shared/tsconfig.json`);
513
514
  }
514
515
  }
515
- function updateSharedIndex(cwd) {
516
+ function updateSharedIndex(cwd, session) {
516
517
  const indexPath = path.join(cwd, "shared", "index.ts");
517
- if (fs.existsSync(indexPath)) {
518
- let content = fs.readFileSync(indexPath, "utf-8");
518
+ if (session.fileExists(indexPath)) {
519
+ let content = session.readFile(indexPath);
519
520
  if (!content.includes("./models/auth")) {
520
521
  content += `\nexport { LoginRequest, AuthUser, LoginResponse } from './models/auth';\n`;
521
- fs.writeFileSync(indexPath, content, "utf-8");
522
+ session.writeFile(indexPath, content, extensionMeta);
522
523
  console.log(` Updated: shared/index.ts`);
523
524
  }
524
525
  }
525
526
  else {
526
- fs.writeFileSync(indexPath, `export { LoginRequest, AuthUser, LoginResponse } from './models/auth';\n`, "utf-8");
527
+ session.writeFile(indexPath, `export { LoginRequest, AuthUser, LoginResponse } from './models/auth';\n`, extensionMeta);
527
528
  console.log(` Created: shared/index.ts`);
528
529
  }
529
530
  }
530
- function generateFunctionsAuth(cwd, backendLanguage, sharedPackageName, config) {
531
+ function generateFunctionsAuth(cwd, backendLanguage, sharedPackageName, config, session) {
531
532
  const functionsDir = path.join(cwd, "functions");
532
533
  // Resolve the RDB provider from the connector definition
533
534
  const connDef = (0, config_1.getConnectorDefinition)(config.userConnector);
@@ -535,76 +536,70 @@ function generateFunctionsAuth(cwd, backendLanguage, sharedPackageName, config)
535
536
  if (backendLanguage === "typescript") {
536
537
  // Auth functions
537
538
  const srcDir = path.join(functionsDir, "src");
538
- fs.mkdirSync(srcDir, { recursive: true });
539
- fs.writeFileSync(path.join(srcDir, "auth.ts"), (0, auth_generator_1.generateAuthFunctionsTS)(sharedPackageName, config, provider), "utf-8");
539
+ session.writeFile(path.join(srcDir, "auth.ts"), (0, auth_generator_1.generateAuthFunctionsTS)(sharedPackageName, config, provider), managedMeta);
540
540
  console.log(` Created: functions/src/auth.ts`);
541
541
  // JWT helper
542
542
  const authDir = path.join(srcDir, "auth");
543
- fs.mkdirSync(authDir, { recursive: true });
544
- fs.writeFileSync(path.join(authDir, "jwt-helper.ts"), (0, auth_generator_1.generateJwtHelperTS)(), "utf-8");
543
+ session.writeFile(path.join(authDir, "jwt-helper.ts"), (0, auth_generator_1.generateJwtHelperTS)(), managedMeta);
545
544
  console.log(` Created: functions/src/auth/jwt-helper.ts`);
546
545
  }
547
546
  else if (backendLanguage === "csharp") {
548
547
  const authDir = path.join(functionsDir, "Auth");
549
- fs.mkdirSync(authDir, { recursive: true });
550
- fs.writeFileSync(path.join(authDir, "AuthFunctions.cs"), (0, auth_generator_1.generateAuthFunctionsCSharp)(config, provider), "utf-8");
548
+ session.writeFile(path.join(authDir, "AuthFunctions.cs"), (0, auth_generator_1.generateAuthFunctionsCSharp)(config, provider), managedMeta);
551
549
  console.log(` Created: functions/Auth/AuthFunctions.cs`);
552
- fs.writeFileSync(path.join(authDir, "JwtHelper.cs"), (0, auth_generator_1.generateJwtHelperCSharp)(), "utf-8");
550
+ session.writeFile(path.join(authDir, "JwtHelper.cs"), (0, auth_generator_1.generateJwtHelperCSharp)(), managedMeta);
553
551
  console.log(` Created: functions/Auth/JwtHelper.cs`);
554
552
  }
555
553
  else if (backendLanguage === "python") {
556
554
  const blueprintsDir = path.join(functionsDir, "blueprints");
557
- fs.mkdirSync(blueprintsDir, { recursive: true });
558
- fs.writeFileSync(path.join(blueprintsDir, "auth.py"), (0, auth_generator_1.generateAuthFunctionsPython)(config, provider), "utf-8");
555
+ session.writeFile(path.join(blueprintsDir, "auth.py"), (0, auth_generator_1.generateAuthFunctionsPython)(config, provider), managedMeta);
559
556
  console.log(` Created: functions/blueprints/auth.py`);
560
557
  const authDir = path.join(functionsDir, "auth");
561
- fs.mkdirSync(authDir, { recursive: true });
562
- fs.writeFileSync(path.join(authDir, "jwt_helper.py"), (0, auth_generator_1.generateJwtHelperPython)(), "utf-8");
558
+ session.writeFile(path.join(authDir, "jwt_helper.py"), (0, auth_generator_1.generateJwtHelperPython)(), managedMeta);
563
559
  console.log(` Created: functions/auth/jwt_helper.py`);
564
560
  // __init__.py
565
- fs.writeFileSync(path.join(authDir, "__init__.py"), "", "utf-8");
561
+ if (!session.fileExists(path.join(authDir, "__init__.py"))) {
562
+ session.writeFile(path.join(authDir, "__init__.py"), "", onceMeta);
563
+ }
566
564
  // Register auth blueprint in function_app.py
567
565
  const functionAppPath = path.join(functionsDir, "function_app.py");
568
- if (fs.existsSync(functionAppPath)) {
569
- const content = fs.readFileSync(functionAppPath, "utf-8");
566
+ if (session.fileExists(functionAppPath)) {
567
+ const content = session.readFile(functionAppPath);
570
568
  const authImport = "from blueprints.auth import bp as auth_bp";
571
569
  const authRegister = "app.register_blueprint(auth_bp)";
572
570
  if (!content.includes(authImport)) {
573
571
  const marker = "# SwallowKit scaffold registrations";
574
572
  if (content.includes(marker)) {
575
573
  const updated = content.replace(marker, `${authImport}\n${authRegister}\n${marker}`);
576
- fs.writeFileSync(functionAppPath, updated, "utf-8");
574
+ session.writeFile(functionAppPath, updated, extensionMeta);
577
575
  console.log(` Updated: functions/function_app.py (registered auth blueprint)`);
578
576
  }
579
577
  }
580
578
  }
581
579
  }
582
580
  }
583
- function generateBFFAuth(cwd, projectName, sharedPackageName) {
581
+ function generateBFFAuth(cwd, projectName, sharedPackageName, session) {
584
582
  const authApiDir = path.join(cwd, "app", "api", "auth");
585
583
  // Login route
586
584
  const loginDir = path.join(authApiDir, "login");
587
- fs.mkdirSync(loginDir, { recursive: true });
588
- fs.writeFileSync(path.join(loginDir, "route.ts"), (0, auth_generator_1.generateBFFAuthLoginRoute)(projectName, sharedPackageName), "utf-8");
585
+ session.writeFile(path.join(loginDir, "route.ts"), (0, auth_generator_1.generateBFFAuthLoginRoute)(projectName, sharedPackageName), managedMeta);
589
586
  console.log(` Created: app/api/auth/login/route.ts`);
590
587
  // Logout route
591
588
  const logoutDir = path.join(authApiDir, "logout");
592
- fs.mkdirSync(logoutDir, { recursive: true });
593
- fs.writeFileSync(path.join(logoutDir, "route.ts"), (0, auth_generator_1.generateBFFAuthLogoutRoute)(projectName), "utf-8");
589
+ session.writeFile(path.join(logoutDir, "route.ts"), (0, auth_generator_1.generateBFFAuthLogoutRoute)(projectName), managedMeta);
594
590
  console.log(` Created: app/api/auth/logout/route.ts`);
595
591
  // Me route
596
592
  const meDir = path.join(authApiDir, "me");
597
- fs.mkdirSync(meDir, { recursive: true });
598
- fs.writeFileSync(path.join(meDir, "route.ts"), (0, auth_generator_1.generateBFFAuthMeRoute)(), "utf-8");
593
+ session.writeFile(path.join(meDir, "route.ts"), (0, auth_generator_1.generateBFFAuthMeRoute)(), managedMeta);
599
594
  console.log(` Created: app/api/auth/me/route.ts`);
600
595
  }
601
- function updateConfigWithAuth(cwd, provider, config) {
596
+ function updateConfigWithAuth(cwd, provider, config, session) {
602
597
  const configPath = path.join(cwd, "swallowkit.config.js");
603
- if (!fs.existsSync(configPath)) {
598
+ if (!session.fileExists(configPath)) {
604
599
  console.warn(" swallowkit.config.js not found. Please add auth config manually.");
605
600
  return;
606
601
  }
607
- const content = fs.readFileSync(configPath, "utf-8");
602
+ const content = session.readFile(configPath);
608
603
  if (content.includes("auth:") || content.includes("auth :")) {
609
604
  console.log(" 'auth' section already exists in swallowkit.config.js");
610
605
  return;
@@ -642,38 +637,38 @@ ${providerSettings}
642
637
  },
643
638
  `;
644
639
  const newContent = content.substring(0, closingBraceIdx) + authBlock + content.substring(closingBraceIdx);
645
- fs.writeFileSync(configPath, newContent, "utf-8");
640
+ session.writeFile(configPath, newContent, extensionMeta);
646
641
  console.log(` Updated: swallowkit.config.js`);
647
642
  }
648
- function updateEnvironmentFiles(cwd) {
643
+ function updateEnvironmentFiles(cwd, session) {
649
644
  // Update functions/local.settings.json
650
645
  const localSettingsPath = path.join(cwd, "functions", "local.settings.json");
651
- if (fs.existsSync(localSettingsPath)) {
652
- const settings = JSON.parse(fs.readFileSync(localSettingsPath, "utf-8"));
646
+ if (session.fileExists(localSettingsPath)) {
647
+ const settings = JSON.parse(session.readFile(localSettingsPath));
653
648
  if (!settings.Values)
654
649
  settings.Values = {};
655
650
  if (!settings.Values.JWT_SECRET) {
656
651
  settings.Values.JWT_SECRET = "dev-jwt-secret-change-in-production-min-32-chars!!";
657
652
  }
658
- fs.writeFileSync(localSettingsPath, JSON.stringify(settings, null, 2), "utf-8");
653
+ session.writeFile(localSettingsPath, JSON.stringify(settings, null, 2), extensionMeta);
659
654
  console.log(` Updated: functions/local.settings.json`);
660
655
  }
661
656
  // Update .env.example
662
657
  const envExamplePath = path.join(cwd, ".env.example");
663
- if (fs.existsSync(envExamplePath)) {
664
- let content = fs.readFileSync(envExamplePath, "utf-8");
658
+ if (session.fileExists(envExamplePath)) {
659
+ let content = session.readFile(envExamplePath);
665
660
  if (!content.includes("JWT_SECRET")) {
666
661
  content += "\n# Authentication\nJWT_SECRET=your-jwt-secret-key-at-least-32-chars\n";
667
- fs.writeFileSync(envExamplePath, content, "utf-8");
662
+ session.writeFile(envExamplePath, content, extensionMeta);
668
663
  console.log(` Updated: .env.example`);
669
664
  }
670
665
  }
671
666
  }
672
- async function installAuthDependencies(cwd, backendLanguage, provider = "mysql") {
667
+ async function installAuthDependencies(cwd, backendLanguage, provider = "mysql", session) {
673
668
  if (backendLanguage === "typescript") {
674
669
  const funcPkgPath = path.join(cwd, "functions", "package.json");
675
- if (fs.existsSync(funcPkgPath)) {
676
- const funcPkg = JSON.parse(fs.readFileSync(funcPkgPath, "utf-8"));
670
+ if (session.fileExists(funcPkgPath)) {
671
+ const funcPkg = JSON.parse(session.readFile(funcPkgPath));
677
672
  if (!funcPkg.dependencies)
678
673
  funcPkg.dependencies = {};
679
674
  funcPkg.dependencies["jsonwebtoken"] = "^9.0.0";
@@ -691,17 +686,19 @@ async function installAuthDependencies(cwd, backendLanguage, provider = "mysql")
691
686
  funcPkg.devDependencies["@types/bcryptjs"] = "^2.4.0";
692
687
  if (provider === "postgres")
693
688
  funcPkg.devDependencies["@types/pg"] = "^8.11.0";
694
- fs.writeFileSync(funcPkgPath, JSON.stringify(funcPkg, null, 2), "utf-8");
689
+ session.writeFile(funcPkgPath, JSON.stringify(funcPkg, null, 2), extensionMeta);
695
690
  console.log(` Updated: functions/package.json with auth dependencies (${provider})`);
696
691
  }
697
692
  }
698
693
  else if (backendLanguage === "csharp") {
699
694
  // Add NuGet package references to .csproj
700
695
  const functionsDir = path.join(cwd, "functions");
701
- const csprojFiles = fs.readdirSync(functionsDir).filter((f) => f.endsWith(".csproj"));
696
+ const csprojFiles = fs.existsSync(functionsDir)
697
+ ? fs.readdirSync(functionsDir).filter((f) => f.endsWith(".csproj"))
698
+ : [];
702
699
  if (csprojFiles.length > 0) {
703
700
  const csprojPath = path.join(functionsDir, csprojFiles[0]);
704
- let csprojContent = fs.readFileSync(csprojPath, "utf-8");
701
+ let csprojContent = session.readFile(csprojPath);
705
702
  const nugetPackages = [
706
703
  { name: "System.IdentityModel.Tokens.Jwt", version: "7.0.0" },
707
704
  { name: "Microsoft.IdentityModel.Tokens", version: "7.0.0" },
@@ -725,7 +722,7 @@ async function installAuthDependencies(cwd, backendLanguage, provider = "mysql")
725
722
  }
726
723
  }
727
724
  }
728
- fs.writeFileSync(csprojPath, csprojContent, "utf-8");
725
+ session.writeFile(csprojPath, csprojContent, extensionMeta);
729
726
  console.log(` Updated: ${csprojFiles[0]} with auth NuGet packages (${provider})`);
730
727
  }
731
728
  }
@@ -740,18 +737,18 @@ async function installAuthDependencies(cwd, backendLanguage, provider = "mysql")
740
737
  baseDeps.push("psycopg2-binary>=2.9.0");
741
738
  else
742
739
  baseDeps.push("pymssql>=2.2.0");
743
- if (fs.existsSync(requirementsPath)) {
744
- let content = fs.readFileSync(requirementsPath, "utf-8");
740
+ if (session.fileExists(requirementsPath)) {
741
+ let content = session.readFile(requirementsPath);
745
742
  for (const dep of baseDeps) {
746
743
  const pkgName = dep.split(">=")[0].split("==")[0];
747
744
  if (!content.includes(pkgName)) {
748
745
  content += `${dep}\n`;
749
746
  }
750
747
  }
751
- fs.writeFileSync(requirementsPath, content, "utf-8");
748
+ session.writeFile(requirementsPath, content, extensionMeta);
752
749
  }
753
750
  else {
754
- fs.writeFileSync(requirementsPath, baseDeps.join("\n") + "\n", "utf-8");
751
+ session.writeFile(requirementsPath, baseDeps.join("\n") + "\n", extensionMeta);
755
752
  }
756
753
  console.log(` Updated: functions/requirements.txt with auth dependencies`);
757
754
  }