@friggframework/devtools 2.0.0--canary.549.70ef06a.0 → 2.0.0--canary.545.ccb5010.0

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 (127) hide show
  1. package/frigg-cli/README.md +1 -1
  2. package/frigg-cli/__tests__/application/use-cases/AddApiModuleToIntegrationUseCase.test.js +326 -0
  3. package/frigg-cli/__tests__/application/use-cases/CreateApiModuleUseCase.test.js +337 -0
  4. package/frigg-cli/__tests__/domain/entities/ApiModule.test.js +373 -0
  5. package/frigg-cli/__tests__/domain/entities/AppDefinition.test.js +313 -0
  6. package/frigg-cli/__tests__/domain/services/IntegrationValidator.test.js +269 -0
  7. package/frigg-cli/__tests__/domain/value-objects/IntegrationName.test.js +82 -0
  8. package/frigg-cli/__tests__/infrastructure/adapters/IntegrationJsUpdater.test.js +408 -0
  9. package/frigg-cli/__tests__/infrastructure/repositories/FileSystemApiModuleRepository.test.js +583 -0
  10. package/frigg-cli/__tests__/infrastructure/repositories/FileSystemAppDefinitionRepository.test.js +314 -0
  11. package/frigg-cli/__tests__/infrastructure/repositories/FileSystemIntegrationRepository.test.js +383 -0
  12. package/frigg-cli/__tests__/unit/commands/build.test.js +1 -1
  13. package/frigg-cli/__tests__/unit/commands/doctor.test.js +0 -2
  14. package/frigg-cli/__tests__/unit/commands/init.test.js +406 -0
  15. package/frigg-cli/__tests__/unit/commands/install.test.js +23 -19
  16. package/frigg-cli/__tests__/unit/commands/provider-dispatch.test.js +383 -0
  17. package/frigg-cli/__tests__/unit/commands/repair.test.js +275 -0
  18. package/frigg-cli/__tests__/unit/dependencies.test.js +2 -2
  19. package/frigg-cli/__tests__/unit/start-command/application/RunPreflightChecksUseCase.test.js +411 -0
  20. package/frigg-cli/__tests__/unit/start-command/infrastructure/DatabaseAdapter.test.js +405 -0
  21. package/frigg-cli/__tests__/unit/start-command/infrastructure/DockerAdapter.test.js +496 -0
  22. package/frigg-cli/__tests__/unit/start-command/presentation/InteractivePromptAdapter.test.js +474 -0
  23. package/frigg-cli/__tests__/unit/utils/output.test.js +196 -0
  24. package/frigg-cli/application/use-cases/AddApiModuleToIntegrationUseCase.js +93 -0
  25. package/frigg-cli/application/use-cases/CreateApiModuleUseCase.js +93 -0
  26. package/frigg-cli/application/use-cases/CreateIntegrationUseCase.js +103 -0
  27. package/frigg-cli/build-command/index.js +123 -11
  28. package/frigg-cli/container.js +172 -0
  29. package/frigg-cli/deploy-command/index.js +83 -1
  30. package/frigg-cli/docs/OUTPUT_MIGRATION_GUIDE.md +286 -0
  31. package/frigg-cli/doctor-command/index.js +37 -16
  32. package/frigg-cli/domain/entities/ApiModule.js +272 -0
  33. package/frigg-cli/domain/entities/AppDefinition.js +227 -0
  34. package/frigg-cli/domain/entities/Integration.js +198 -0
  35. package/frigg-cli/domain/exceptions/DomainException.js +24 -0
  36. package/frigg-cli/domain/ports/IApiModuleRepository.js +53 -0
  37. package/frigg-cli/domain/ports/IAppDefinitionRepository.js +43 -0
  38. package/frigg-cli/domain/ports/IIntegrationRepository.js +61 -0
  39. package/frigg-cli/domain/services/IntegrationValidator.js +185 -0
  40. package/frigg-cli/domain/value-objects/IntegrationId.js +42 -0
  41. package/frigg-cli/domain/value-objects/IntegrationName.js +60 -0
  42. package/frigg-cli/domain/value-objects/SemanticVersion.js +70 -0
  43. package/frigg-cli/generate-iam-command.js +21 -1
  44. package/frigg-cli/index.js +21 -6
  45. package/frigg-cli/index.test.js +7 -2
  46. package/frigg-cli/infrastructure/UnitOfWork.js +46 -0
  47. package/frigg-cli/infrastructure/adapters/BackendJsUpdater.js +197 -0
  48. package/frigg-cli/infrastructure/adapters/FileSystemAdapter.js +224 -0
  49. package/frigg-cli/infrastructure/adapters/IntegrationJsUpdater.js +249 -0
  50. package/frigg-cli/infrastructure/adapters/SchemaValidator.js +92 -0
  51. package/frigg-cli/infrastructure/repositories/FileSystemApiModuleRepository.js +373 -0
  52. package/frigg-cli/infrastructure/repositories/FileSystemAppDefinitionRepository.js +116 -0
  53. package/frigg-cli/infrastructure/repositories/FileSystemIntegrationRepository.js +277 -0
  54. package/frigg-cli/init-command/backend-first-handler.js +124 -42
  55. package/frigg-cli/init-command/index.js +2 -1
  56. package/frigg-cli/init-command/template-handler.js +13 -3
  57. package/frigg-cli/install-command/backend-js.js +3 -3
  58. package/frigg-cli/install-command/environment-variables.js +16 -19
  59. package/frigg-cli/install-command/environment-variables.test.js +12 -13
  60. package/frigg-cli/install-command/index.js +14 -9
  61. package/frigg-cli/install-command/integration-file.js +3 -3
  62. package/frigg-cli/install-command/validate-package.js +5 -9
  63. package/frigg-cli/jest.config.js +4 -1
  64. package/frigg-cli/package-lock.json +16226 -0
  65. package/frigg-cli/repair-command/index.js +121 -128
  66. package/frigg-cli/start-command/application/RunPreflightChecksUseCase.js +376 -0
  67. package/frigg-cli/start-command/index.js +324 -2
  68. package/frigg-cli/start-command/infrastructure/DatabaseAdapter.js +591 -0
  69. package/frigg-cli/start-command/infrastructure/DockerAdapter.js +306 -0
  70. package/frigg-cli/start-command/presentation/InteractivePromptAdapter.js +329 -0
  71. package/frigg-cli/templates/backend/.env.example +62 -0
  72. package/frigg-cli/templates/backend/.eslintrc.json +12 -0
  73. package/frigg-cli/templates/backend/.prettierrc +6 -0
  74. package/frigg-cli/templates/backend/docker-compose.yml +22 -0
  75. package/frigg-cli/templates/backend/index.js +96 -0
  76. package/frigg-cli/templates/backend/infrastructure.js +12 -0
  77. package/frigg-cli/templates/backend/jest.config.js +17 -0
  78. package/frigg-cli/templates/backend/package.json +50 -0
  79. package/frigg-cli/templates/backend/src/api-modules/.gitkeep +10 -0
  80. package/frigg-cli/templates/backend/src/base/.gitkeep +7 -0
  81. package/frigg-cli/templates/backend/src/integrations/.gitkeep +10 -0
  82. package/frigg-cli/templates/backend/src/integrations/ExampleIntegration.js +65 -0
  83. package/frigg-cli/templates/backend/src/utils/.gitkeep +7 -0
  84. package/frigg-cli/templates/backend/test/setup.js +30 -0
  85. package/frigg-cli/templates/backend/ui-extensions/.gitkeep +0 -0
  86. package/frigg-cli/templates/backend/ui-extensions/README.md +77 -0
  87. package/frigg-cli/ui-command/index.js +58 -36
  88. package/frigg-cli/utils/__tests__/provider-helper.test.js +55 -0
  89. package/frigg-cli/utils/__tests__/repo-detection.test.js +436 -0
  90. package/frigg-cli/utils/output.js +382 -0
  91. package/frigg-cli/utils/provider-helper.js +75 -0
  92. package/frigg-cli/utils/repo-detection.js +85 -37
  93. package/frigg-cli/validate-command/__tests__/adapters/validate-command.test.js +205 -0
  94. package/frigg-cli/validate-command/__tests__/application/validate-app-use-case.test.js +104 -0
  95. package/frigg-cli/validate-command/__tests__/domain/fix-suggestion.test.js +153 -0
  96. package/frigg-cli/validate-command/__tests__/domain/validation-error.test.js +162 -0
  97. package/frigg-cli/validate-command/__tests__/domain/validation-result.test.js +152 -0
  98. package/frigg-cli/validate-command/__tests__/infrastructure/api-module-validator.test.js +332 -0
  99. package/frigg-cli/validate-command/__tests__/infrastructure/app-definition-validator.test.js +191 -0
  100. package/frigg-cli/validate-command/__tests__/infrastructure/integration-class-validator.test.js +146 -0
  101. package/frigg-cli/validate-command/__tests__/infrastructure/template-validation.test.js +155 -0
  102. package/frigg-cli/validate-command/adapters/cli/validate-command.js +199 -0
  103. package/frigg-cli/validate-command/application/use-cases/validate-app-use-case.js +35 -0
  104. package/frigg-cli/validate-command/domain/entities/validation-result.js +74 -0
  105. package/frigg-cli/validate-command/domain/value-objects/fix-suggestion.js +74 -0
  106. package/frigg-cli/validate-command/domain/value-objects/validation-error.js +68 -0
  107. package/frigg-cli/validate-command/infrastructure/validators/api-module-validator.js +181 -0
  108. package/frigg-cli/validate-command/infrastructure/validators/app-definition-validator.js +145 -0
  109. package/frigg-cli/validate-command/infrastructure/validators/integration-class-validator.js +113 -0
  110. package/infrastructure/create-frigg-infrastructure.js +93 -0
  111. package/infrastructure/docs/iam-policy-templates.md +1 -1
  112. package/infrastructure/domains/admin-scripts/admin-script-builder.js +200 -0
  113. package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +499 -0
  114. package/infrastructure/domains/admin-scripts/index.js +5 -0
  115. package/infrastructure/domains/networking/vpc-builder.test.js +2 -4
  116. package/infrastructure/domains/networking/vpc-resolver.test.js +1 -1
  117. package/infrastructure/domains/shared/resource-discovery.js +5 -5
  118. package/infrastructure/domains/shared/types/app-definition.js +35 -0
  119. package/infrastructure/domains/shared/types/discovery-result.test.js +1 -1
  120. package/infrastructure/domains/shared/utilities/base-definition-factory.js +10 -1
  121. package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +2 -2
  122. package/infrastructure/infrastructure-composer.js +2 -0
  123. package/infrastructure/infrastructure-composer.test.js +2 -2
  124. package/infrastructure/jest.config.js +16 -0
  125. package/management-ui/README.md +245 -109
  126. package/package.json +8 -7
  127. package/frigg-cli/install-command/logger.js +0 -12
