@friggframework/devtools 2.0.0-next.4 → 2.0.0-next.40

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 (197) hide show
  1. package/frigg-cli/.eslintrc.js +141 -0
  2. package/frigg-cli/__tests__/jest.config.js +102 -0
  3. package/frigg-cli/__tests__/unit/commands/build.test.js +483 -0
  4. package/frigg-cli/__tests__/unit/commands/install.test.js +418 -0
  5. package/frigg-cli/__tests__/unit/commands/ui.test.js +592 -0
  6. package/frigg-cli/__tests__/utils/command-tester.js +170 -0
  7. package/frigg-cli/__tests__/utils/mock-factory.js +270 -0
  8. package/frigg-cli/__tests__/utils/test-fixtures.js +463 -0
  9. package/frigg-cli/__tests__/utils/test-setup.js +286 -0
  10. package/frigg-cli/build-command/index.js +54 -0
  11. package/frigg-cli/deploy-command/index.js +175 -0
  12. package/frigg-cli/generate-command/__tests__/generate-command.test.js +312 -0
  13. package/frigg-cli/generate-command/azure-generator.js +43 -0
  14. package/frigg-cli/generate-command/gcp-generator.js +47 -0
  15. package/frigg-cli/generate-command/index.js +332 -0
  16. package/frigg-cli/generate-command/terraform-generator.js +555 -0
  17. package/frigg-cli/generate-iam-command.js +115 -0
  18. package/frigg-cli/index.js +47 -1
  19. package/frigg-cli/index.test.js +1 -4
  20. package/frigg-cli/init-command/backend-first-handler.js +756 -0
  21. package/frigg-cli/init-command/index.js +93 -0
  22. package/frigg-cli/init-command/template-handler.js +143 -0
  23. package/frigg-cli/install-command/index.js +1 -4
  24. package/frigg-cli/package.json +51 -0
  25. package/frigg-cli/start-command/index.js +24 -4
  26. package/frigg-cli/test/init-command.test.js +180 -0
  27. package/frigg-cli/test/npm-registry.test.js +319 -0
  28. package/frigg-cli/ui-command/index.js +154 -0
  29. package/frigg-cli/utils/app-resolver.js +319 -0
  30. package/frigg-cli/utils/backend-path.js +16 -17
  31. package/frigg-cli/utils/npm-registry.js +167 -0
  32. package/frigg-cli/utils/process-manager.js +199 -0
  33. package/frigg-cli/utils/repo-detection.js +405 -0
  34. package/infrastructure/DEPLOYMENT-INSTRUCTIONS.md +268 -0
  35. package/infrastructure/GENERATE-IAM-DOCS.md +278 -0
  36. package/infrastructure/IAM-POLICY-TEMPLATES.md +176 -0
  37. package/infrastructure/README.md +443 -0
  38. package/infrastructure/WEBSOCKET-CONFIGURATION.md +105 -0
  39. package/infrastructure/__tests__/fixtures/mock-aws-resources.js +391 -0
  40. package/infrastructure/__tests__/helpers/test-utils.js +277 -0
  41. package/infrastructure/aws-discovery.js +1176 -0
  42. package/infrastructure/aws-discovery.test.js +1220 -0
  43. package/infrastructure/build-time-discovery.js +206 -0
  44. package/infrastructure/build-time-discovery.test.js +378 -0
  45. package/infrastructure/create-frigg-infrastructure.js +3 -5
  46. package/infrastructure/env-validator.js +77 -0
  47. package/infrastructure/frigg-deployment-iam-stack.yaml +401 -0
  48. package/infrastructure/iam-generator.js +836 -0
  49. package/infrastructure/iam-generator.test.js +172 -0
  50. package/infrastructure/iam-policy-basic.json +218 -0
  51. package/infrastructure/iam-policy-full.json +288 -0
  52. package/infrastructure/integration.test.js +383 -0
  53. package/infrastructure/run-discovery.js +110 -0
  54. package/infrastructure/serverless-template.js +1472 -138
  55. package/infrastructure/serverless-template.test.js +1759 -0
  56. package/management-ui/.eslintrc.js +22 -0
  57. package/management-ui/README.md +203 -0
  58. package/management-ui/components.json +21 -0
  59. package/management-ui/docs/phase2-integration-guide.md +320 -0
  60. package/management-ui/index.html +13 -0
  61. package/management-ui/package-lock.json +16517 -0
  62. package/management-ui/package.json +76 -0
  63. package/management-ui/packages/devtools/frigg-cli/ui-command/index.js +302 -0
  64. package/management-ui/postcss.config.js +6 -0
  65. package/management-ui/server/api/backend.js +256 -0
  66. package/management-ui/server/api/cli.js +315 -0
  67. package/management-ui/server/api/codegen.js +663 -0
  68. package/management-ui/server/api/connections.js +857 -0
  69. package/management-ui/server/api/discovery.js +185 -0
  70. package/management-ui/server/api/environment/index.js +1 -0
  71. package/management-ui/server/api/environment/router.js +378 -0
  72. package/management-ui/server/api/environment.js +328 -0
  73. package/management-ui/server/api/integrations.js +876 -0
  74. package/management-ui/server/api/logs.js +248 -0
  75. package/management-ui/server/api/monitoring.js +282 -0
  76. package/management-ui/server/api/open-ide.js +31 -0
  77. package/management-ui/server/api/project.js +1029 -0
  78. package/management-ui/server/api/users/sessions.js +371 -0
  79. package/management-ui/server/api/users/simulation.js +254 -0
  80. package/management-ui/server/api/users.js +362 -0
  81. package/management-ui/server/api-contract.md +275 -0
  82. package/management-ui/server/index.js +873 -0
  83. package/management-ui/server/middleware/errorHandler.js +93 -0
  84. package/management-ui/server/middleware/security.js +32 -0
  85. package/management-ui/server/processManager.js +296 -0
  86. package/management-ui/server/server.js +346 -0
  87. package/management-ui/server/services/aws-monitor.js +413 -0
  88. package/management-ui/server/services/npm-registry.js +347 -0
  89. package/management-ui/server/services/template-engine.js +538 -0
  90. package/management-ui/server/utils/cliIntegration.js +220 -0
  91. package/management-ui/server/utils/environment/auditLogger.js +471 -0
  92. package/management-ui/server/utils/environment/awsParameterStore.js +264 -0
  93. package/management-ui/server/utils/environment/encryption.js +278 -0
  94. package/management-ui/server/utils/environment/envFileManager.js +286 -0
  95. package/management-ui/server/utils/import-commonjs.js +28 -0
  96. package/management-ui/server/utils/response.js +83 -0
  97. package/management-ui/server/websocket/handler.js +325 -0
  98. package/management-ui/src/App.jsx +109 -0
  99. package/management-ui/src/assets/FriggLogo.svg +1 -0
  100. package/management-ui/src/components/AppRouter.jsx +65 -0
  101. package/management-ui/src/components/Button.jsx +70 -0
  102. package/management-ui/src/components/Card.jsx +97 -0
  103. package/management-ui/src/components/EnvironmentCompare.jsx +400 -0
  104. package/management-ui/src/components/EnvironmentEditor.jsx +372 -0
  105. package/management-ui/src/components/EnvironmentImportExport.jsx +469 -0
  106. package/management-ui/src/components/EnvironmentSchema.jsx +491 -0
  107. package/management-ui/src/components/EnvironmentSecurity.jsx +463 -0
  108. package/management-ui/src/components/ErrorBoundary.jsx +73 -0
  109. package/management-ui/src/components/IntegrationCard.jsx +481 -0
  110. package/management-ui/src/components/IntegrationCardEnhanced.jsx +770 -0
  111. package/management-ui/src/components/IntegrationExplorer.jsx +379 -0
  112. package/management-ui/src/components/IntegrationStatus.jsx +336 -0
  113. package/management-ui/src/components/Layout.jsx +716 -0
  114. package/management-ui/src/components/LoadingSpinner.jsx +113 -0
  115. package/management-ui/src/components/RepositoryPicker.jsx +248 -0
  116. package/management-ui/src/components/SessionMonitor.jsx +350 -0
  117. package/management-ui/src/components/StatusBadge.jsx +208 -0
  118. package/management-ui/src/components/UserContextSwitcher.jsx +212 -0
  119. package/management-ui/src/components/UserSimulation.jsx +327 -0
  120. package/management-ui/src/components/Welcome.jsx +434 -0
  121. package/management-ui/src/components/codegen/APIEndpointGenerator.jsx +637 -0
  122. package/management-ui/src/components/codegen/APIModuleSelector.jsx +227 -0
  123. package/management-ui/src/components/codegen/CodeGenerationWizard.jsx +247 -0
  124. package/management-ui/src/components/codegen/CodePreviewEditor.jsx +316 -0
  125. package/management-ui/src/components/codegen/DynamicModuleForm.jsx +271 -0
  126. package/management-ui/src/components/codegen/FormBuilder.jsx +737 -0
  127. package/management-ui/src/components/codegen/IntegrationGenerator.jsx +855 -0
  128. package/management-ui/src/components/codegen/ProjectScaffoldWizard.jsx +797 -0
  129. package/management-ui/src/components/codegen/SchemaBuilder.jsx +303 -0
  130. package/management-ui/src/components/codegen/TemplateSelector.jsx +586 -0
  131. package/management-ui/src/components/codegen/index.js +10 -0
  132. package/management-ui/src/components/connections/ConnectionConfigForm.jsx +362 -0
  133. package/management-ui/src/components/connections/ConnectionHealthMonitor.jsx +182 -0
  134. package/management-ui/src/components/connections/ConnectionTester.jsx +200 -0
  135. package/management-ui/src/components/connections/EntityRelationshipMapper.jsx +292 -0
  136. package/management-ui/src/components/connections/OAuthFlow.jsx +204 -0
  137. package/management-ui/src/components/connections/index.js +5 -0
  138. package/management-ui/src/components/index.js +21 -0
  139. package/management-ui/src/components/monitoring/APIGatewayMetrics.jsx +222 -0
  140. package/management-ui/src/components/monitoring/LambdaMetrics.jsx +169 -0
  141. package/management-ui/src/components/monitoring/MetricsChart.jsx +197 -0
  142. package/management-ui/src/components/monitoring/MonitoringDashboard.jsx +393 -0
  143. package/management-ui/src/components/monitoring/SQSMetrics.jsx +246 -0
  144. package/management-ui/src/components/monitoring/index.js +6 -0
  145. package/management-ui/src/components/monitoring/monitoring.css +218 -0
  146. package/management-ui/src/components/theme-provider.jsx +52 -0
  147. package/management-ui/src/components/theme-toggle.jsx +39 -0
  148. package/management-ui/src/components/ui/badge.tsx +36 -0
  149. package/management-ui/src/components/ui/button.test.jsx +56 -0
  150. package/management-ui/src/components/ui/button.tsx +57 -0
  151. package/management-ui/src/components/ui/card.tsx +76 -0
  152. package/management-ui/src/components/ui/dropdown-menu.tsx +199 -0
  153. package/management-ui/src/components/ui/select.tsx +157 -0
  154. package/management-ui/src/components/ui/skeleton.jsx +15 -0
  155. package/management-ui/src/hooks/useFrigg.jsx +601 -0
  156. package/management-ui/src/hooks/useSocket.jsx +58 -0
  157. package/management-ui/src/index.css +193 -0
  158. package/management-ui/src/lib/utils.ts +6 -0
  159. package/management-ui/src/main.jsx +10 -0
  160. package/management-ui/src/pages/CodeGeneration.jsx +14 -0
  161. package/management-ui/src/pages/Connections.jsx +252 -0
  162. package/management-ui/src/pages/ConnectionsEnhanced.jsx +633 -0
  163. package/management-ui/src/pages/Dashboard.jsx +311 -0
  164. package/management-ui/src/pages/Environment.jsx +314 -0
  165. package/management-ui/src/pages/IntegrationConfigure.jsx +669 -0
  166. package/management-ui/src/pages/IntegrationDiscovery.jsx +567 -0
  167. package/management-ui/src/pages/IntegrationTest.jsx +742 -0
  168. package/management-ui/src/pages/Integrations.jsx +253 -0
  169. package/management-ui/src/pages/Monitoring.jsx +17 -0
  170. package/management-ui/src/pages/Simulation.jsx +155 -0
  171. package/management-ui/src/pages/Users.jsx +492 -0
  172. package/management-ui/src/services/api.js +41 -0
  173. package/management-ui/src/services/apiModuleService.js +193 -0
  174. package/management-ui/src/services/websocket-handlers.js +120 -0
  175. package/management-ui/src/test/api/project.test.js +273 -0
  176. package/management-ui/src/test/components/Welcome.test.jsx +378 -0
  177. package/management-ui/src/test/mocks/server.js +178 -0
  178. package/management-ui/src/test/setup.js +61 -0
  179. package/management-ui/src/test/utils/test-utils.jsx +134 -0
  180. package/management-ui/src/utils/repository.js +98 -0
  181. package/management-ui/src/utils/repository.test.js +118 -0
  182. package/management-ui/src/workflows/phase2-integration-workflows.js +884 -0
  183. package/management-ui/tailwind.config.js +63 -0
  184. package/management-ui/tsconfig.json +37 -0
  185. package/management-ui/tsconfig.node.json +10 -0
  186. package/management-ui/vite.config.js +26 -0
  187. package/management-ui/vitest.config.js +38 -0
  188. package/package.json +20 -9
  189. package/infrastructure/app-handler-helpers.js +0 -57
  190. package/infrastructure/backend-utils.js +0 -90
  191. package/infrastructure/routers/auth.js +0 -26
  192. package/infrastructure/routers/integration-defined-routers.js +0 -37
  193. package/infrastructure/routers/middleware/loadUser.js +0 -15
  194. package/infrastructure/routers/middleware/requireLoggedInUser.js +0 -12
  195. package/infrastructure/routers/user.js +0 -41
  196. package/infrastructure/routers/websocket.js +0 -55
  197. package/infrastructure/workers/integration-defined-workers.js +0 -24