@@ -0,0 +1,249 @@
1
+ const path = require('path');
2
+
3
+ /**
4
+ * IntegrationJsUpdater
5
+ *
6
+ * Infrastructure adapter for updating Integration.js class files
7
+ * Adds API module imports and updates the static Definition.modules object
8
+ */
9
+ class IntegrationJsUpdater {
10
+ constructor(fileSystemAdapter, backendPath = process.cwd()) {
11
+ this.fileSystemAdapter = fileSystemAdapter;
12
+ this.backendPath = backendPath;
13
+ }
14
+
15
+ /**
16
+ * Add an API module to an Integration.js file
17
+ *
18
+ * Updates:
19
+ * 1. Adds require() import at top of file
20
+ * 2. Adds module to static Definition.modules object
21
+ *
22
+ * @param {string} integrationName - Integration name (kebab-case)
23
+ * @param {string} moduleName - API module name (kebab-case)
24
+ * @param {string} source - Module source ('local', 'npm', 'git')
25
+ */
26
+ async addModuleToIntegration(integrationName, moduleName, source = 'local') {
27
+ return this.addModulesToIntegration(integrationName, [{name: moduleName, source}]);
28
+ }
29
+
30
+ /**
31
+ * Add multiple API modules to an Integration.js file (batch operation)
32
+ *
33
+ * @param {string} integrationName - Integration name (kebab-case)
34
+ * @param {Array<{name: string, source: string}>} modules - Array of modules to add
35
+ */
36
+ async addModulesToIntegration(integrationName, modules = []) {
37
+ const className = this._toClassName(integrationName);
38
+ const integrationJsPath = path.join(
39
+ this.backendPath,
40
+ 'src/integrations',
41
+ `${className}Integration.js`
42
+ );
43
+
44
+ // Check if file exists
45
+ const exists = await this.fileSystemAdapter.exists(integrationJsPath);
46
+ if (!exists) {
47
+ throw new Error(`Integration.js not found at ${integrationJsPath}`);
48
+ }
49
+
50
+ // Write updated content using updateFile's callback pattern
51
+ await this.fileSystemAdapter.updateFile(integrationJsPath, (currentContent) => {
52
+ let content = currentContent;
53
+
54
+ // Add all imports
55
+ for (const module of modules) {
56
+ content = this._addModuleImport(content, module.name, module.source || 'local');
57
+ }
58
+
59
+ // Add all modules to Definition
60
+ for (const module of modules) {
61
+ content = this._addModuleToDefinition(content, module.name);
62
+ }
63
+
64
+ return content;
65
+ });
66
+ }
67
+
68
+ /**
69
+ * Add require() import for API module at top of file
70
+ */
71
+ _addModuleImport(content, moduleName, source = 'local') {
72
+ const camelName = this._toCamelCase(moduleName);
73
+ let importStatement;
74
+
75
+ // Different import path based on source
76
+ if (source === 'npm') {
77
+ importStatement = `const ${camelName} = require('@friggframework/api-module-${moduleName}');`;
78
+ } else {
79
+ // local or git - use relative path
80
+ importStatement = `const ${camelName} = require('../api-modules/${moduleName}');`;
81
+ }
82
+
83
+ // Check if import already exists
84
+ if (content.includes(importStatement)) {
85
+ return content;
86
+ }
87
+
88
+ // Find the position to insert (after other requires, before class definition)
89
+ const lines = content.split('\n');
90
+ let insertIndex = 0;
91
+
92
+ // Find last require statement
93
+ for (let i = 0; i < lines.length; i++) {
94
+ if (lines[i].includes('require(')) {
95
+ insertIndex = i + 1;
96
+ }
97
+ // Stop at class definition
98
+ if (lines[i].includes('class ') && lines[i].includes('Integration')) {
99
+ break;
100
+ }
101
+ }
102
+
103
+ // Insert import
104
+ lines.splice(insertIndex, 0, importStatement);
105
+
106
+ return lines.join('\n');
107
+ }
108
+
109
+ /**
110
+ * Add module to static Definition.modules object
111
+ */
112
+ _addModuleToDefinition(content, moduleName) {
113
+ const camelName = this._toCamelCase(moduleName);
114
+ const moduleEntry = ` ${camelName}: {\n definition: ${camelName}.Definition,\n },`;
115
+
116
+ // Check if module already exists in Definition
117
+ const modulePattern = new RegExp(`${camelName}:\\s*{[\\s\\S]*?definition:`);
118
+ if (modulePattern.test(content)) {
119
+ return content;
120
+ }
121
+
122
+ // Find the modules object in static Definition
123
+ const modulesPattern = /modules:\s*{/;
124
+ const match = content.match(modulesPattern);
125
+
126
+ if (!match) {
127
+ // No modules object exists yet, need to add it
128
+ return this._addModulesObjectToDefinition(content, moduleName);
129
+ }
130
+
131
+ // Parse line by line to find the right insertion point
132
+ const lines = content.split('\n');
133
+ let insertIndex = -1;
134
+ let modulesLineIndex = -1;
135
+ let braceCount = 0;
136
+ let inModules = false;
137
+
138
+ for (let i = 0; i < lines.length; i++) {
139
+ const line = lines[i];
140
+
141
+ if (line.includes('modules: {')) {
142
+ modulesLineIndex = i;
143
+ inModules = true;
144
+ braceCount = 1;
145
+ // Always insert right after modules: { line
146
+ insertIndex = i + 1;
147
+ break;
148
+ }
149
+ }
150
+
151
+ // Insert the module entry
152
+ lines.splice(insertIndex, 0, moduleEntry);
153
+
154
+ return lines.join('\n');
155
+ }
156
+
157
+ /**
158
+ * Add modules object to Definition if it doesn't exist
159
+ */
160
+ _addModulesObjectToDefinition(content, moduleName) {
161
+ const camelName = this._toCamelCase(moduleName);
162
+ const modulesBlock = ` modules: {\n ${camelName}: {\n definition: ${camelName}.Definition,\n },\n },`;
163
+
164
+ // Find static Definition
165
+ const definitionPattern = /static\s+Definition\s*=\s*{/;
166
+ const match = content.match(definitionPattern);
167
+
168
+ if (!match) {
169
+ throw new Error('Could not find static Definition in Integration.js');
170
+ }
171
+
172
+ // Find good insertion point (after display object)
173
+ const displayEndPattern = /},\s*$/m;
174
+ let lines = content.split('\n');
175
+ let insertIndex = -1;
176
+
177
+ let inDefinition = false;
178
+ let braceCount = 0;
179
+
180
+ for (let i = 0; i < lines.length; i++) {
181
+ const line = lines[i];
182
+
183
+ if (line.includes('static Definition')) {
184
+ inDefinition = true;
185
+ braceCount = 1;
186
+ continue;
187
+ }
188
+
189
+ if (inDefinition) {
190
+ // Count braces
191
+ braceCount += (line.match(/{/g) || []).length;
192
+ braceCount -= (line.match(/}/g) || []).length;
193
+
194
+ // Look for display block end
195
+ if (line.includes('display:')) {
196
+ // Find the closing of display object
197
+ for (let j = i + 1; j < lines.length; j++) {
198
+ if (lines[j].trim().startsWith('},')) {
199
+ insertIndex = j + 1;
200
+ break;
201
+ }
202
+ }
203
+ if (insertIndex !== -1) break;
204
+ }
205
+ }
206
+ }
207
+
208
+ if (insertIndex === -1) {
209
+ throw new Error('Could not find insertion point for modules in static Definition');
210
+ }
211
+
212
+ // Insert modules block
213
+ lines.splice(insertIndex, 0, modulesBlock);
214
+
215
+ return lines.join('\n');
216
+ }
217
+
218
+ /**
219
+ * Convert kebab-case to camelCase
220
+ */
221
+ _toCamelCase(str) {
222
+ return str.replace(/-([a-z])/g, (match, letter) => letter.toUpperCase());
223
+ }
224
+
225
+ /**
226
+ * Convert kebab-case to ClassName
227
+ */
228
+ _toClassName(str) {
229
+ return str
230
+ .split('-')
231
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
232
+ .join('');
233
+ }
234
+
235
+ /**
236
+ * Check if Integration.js file exists
237
+ */
238
+ async exists(integrationName) {
239
+ const className = this._toClassName(integrationName);
240
+ const integrationJsPath = path.join(
241
+ this.backendPath,
242
+ 'src/integrations',
243
+ `${className}Integration.js`
244
+ );
245
+ return await this.fileSystemAdapter.exists(integrationJsPath);
246
+ }
247
+ }
248
+
249
+ module.exports = {IntegrationJsUpdater};
@@ -0,0 +1,92 @@
1
+ const Ajv = require('ajv');
2
+ const addFormats = require('ajv-formats');
3
+ const path = require('path');
4
+ const fs = require('fs-extra');
5
+
6
+ /**
7
+ * SchemaValidator
8
+ * Validates data against JSON schemas from /packages/schemas
9
+ */
10
+ class SchemaValidator {
11
+ constructor(schemasPath) {
12
+ // Default to schemas package in monorepo
13
+ this.schemasPath = schemasPath || path.join(__dirname, '../../../../schemas/schemas');
14
+
15
+ this.ajv = new Ajv({
16
+ allErrors: true,
17
+ strict: false,
18
+ validateFormats: true
19
+ });
20
+
21
+ addFormats(this.ajv);
22
+ this.schemas = new Map();
23
+ }
24
+
25
+ /**
26
+ * Load and compile a schema
27
+ */
28
+ async loadSchema(schemaName) {
29
+ if (this.schemas.has(schemaName)) {
30
+ return this.schemas.get(schemaName);
31
+ }
32
+
33
+ const schemaPath = path.join(this.schemasPath, `${schemaName}.schema.json`);
34
+
35
+ if (!await fs.pathExists(schemaPath)) {
36
+ throw new Error(`Schema not found: ${schemaPath}`);
37
+ }
38
+
39
+ const schemaContent = await fs.readFile(schemaPath, 'utf-8');
40
+ const schema = JSON.parse(schemaContent);
41
+
42
+ const validate = this.ajv.compile(schema);
43
+ this.schemas.set(schemaName, validate);
44
+
45
+ return validate;
46
+ }
47
+
48
+ /**
49
+ * Validate data against a schema
50
+ * @param {string} schemaName - Name of the schema (e.g., 'integration-definition')
51
+ * @param {object} data - Data to validate
52
+ * @returns {Promise<{valid: boolean, errors: string[]}>}
53
+ */
54
+ async validate(schemaName, data) {
55
+ try {
56
+ const validate = await this.loadSchema(schemaName);
57
+ const valid = validate(data);
58
+
59
+ if (!valid) {
60
+ const errors = validate.errors.map(err => {
61
+ const path = err.instancePath || '/';
62
+ return `${path} ${err.message}`;
63
+ });
64
+
65
+ return {
66
+ valid: false,
67
+ errors
68
+ };
69
+ }
70
+
71
+ return {
72
+ valid: true,
73
+ errors: []
74
+ };
75
+ } catch (error) {
76
+ return {
77
+ valid: false,
78
+ errors: [`Schema validation error: ${error.message}`]
79
+ };
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Check if a schema exists
85
+ */
86
+ async hasSchema(schemaName) {
87
+ const schemaPath = path.join(this.schemasPath, `${schemaName}.schema.json`);
88
+ return await fs.pathExists(schemaPath);
89
+ }
90
+ }
91
+
92
+ module.exports = {SchemaValidator};
@@ -0,0 +1,373 @@
1
+ const path = require('path');
2
+ const {ApiModule} = require('../../domain/entities/ApiModule');
3
+ const {IApiModuleRepository} = require('../../domain/ports/IApiModuleRepository');
4
+
5
+ /**
6
+ * FileSystemApiModuleRepository
7
+ *
8
+ * Concrete implementation of IApiModuleRepository for file system storage
9
+ * Creates API module directories with class files, definitions, and configs
10
+ */
11
+ class FileSystemApiModuleRepository extends IApiModuleRepository {
12
+ constructor(fileSystemAdapter, projectRoot, schemaValidator) {
13
+ super();
14
+ this.fileSystemAdapter = fileSystemAdapter;
15
+ this.projectRoot = projectRoot;
16
+ this.schemaValidator = schemaValidator;
17
+ this.apiModulesDir = path.join(projectRoot, 'backend/src/api-modules');
18
+ }
19
+
20
+ /**
21
+ * Save API module to file system
22
+ */
23
+ async save(apiModule) {
24
+ // 1. Validate domain entity
25
+ const validation = apiModule.validate();
26
+ if (!validation.isValid) {
27
+ throw new Error(`ApiModule validation failed: ${validation.errors.join(', ')}`);
28
+ }
29
+
30
+ // 2. Convert to persistence format
31
+ const persistenceData = this._toPersistenceFormat(apiModule);
32
+
33
+ // 3. Validate against schema (if schema exists)
34
+ // TODO: Create api-module schema
35
+ // const schemaValidation = await this.schemaValidator.validate('api-module', persistenceData.definition);
36
+
37
+ // 4. Create directories
38
+ const modulePath = path.join(this.apiModulesDir, apiModule.name);
39
+ await this.fileSystemAdapter.ensureDirectory(modulePath);
40
+
41
+ // 5. Write files atomically
42
+ const files = [
43
+ {
44
+ path: path.join(modulePath, 'Api.js'),
45
+ content: persistenceData.classFile
46
+ },
47
+ {
48
+ path: path.join(modulePath, 'definition.js'),
49
+ content: persistenceData.definitionFile
50
+ },
51
+ {
52
+ path: path.join(modulePath, 'config.json'),
53
+ content: JSON.stringify(persistenceData.config, null, 2)
54
+ },
55
+ {
56
+ path: path.join(modulePath, 'README.md'),
57
+ content: persistenceData.readme
58
+ }
59
+ ];
60
+
61
+ // Create Entity.js if module has entities
62
+ if (Object.keys(apiModule.entities).length > 0) {
63
+ files.push({
64
+ path: path.join(modulePath, 'Entity.js'),
65
+ content: this._generateEntityClass(apiModule)
66
+ });
67
+ }
68
+
69
+ // Create tests directory
70
+ const testsDir = path.join(modulePath, 'tests');
71
+ await this.fileSystemAdapter.ensureDirectory(testsDir);
72
+
73
+ for (const file of files) {
74
+ await this.fileSystemAdapter.writeFile(file.path, file.content);
75
+ }
76
+
77
+ return apiModule;
78
+ }
79
+
80
+ /**
81
+ * Find API module by name
82
+ */
83
+ async findByName(name) {
84
+ const modulePath = path.join(this.apiModulesDir, name);
85
+
86
+ if (!await this.fileSystemAdapter.exists(modulePath)) {
87
+ return null;
88
+ }
89
+
90
+ const definitionPath = path.join(modulePath, 'definition.js');
91
+ if (!await this.fileSystemAdapter.exists(definitionPath)) {
92
+ return null;
93
+ }
94
+
95
+ // Read definition file (this is a simple implementation)
96
+ const content = await this.fileSystemAdapter.readFile(definitionPath);
97
+ // For now, return a basic ApiModule - full parsing would require more work
98
+ return ApiModule.create({name});
99
+ }
100
+
101
+ /**
102
+ * Check if API module exists
103
+ */
104
+ async exists(name) {
105
+ const modulePath = path.join(this.apiModulesDir, name);
106
+ return await this.fileSystemAdapter.exists(modulePath);
107
+ }
108
+
109
+ /**
110
+ * List all API modules
111
+ */
112
+ async list() {
113
+ if (!await this.fileSystemAdapter.exists(this.apiModulesDir)) {
114
+ return [];
115
+ }
116
+
117
+ const moduleDirs = await this.fileSystemAdapter.listDirectories(this.apiModulesDir);
118
+ const modules = [];
119
+
120
+ for (const dirName of moduleDirs) {
121
+ try {
122
+ const module = await this.findByName(dirName);
123
+ if (module) {
124
+ modules.push(module);
125
+ }
126
+ } catch (error) {
127
+ console.warn(`Failed to load API module ${dirName}:`, error.message);
128
+ }
129
+ }
130
+
131
+ return modules;
132
+ }
133
+
134
+ /**
135
+ * Delete API module
136
+ */
137
+ async delete(name) {
138
+ const modulePath = path.join(this.apiModulesDir, name);
139
+
140
+ if (!await this.fileSystemAdapter.exists(modulePath)) {
141
+ return false;
142
+ }
143
+
144
+ await this.fileSystemAdapter.deleteDirectory(modulePath);
145
+ return true;
146
+ }
147
+
148
+ /**
149
+ * Convert domain entity to persistence format
150
+ */
151
+ _toPersistenceFormat(apiModule) {
152
+ const obj = apiModule.toObject();
153
+ const json = apiModule.toJSON();
154
+
155
+ return {
156
+ classFile: this._generateApiClass(apiModule),
157
+ definitionFile: this._generateDefinitionFile(apiModule),
158
+ definition: json,
159
+ config: {
160
+ name: obj.name,
161
+ version: obj.version,
162
+ authType: obj.apiConfig.authType
163
+ },
164
+ readme: this._generateReadme(apiModule)
165
+ };
166
+ }
167
+
168
+ /**
169
+ * Generate Api.js class file
170
+ */
171
+ _generateApiClass(apiModule) {
172
+ const className = apiModule.name
173
+ .split('-')
174
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
175
+ .join('');
176
+
177
+ const obj = apiModule.toObject();
178
+
179
+ return `const { ApiBase } = require('@friggframework/core');
180
+
181
+ /**
182
+ * ${apiModule.displayName} API Client
183
+ * ${apiModule.description || 'No description provided'}
184
+ *
185
+ * Base URL: ${obj.apiConfig.baseUrl || 'Not configured'}
186
+ * Auth Type: ${obj.apiConfig.authType}
187
+ */
188
+ class ${className}Api extends ApiBase {
189
+ constructor(params) {
190
+ super(params);
191
+ this.baseUrl = '${obj.apiConfig.baseUrl || ''}';
192
+ this.authType = '${obj.apiConfig.authType}';
193
+ ${obj.entities.credential ? ` this.credential = params.credential;\n` : ''} }
194
+
195
+ static get Definition() {
196
+ return require('./definition');
197
+ }
198
+
199
+ /**
200
+ * Get authorization URL for OAuth2 flow
201
+ */
202
+ async getAuthorizationUri() {
203
+ // TODO: Implement OAuth authorization URL
204
+ return \`\${this.baseUrl}/oauth/authorize\`;
205
+ }
206
+
207
+ /**
208
+ * Exchange authorization code for access token
209
+ */
210
+ async getTokenFromCode(code) {
211
+ // TODO: Implement token exchange
212
+ return await this.api.post('/oauth/token', {
213
+ code,
214
+ grant_type: 'authorization_code'
215
+ });
216
+ }
217
+
218
+ /**
219
+ * Set API credentials
220
+ */
221
+ async setCredential(credential) {
222
+ this.credential = credential;
223
+
224
+ // Set auth headers based on auth type
225
+ if (this.authType === 'oauth2' && credential.accessToken) {
226
+ this.setHeader('Authorization', \`Bearer \${credential.accessToken}\`);
227
+ } else if (this.authType === 'api-key' && credential.apiKey) {
228
+ this.setHeader('X-API-Key', credential.apiKey);
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Test API connection
234
+ */
235
+ async testAuth() {
236
+ // TODO: Implement connection test
237
+ return await this.get('/user/me');
238
+ }
239
+
240
+ ${this._generateEndpointMethods(apiModule)}
241
+ // TODO: Add your API methods here
242
+ }
243
+
244
+ module.exports = ${className}Api;
245
+ `;
246
+ }
247
+
248
+ /**
249
+ * Generate endpoint methods
250
+ */
251
+ _generateEndpointMethods(apiModule) {
252
+ if (Object.keys(apiModule.endpoints).length === 0) {
253
+ return '';
254
+ }
255
+
256
+ return Object.entries(apiModule.endpoints).map(([name, config]) => {
257
+ const method = config.method.toLowerCase();
258
+ const params = config.parameters || [];
259
+ const paramList = params.map(p => p.name).join(', ');
260
+
261
+ return ` /**
262
+ * ${config.description || name}
263
+ */
264
+ async ${name}(${paramList}) {
265
+ return await this.${method}('${config.path}'${paramList ? `, {${paramList}}` : ''});
266
+ }
267
+ `;
268
+ }).join('\n');
269
+ }
270
+
271
+ /**
272
+ * Generate Entity.js class file
273
+ */
274
+ _generateEntityClass(apiModule) {
275
+ const className = apiModule.name
276
+ .split('-')
277
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
278
+ .join('');
279
+
280
+ const entities = Object.entries(apiModule.entities);
281
+ const primaryEntity = entities[0]; // Use first entity as primary
282
+
283
+ return `const { EntityBase } = require('@friggframework/core');
284
+
285
+ /**
286
+ * ${apiModule.displayName} Entity
287
+ * Database entity for storing ${apiModule.displayName} credentials and state
288
+ */
289
+ class ${className}Entity extends EntityBase {
290
+ static getName() {
291
+ return '${primaryEntity[0]}';
292
+ }
293
+
294
+ static get Definition() {
295
+ return {
296
+ type: '${primaryEntity[0]}',
297
+ fields: ${JSON.stringify(primaryEntity[1].fields || [], null, 12)}
298
+ };
299
+ }
300
+ }
301
+
302
+ module.exports = ${className}Entity;
303
+ `;
304
+ }
305
+
306
+ /**
307
+ * Generate definition.js file
308
+ */
309
+ _generateDefinitionFile(apiModule) {
310
+ const json = apiModule.toJSON();
311
+
312
+ return `module.exports = ${JSON.stringify(json, null, 2)};
313
+ `;
314
+ }
315
+
316
+ /**
317
+ * Generate README.md
318
+ */
319
+ _generateReadme(apiModule) {
320
+ const obj = apiModule.toObject();
321
+
322
+ return `# ${apiModule.displayName}
323
+
324
+ ${apiModule.description || 'No description provided'}
325
+
326
+ ## Configuration
327
+
328
+ **Base URL:** ${obj.apiConfig.baseUrl || 'Not configured'}
329
+ **Auth Type:** ${obj.apiConfig.authType}
330
+ **API Version:** ${obj.apiConfig.version || 'v1'}
331
+
332
+ ## Required Credentials
333
+
334
+ ${obj.credentials.length > 0 ? obj.credentials.map(c =>
335
+ `- **${c.name}** (\`${c.type}\`): ${c.description || 'No description'}${c.required ? ' (Required)' : ''}`
336
+ ).join('\n') : 'No credentials required'}
337
+
338
+ ## OAuth Scopes
339
+
340
+ ${obj.scopes.length > 0 ? obj.scopes.map(s => `- ${s}`).join('\n') : 'No scopes required'}
341
+
342
+ ## Entities
343
+
344
+ ${Object.keys(obj.entities).length > 0 ? Object.entries(obj.entities).map(([name, config]) =>
345
+ `### ${config.label || name}
346
+
347
+ - Type: \`${config.type}\`
348
+ - Required: ${config.required ? 'Yes' : 'No'}
349
+ `).join('\n') : 'No entities defined'}
350
+
351
+ ## Usage
352
+
353
+ \`\`\`javascript
354
+ const ${apiModule.name.split('-').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join('')}Api = require('./${apiModule.name}/Api');
355
+
356
+ const api = new ${apiModule.name.split('-').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join('')}Api({
357
+ credential: myCredential
358
+ });
359
+
360
+ // Test authentication
361
+ await api.testAuth();
362
+ \`\`\`
363
+
364
+ ## Development
365
+
366
+ 1. Implement the API methods in \`Api.js\`
367
+ 2. Add entity configuration in \`Entity.js\` if needed
368
+ 3. Test with \`frigg start\`
369
+ `;
370
+ }
371
+ }
372
+
373
+ module.exports = {FileSystemApiModuleRepository};