@@ -0,0 +1,286 @@
1
+ /**
2
+ * Global test setup for Frigg CLI tests
3
+ * This file is executed before each test file
4
+ */
5
+
6
+ // Store original environment
7
+ const originalEnv = process.env;
8
+ const originalConsole = { ...console };
9
+ const originalProcess = { ...process };
10
+
11
+ // Mock console to prevent noisy output during tests
12
+ global.console = {
13
+ ...console,
14
+ log: jest.fn(),
15
+ info: jest.fn(),
16
+ warn: jest.fn(),
17
+ error: jest.fn(),
18
+ debug: jest.fn()
19
+ };
20
+
21
+ // Set up test environment variables
22
+ process.env = {
23
+ ...originalEnv,
24
+ NODE_ENV: 'test',
25
+ HOME: '/mock/home',
26
+ PATH: '/mock/path',
27
+ CI: 'true'
28
+ };
29
+
30
+ // Global setup before each test
31
+ beforeEach(() => {
32
+ // Clear all mocks
33
+ jest.clearAllMocks();
34
+
35
+ // Reset timers
36
+ jest.clearAllTimers();
37
+
38
+ // Reset modules
39
+ jest.resetModules();
40
+
41
+ // Restore original process methods
42
+ process.exit = originalProcess.exit;
43
+ process.cwd = originalProcess.cwd;
44
+
45
+ // Mock process.exit to prevent actual exit
46
+ process.exit = jest.fn();
47
+
48
+ // Mock process.cwd to return predictable path
49
+ process.cwd = jest.fn().mockReturnValue('/mock/cwd');
50
+
51
+ // Reset console mocks
52
+ global.console.log.mockClear();
53
+ global.console.info.mockClear();
54
+ global.console.warn.mockClear();
55
+ global.console.error.mockClear();
56
+ global.console.debug.mockClear();
57
+ });
58
+
59
+ // Global cleanup after each test
60
+ afterEach(() => {
61
+ // Restore environment
62
+ process.env = { ...originalEnv };
63
+
64
+ // Restore process methods
65
+ process.exit = originalProcess.exit;
66
+ process.cwd = originalProcess.cwd;
67
+
68
+ // Clear any remaining timers
69
+ jest.clearAllTimers();
70
+
71
+ // Unmock all modules
72
+ jest.restoreAllMocks();
73
+ });
74
+
75
+ // Global teardown after all tests
76
+ afterAll(() => {
77
+ // Restore original environment completely
78
+ process.env = originalEnv;
79
+
80
+ // Restore original console
81
+ global.console = originalConsole;
82
+
83
+ // Restore original process
84
+ Object.assign(process, originalProcess);
85
+ });
86
+
87
+ // Custom matchers for CLI testing
88
+ expect.extend({
89
+ toBeValidExitCode(received) {
90
+ const validCodes = [0, 1, 2];
91
+ const pass = validCodes.includes(received);
92
+
93
+ if (pass) {
94
+ return {
95
+ message: () => `expected ${received} not to be a valid exit code`,
96
+ pass: true
97
+ };
98
+ } else {
99
+ return {
100
+ message: () => `expected ${received} to be a valid exit code (0, 1, or 2)`,
101
+ pass: false
102
+ };
103
+ }
104
+ },
105
+
106
+ toHaveLoggedError(received, expected) {
107
+ const errorLogs = global.console.error.mock.calls;
108
+ const pass = errorLogs.some(call =>
109
+ call.some(arg =>
110
+ typeof arg === 'string' && arg.includes(expected)
111
+ )
112
+ );
113
+
114
+ if (pass) {
115
+ return {
116
+ message: () => `expected not to have logged error containing "${expected}"`,
117
+ pass: true
118
+ };
119
+ } else {
120
+ return {
121
+ message: () => `expected to have logged error containing "${expected}"`,
122
+ pass: false
123
+ };
124
+ }
125
+ },
126
+
127
+ toHaveLoggedInfo(received, expected) {
128
+ const infoLogs = global.console.info.mock.calls;
129
+ const pass = infoLogs.some(call =>
130
+ call.some(arg =>
131
+ typeof arg === 'string' && arg.includes(expected)
132
+ )
133
+ );
134
+
135
+ if (pass) {
136
+ return {
137
+ message: () => `expected not to have logged info containing "${expected}"`,
138
+ pass: true
139
+ };
140
+ } else {
141
+ return {
142
+ message: () => `expected to have logged info containing "${expected}"`,
143
+ pass: false
144
+ };
145
+ }
146
+ },
147
+
148
+ toBeWithinTimeLimit(received, limit) {
149
+ const pass = received <= limit;
150
+
151
+ if (pass) {
152
+ return {
153
+ message: () => `expected ${received}ms not to be within ${limit}ms`,
154
+ pass: true
155
+ };
156
+ } else {
157
+ return {
158
+ message: () => `expected ${received}ms to be within ${limit}ms`,
159
+ pass: false
160
+ };
161
+ }
162
+ }
163
+ });
164
+
165
+ // Helper functions available in all tests
166
+ global.TestHelpers = {
167
+ /**
168
+ * Create a temporary directory for tests
169
+ */
170
+ createTempDir() {
171
+ const fs = require('fs');
172
+ const path = require('path');
173
+ const os = require('os');
174
+
175
+ return fs.mkdtempSync(path.join(os.tmpdir(), 'frigg-cli-test-'));
176
+ },
177
+
178
+ /**
179
+ * Clean up temporary directory
180
+ */
181
+ cleanupTempDir(dirPath) {
182
+ const fs = require('fs');
183
+
184
+ if (fs.existsSync(dirPath)) {
185
+ fs.rmSync(dirPath, { recursive: true, force: true });
186
+ }
187
+ },
188
+
189
+ /**
190
+ * Create a mock package.json file
191
+ */
192
+ createMockPackageJson(overrides = {}) {
193
+ return JSON.stringify({
194
+ name: 'test-package',
195
+ version: '1.0.0',
196
+ main: 'index.js',
197
+ scripts: {
198
+ test: 'jest',
199
+ start: 'node index.js'
200
+ },
201
+ dependencies: {},
202
+ devDependencies: {},
203
+ ...overrides
204
+ }, null, 2);
205
+ },
206
+
207
+ /**
208
+ * Create a mock frigg.config.json file
209
+ */
210
+ createMockFriggConfig(overrides = {}) {
211
+ return JSON.stringify({
212
+ stage: 'dev',
213
+ region: 'us-east-1',
214
+ profile: 'default',
215
+ backend: {
216
+ runtime: 'nodejs18.x',
217
+ timeout: 30,
218
+ memory: 128
219
+ },
220
+ ...overrides
221
+ }, null, 2);
222
+ },
223
+
224
+ /**
225
+ * Wait for a specific amount of time
226
+ */
227
+ async delay(ms) {
228
+ return new Promise(resolve => setTimeout(resolve, ms));
229
+ },
230
+
231
+ /**
232
+ * Generate a random string for test data
233
+ */
234
+ randomString(length = 10) {
235
+ return Math.random().toString(36).substring(2, length + 2);
236
+ },
237
+
238
+ /**
239
+ * Simulate file system structure
240
+ */
241
+ mockFileSystem(structure) {
242
+ const fs = require('fs');
243
+
244
+ const originalExistsSync = fs.existsSync;
245
+ const originalReadFileSync = fs.readFileSync;
246
+ const originalWriteFileSync = fs.writeFileSync;
247
+
248
+ fs.existsSync = jest.fn((path) => {
249
+ return structure.hasOwnProperty(path);
250
+ });
251
+
252
+ fs.readFileSync = jest.fn((path) => {
253
+ if (structure.hasOwnProperty(path)) {
254
+ return structure[path];
255
+ }
256
+ throw new Error(`ENOENT: no such file or directory, open '${path}'`);
257
+ });
258
+
259
+ fs.writeFileSync = jest.fn((path, data) => {
260
+ structure[path] = data;
261
+ });
262
+
263
+ return {
264
+ restore() {
265
+ fs.existsSync = originalExistsSync;
266
+ fs.readFileSync = originalReadFileSync;
267
+ fs.writeFileSync = originalWriteFileSync;
268
+ }
269
+ };
270
+ }
271
+ };
272
+
273
+ // Global timeout for all tests
274
+ jest.setTimeout(30000);
275
+
276
+ // Suppress specific warnings during tests
277
+ const originalWarn = console.warn;
278
+ console.warn = (...args) => {
279
+ // Suppress specific warnings that are expected during testing
280
+ const message = args.join(' ');
281
+ if (message.includes('ExperimentalWarning') ||
282
+ message.includes('DeprecationWarning')) {
283
+ return;
284
+ }
285
+ originalWarn.apply(console, args);
286
+ };
@@ -0,0 +1,54 @@
1
+ const { spawnSync } = require('child_process');
2
+ const path = require('path');
3
+
4
+ async function buildCommand(options) {
5
+ console.log('Building the serverless application...');
6
+
7
+ // AWS discovery is now handled directly in serverless-template.js
8
+ console.log('📦 Packaging serverless application...');
9
+ const backendPath = path.resolve(process.cwd());
10
+ const infrastructurePath = 'infrastructure.js';
11
+ const command = 'serverless';
12
+ const serverlessArgs = [
13
+ 'package',
14
+ '--config',
15
+ infrastructurePath,
16
+ '--stage',
17
+ options.stage
18
+ ];
19
+
20
+ // Add support for --verbose option
21
+ if (options.verbose) {
22
+ serverlessArgs.push('--verbose');
23
+ }
24
+
25
+ console.log('Running command from ', backendPath);
26
+ console.log('Serverless Command:', command, serverlessArgs.join(' '));
27
+
28
+ const result = spawnSync(command, serverlessArgs, {
29
+ cwd: backendPath,
30
+ stdio: 'inherit',
31
+ shell: true,
32
+ env: {
33
+ ...process.env,
34
+ NODE_PATH: path.resolve(backendPath, 'node_modules'),
35
+ }
36
+ });
37
+
38
+ if (result.status !== 0) {
39
+ console.error(`Serverless build failed with code ${result.status}`);
40
+ process.exit(1);
41
+ }
42
+
43
+ // childProcess.on('error', (error) => {
44
+ // console.error(`Error executing command: ${error.message}`);
45
+ // });
46
+
47
+ // childProcess.on('close', (code) => {
48
+ // if (code !== 0) {
49
+ // console.log(`Child process exited with code ${code}`);
50
+ // }
51
+ // });
52
+ }
53
+
54
+ module.exports = { buildCommand };
@@ -0,0 +1,175 @@
1
+ const { spawn } = require('child_process');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ // Configuration constants
6
+ const PATHS = {
7
+ APP_DEFINITION: 'index.js',
8
+ INFRASTRUCTURE: 'infrastructure.js'
9
+ };
10
+
11
+ const COMMANDS = {
12
+ SERVERLESS: 'serverless'
13
+ };
14
+
15
+ /**
16
+ * Constructs filtered environment variables for serverless deployment
17
+ * @param {string[]} appDefinedVariables - Array of environment variable names from app definition
18
+ * @returns {Object} Filtered environment variables object
19
+ */
20
+ function buildFilteredEnvironment(appDefinedVariables) {
21
+ return {
22
+ // Essential system variables needed to run serverless
23
+ PATH: process.env.PATH,
24
+ HOME: process.env.HOME,
25
+ USER: process.env.USER,
26
+
27
+ // AWS credentials and configuration (all AWS_ prefixed variables)
28
+ ...Object.fromEntries(
29
+ Object.entries(process.env).filter(([key]) =>
30
+ key.startsWith('AWS_')
31
+ )
32
+ ),
33
+
34
+ // App-defined environment variables
35
+ ...Object.fromEntries(
36
+ appDefinedVariables
37
+ .map((key) => [key, process.env[key]])
38
+ .filter(([_, value]) => value !== undefined)
39
+ ),
40
+ };
41
+ }
42
+
43
+ /**
44
+ * Loads and parses the app definition from index.js
45
+ * @returns {Object|null} App definition object or null if not found
46
+ */
47
+ function loadAppDefinition() {
48
+ const appDefPath = path.join(process.cwd(), PATHS.APP_DEFINITION);
49
+
50
+ if (!fs.existsSync(appDefPath)) {
51
+ return null;
52
+ }
53
+
54
+ try {
55
+ const { Definition } = require(appDefPath);
56
+ return Definition;
57
+ } catch (error) {
58
+ console.warn('Could not load appDefinition environment config:', error.message);
59
+ return null;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Extracts environment variable names from app definition
65
+ * @param {Object} appDefinition - App definition object
66
+ * @returns {string[]} Array of environment variable names
67
+ */
68
+ function extractEnvironmentVariables(appDefinition) {
69
+ if (!appDefinition?.environment) {
70
+ return [];
71
+ }
72
+
73
+ console.log('🔧 Loading environment configuration from appDefinition...');
74
+
75
+ const appDefinedVariables = Object.keys(appDefinition.environment).filter(
76
+ (key) => appDefinition.environment[key] === true
77
+ );
78
+
79
+ console.log(` Found ${appDefinedVariables.length} environment variables: ${appDefinedVariables.join(', ')}`);
80
+ return appDefinedVariables;
81
+ }
82
+
83
+ /**
84
+ * Handles environment validation warnings
85
+ * @param {Object} validation - Validation result object
86
+ * @param {Object} options - Deploy command options
87
+ */
88
+ function handleValidationWarnings(validation, options) {
89
+ if (validation.missing.length === 0 || options.skipEnvValidation) {
90
+ return;
91
+ }
92
+
93
+ console.warn(`⚠️ Warning: Missing ${validation.missing.length} environment variables: ${validation.missing.join(', ')}`);
94
+ console.warn(' These variables are optional and deployment will continue');
95
+ console.warn(' Run with --skip-env-validation to bypass this check');
96
+ }
97
+
98
+ /**
99
+ * Validates environment variables and builds filtered environment
100
+ * @param {Object} appDefinition - App definition object
101
+ * @param {Object} options - Deploy command options
102
+ * @returns {Object} Filtered environment variables
103
+ */
104
+ function validateAndBuildEnvironment(appDefinition, options) {
105
+ if (!appDefinition) {
106
+ return buildFilteredEnvironment([]);
107
+ }
108
+
109
+ const appDefinedVariables = extractEnvironmentVariables(appDefinition);
110
+
111
+ // Try to use the env-validator if available
112
+ try {
113
+ const { validateEnvironmentVariables } = require('@friggframework/devtools/infrastructure/env-validator');
114
+ const validation = validateEnvironmentVariables(appDefinition);
115
+
116
+ handleValidationWarnings(validation, options);
117
+ return buildFilteredEnvironment(appDefinedVariables);
118
+
119
+ } catch (validatorError) {
120
+ // Validator not available, do basic validation
121
+ const missingVariables = appDefinedVariables.filter((variable) => !process.env[variable]);
122
+
123
+ if (missingVariables.length > 0) {
124
+ console.warn(`⚠️ Warning: Missing ${missingVariables.length} environment variables: ${missingVariables.join(', ')}`);
125
+ console.warn(' These variables are optional and deployment will continue');
126
+ console.warn(' Set them in your CI/CD environment or .env file if needed');
127
+ }
128
+
129
+ return buildFilteredEnvironment(appDefinedVariables);
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Executes the serverless deployment command
135
+ * @param {Object} environment - Environment variables to pass to serverless
136
+ * @param {Object} options - Deploy command options
137
+ */
138
+ function executeServerlessDeployment(environment, options) {
139
+ console.log('🚀 Deploying serverless application...');
140
+
141
+ const serverlessArgs = [
142
+ 'deploy',
143
+ '--config',
144
+ PATHS.INFRASTRUCTURE,
145
+ '--stage',
146
+ options.stage,
147
+ ];
148
+
149
+ const childProcess = spawn(COMMANDS.SERVERLESS, serverlessArgs, {
150
+ cwd: path.resolve(process.cwd()),
151
+ stdio: 'inherit',
152
+ env: environment,
153
+ });
154
+
155
+ childProcess.on('error', (error) => {
156
+ console.error(`Error executing command: ${error.message}`);
157
+ });
158
+
159
+ childProcess.on('close', (code) => {
160
+ if (code !== 0) {
161
+ console.log(`Child process exited with code ${code}`);
162
+ }
163
+ });
164
+ }
165
+
166
+ async function deployCommand(options) {
167
+ console.log('Deploying the serverless application...');
168
+
169
+ const appDefinition = loadAppDefinition();
170
+ const environment = validateAndBuildEnvironment(appDefinition, options);
171
+
172
+ executeServerlessDeployment(environment, options);
173
+ }
174
+
175
+ module.exports = { deployCommand };