appwrite-utils-cli 1.6.3 → 1.6.5
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.
- package/CONFIG_TODO.md +1189 -0
- package/SERVICE_IMPLEMENTATION_REPORT.md +462 -0
- package/dist/cli/commands/configCommands.js +7 -1
- package/dist/collections/attributes.js +102 -30
- package/dist/collections/indexes.js +6 -1
- package/dist/collections/methods.js +4 -5
- package/dist/config/ConfigManager.d.ts +445 -0
- package/dist/config/ConfigManager.js +625 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +7 -0
- package/dist/config/services/ConfigDiscoveryService.d.ts +126 -0
- package/dist/config/services/ConfigDiscoveryService.js +374 -0
- package/dist/config/services/ConfigLoaderService.d.ts +105 -0
- package/dist/config/services/ConfigLoaderService.js +410 -0
- package/dist/config/services/ConfigMergeService.d.ts +208 -0
- package/dist/config/services/ConfigMergeService.js +307 -0
- package/dist/config/services/ConfigValidationService.d.ts +214 -0
- package/dist/config/services/ConfigValidationService.js +310 -0
- package/dist/config/services/SessionAuthService.d.ts +225 -0
- package/dist/config/services/SessionAuthService.js +456 -0
- package/dist/config/services/__tests__/ConfigMergeService.test.d.ts +1 -0
- package/dist/config/services/__tests__/ConfigMergeService.test.js +271 -0
- package/dist/config/services/index.d.ts +13 -0
- package/dist/config/services/index.js +10 -0
- package/dist/interactiveCLI.js +8 -6
- package/dist/main.js +14 -19
- package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +1 -1
- package/dist/shared/operationQueue.js +1 -1
- package/dist/utils/ClientFactory.d.ts +87 -0
- package/dist/utils/ClientFactory.js +164 -0
- package/dist/utils/getClientFromConfig.js +4 -3
- package/dist/utils/helperFunctions.d.ts +1 -0
- package/dist/utils/helperFunctions.js +21 -5
- package/dist/utils/yamlConverter.d.ts +2 -2
- package/dist/utils/yamlConverter.js +2 -2
- package/dist/utilsController.d.ts +18 -15
- package/dist/utilsController.js +83 -131
- package/package.json +1 -1
- package/src/cli/commands/configCommands.ts +8 -1
- package/src/collections/attributes.ts +118 -31
- package/src/collections/indexes.ts +7 -1
- package/src/collections/methods.ts +4 -6
- package/src/config/ConfigManager.ts +808 -0
- package/src/config/index.ts +10 -0
- package/src/config/services/ConfigDiscoveryService.ts +463 -0
- package/src/config/services/ConfigLoaderService.ts +560 -0
- package/src/config/services/ConfigMergeService.ts +386 -0
- package/src/config/services/ConfigValidationService.ts +394 -0
- package/src/config/services/SessionAuthService.ts +565 -0
- package/src/config/services/__tests__/ConfigMergeService.test.ts +351 -0
- package/src/config/services/index.ts +29 -0
- package/src/interactiveCLI.ts +9 -7
- package/src/main.ts +14 -24
- package/src/shared/operationQueue.ts +1 -1
- package/src/utils/ClientFactory.ts +186 -0
- package/src/utils/getClientFromConfig.ts +4 -3
- package/src/utils/helperFunctions.ts +27 -7
- package/src/utils/yamlConverter.ts +4 -4
- package/src/utilsController.ts +99 -187
@@ -0,0 +1,560 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import yaml from "js-yaml";
|
4
|
+
import { register } from "tsx/esm/api";
|
5
|
+
import { pathToFileURL } from "node:url";
|
6
|
+
import type { AppwriteConfig, Collection, CollectionCreate } from "appwrite-utils";
|
7
|
+
import { MessageFormatter } from "../../shared/messageFormatter.js";
|
8
|
+
import { normalizeYamlData } from "../../utils/yamlConverter.js";
|
9
|
+
import { loadYamlConfig, type YamlSessionOptions } from "../yamlConfig.js";
|
10
|
+
import {
|
11
|
+
loadAppwriteProjectConfig,
|
12
|
+
projectConfigToAppwriteConfig,
|
13
|
+
getCollectionsFromProject,
|
14
|
+
type AppwriteProjectConfig
|
15
|
+
} from "../../utils/projectConfig.js";
|
16
|
+
import {
|
17
|
+
loadYamlCollection,
|
18
|
+
loadYamlTable,
|
19
|
+
type CollectionDiscoveryResult,
|
20
|
+
type TableDiscoveryResult,
|
21
|
+
} from "../../utils/configDiscovery.js";
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Options for loading collections or tables
|
25
|
+
*/
|
26
|
+
export interface CollectionLoadOptions {
|
27
|
+
/**
|
28
|
+
* Whether to mark loaded items as coming from tables directory
|
29
|
+
*/
|
30
|
+
markAsTablesDir?: boolean;
|
31
|
+
/**
|
32
|
+
* Set of existing collection names to check for conflicts
|
33
|
+
*/
|
34
|
+
existingNames?: Set<string>;
|
35
|
+
}
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Service for loading and parsing Appwrite configuration files.
|
39
|
+
*
|
40
|
+
* Supports:
|
41
|
+
* - YAML configuration files (.appwrite/config.yaml)
|
42
|
+
* - TypeScript configuration files (appwriteConfig.ts)
|
43
|
+
* - JSON project configuration files (appwrite.json)
|
44
|
+
* - Collection and table YAML/TypeScript definitions
|
45
|
+
*
|
46
|
+
* Features:
|
47
|
+
* - Auto-detects configuration file type
|
48
|
+
* - Converts between different configuration formats
|
49
|
+
* - Loads collections and tables from directories
|
50
|
+
* - Handles session preservation
|
51
|
+
* - Validates and normalizes configuration data
|
52
|
+
*/
|
53
|
+
export class ConfigLoaderService {
|
54
|
+
/**
|
55
|
+
* Loads configuration from a discovered path, auto-detecting the type
|
56
|
+
* @param configPath Path to the configuration file
|
57
|
+
* @param sessionOptions Optional session preservation options
|
58
|
+
* @returns Parsed AppwriteConfig
|
59
|
+
*/
|
60
|
+
public async loadFromPath(
|
61
|
+
configPath: string,
|
62
|
+
sessionOptions?: YamlSessionOptions
|
63
|
+
): Promise<AppwriteConfig> {
|
64
|
+
const ext = path.extname(configPath).toLowerCase();
|
65
|
+
|
66
|
+
// Determine file type and load accordingly
|
67
|
+
if (ext === ".yaml" || ext === ".yml") {
|
68
|
+
return this.loadYaml(configPath, sessionOptions);
|
69
|
+
} else if (ext === ".ts") {
|
70
|
+
return this.loadTypeScript(configPath);
|
71
|
+
} else if (ext === ".json") {
|
72
|
+
const partialConfig = await this.loadProjectConfig(configPath);
|
73
|
+
|
74
|
+
// Convert partial config to full AppwriteConfig with sensible defaults
|
75
|
+
if (!partialConfig.appwriteEndpoint || !partialConfig.appwriteProject) {
|
76
|
+
throw new Error(
|
77
|
+
"JSON project config must contain at minimum 'endpoint' and 'projectId' fields"
|
78
|
+
);
|
79
|
+
}
|
80
|
+
|
81
|
+
return {
|
82
|
+
appwriteEndpoint: partialConfig.appwriteEndpoint,
|
83
|
+
appwriteProject: partialConfig.appwriteProject,
|
84
|
+
appwriteKey: partialConfig.appwriteKey || "",
|
85
|
+
apiMode: partialConfig.apiMode || "auto",
|
86
|
+
appwriteClient: null,
|
87
|
+
logging: partialConfig.logging || {
|
88
|
+
enabled: false,
|
89
|
+
level: "info",
|
90
|
+
console: false,
|
91
|
+
},
|
92
|
+
enableBackups: partialConfig.enableBackups ?? true,
|
93
|
+
backupInterval: partialConfig.backupInterval || 3600,
|
94
|
+
backupRetention: partialConfig.backupRetention || 30,
|
95
|
+
enableBackupCleanup: partialConfig.enableBackupCleanup ?? true,
|
96
|
+
enableMockData: partialConfig.enableMockData || false,
|
97
|
+
documentBucketId: partialConfig.documentBucketId || "documents",
|
98
|
+
usersCollectionName: partialConfig.usersCollectionName || "Members",
|
99
|
+
schemaConfig: partialConfig.schemaConfig || {
|
100
|
+
outputDirectory: "schemas",
|
101
|
+
yamlSchemaDirectory: ".yaml_schemas",
|
102
|
+
importDirectory: "importData",
|
103
|
+
collectionsDirectory: "collections",
|
104
|
+
tablesDirectory: "tables",
|
105
|
+
},
|
106
|
+
databases: partialConfig.databases || [],
|
107
|
+
buckets: partialConfig.buckets || [],
|
108
|
+
functions: partialConfig.functions || [],
|
109
|
+
collections: partialConfig.collections || [],
|
110
|
+
sessionCookie: partialConfig.sessionCookie,
|
111
|
+
authMethod: partialConfig.authMethod || "auto",
|
112
|
+
sessionMetadata: partialConfig.sessionMetadata,
|
113
|
+
};
|
114
|
+
}
|
115
|
+
|
116
|
+
throw new Error(`Unsupported configuration file type: ${ext}`);
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Loads a YAML configuration file
|
121
|
+
* @param yamlPath Path to the YAML config file
|
122
|
+
* @param sessionOptions Optional session preservation options
|
123
|
+
* @returns Parsed AppwriteConfig
|
124
|
+
*/
|
125
|
+
public async loadYaml(
|
126
|
+
yamlPath: string,
|
127
|
+
sessionOptions?: YamlSessionOptions
|
128
|
+
): Promise<AppwriteConfig> {
|
129
|
+
if (!fs.existsSync(yamlPath)) {
|
130
|
+
throw new Error(`YAML config file not found: ${yamlPath}`);
|
131
|
+
}
|
132
|
+
|
133
|
+
try {
|
134
|
+
const config = await loadYamlConfig(yamlPath);
|
135
|
+
|
136
|
+
if (!config) {
|
137
|
+
throw new Error(`Failed to load YAML config from: ${yamlPath}`);
|
138
|
+
}
|
139
|
+
|
140
|
+
// Apply session preservation if provided
|
141
|
+
if (sessionOptions) {
|
142
|
+
if (sessionOptions.sessionCookie) {
|
143
|
+
config.sessionCookie = sessionOptions.sessionCookie;
|
144
|
+
}
|
145
|
+
if (sessionOptions.authMethod) {
|
146
|
+
config.authMethod = sessionOptions.authMethod;
|
147
|
+
}
|
148
|
+
if (sessionOptions.sessionMetadata) {
|
149
|
+
config.sessionMetadata = {
|
150
|
+
...config.sessionMetadata,
|
151
|
+
...sessionOptions.sessionMetadata,
|
152
|
+
};
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
MessageFormatter.success(`Loaded YAML config from: ${yamlPath}`, {
|
157
|
+
prefix: "Config",
|
158
|
+
});
|
159
|
+
|
160
|
+
return config;
|
161
|
+
} catch (error) {
|
162
|
+
MessageFormatter.error(
|
163
|
+
`Error loading YAML config from ${yamlPath}`,
|
164
|
+
error instanceof Error ? error : undefined,
|
165
|
+
{ prefix: "Config" }
|
166
|
+
);
|
167
|
+
throw error;
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
/**
|
172
|
+
* Loads a TypeScript configuration file
|
173
|
+
* @param tsPath Path to the TypeScript config file
|
174
|
+
* @returns Parsed AppwriteConfig
|
175
|
+
*/
|
176
|
+
public async loadTypeScript(tsPath: string): Promise<AppwriteConfig> {
|
177
|
+
if (!fs.existsSync(tsPath)) {
|
178
|
+
throw new Error(`TypeScript config file not found: ${tsPath}`);
|
179
|
+
}
|
180
|
+
|
181
|
+
const unregister = register(); // Register tsx enhancement
|
182
|
+
|
183
|
+
try {
|
184
|
+
const configUrl = pathToFileURL(tsPath).href;
|
185
|
+
const configModule = await import(configUrl);
|
186
|
+
const config: AppwriteConfig =
|
187
|
+
configModule.default?.default || configModule.default || configModule;
|
188
|
+
|
189
|
+
if (!config) {
|
190
|
+
throw new Error(`Failed to load TypeScript config from: ${tsPath}`);
|
191
|
+
}
|
192
|
+
|
193
|
+
MessageFormatter.success(`Loaded TypeScript config from: ${tsPath}`, {
|
194
|
+
prefix: "Config",
|
195
|
+
});
|
196
|
+
|
197
|
+
return config;
|
198
|
+
} catch (error) {
|
199
|
+
MessageFormatter.error(
|
200
|
+
`Error loading TypeScript config from ${tsPath}`,
|
201
|
+
error instanceof Error ? error : undefined,
|
202
|
+
{ prefix: "Config" }
|
203
|
+
);
|
204
|
+
throw error;
|
205
|
+
} finally {
|
206
|
+
unregister(); // Unregister tsx when done
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
/**
|
211
|
+
* Loads an appwrite.json project configuration file
|
212
|
+
* Converts projectId → appwriteProject and detects API mode
|
213
|
+
* @param jsonPath Path to the JSON config file
|
214
|
+
* @returns Partial AppwriteConfig (requires merging with defaults)
|
215
|
+
*/
|
216
|
+
public async loadProjectConfig(jsonPath: string): Promise<Partial<AppwriteConfig>> {
|
217
|
+
if (!fs.existsSync(jsonPath)) {
|
218
|
+
throw new Error(`JSON config file not found: ${jsonPath}`);
|
219
|
+
}
|
220
|
+
|
221
|
+
try {
|
222
|
+
const projectConfig = loadAppwriteProjectConfig(jsonPath);
|
223
|
+
|
224
|
+
if (!projectConfig) {
|
225
|
+
throw new Error(`Failed to load project config from: ${jsonPath}`);
|
226
|
+
}
|
227
|
+
|
228
|
+
// Convert project config to AppwriteConfig format
|
229
|
+
const appwriteConfig = projectConfigToAppwriteConfig(projectConfig);
|
230
|
+
|
231
|
+
// Get collections from project config
|
232
|
+
const collections = getCollectionsFromProject(projectConfig);
|
233
|
+
if (collections.length > 0) {
|
234
|
+
appwriteConfig.collections = collections;
|
235
|
+
}
|
236
|
+
|
237
|
+
MessageFormatter.success(`Loaded project config from: ${jsonPath}`, {
|
238
|
+
prefix: "Config",
|
239
|
+
});
|
240
|
+
|
241
|
+
return appwriteConfig;
|
242
|
+
} catch (error) {
|
243
|
+
MessageFormatter.error(
|
244
|
+
`Error loading project config from ${jsonPath}`,
|
245
|
+
error instanceof Error ? error : undefined,
|
246
|
+
{ prefix: "Config" }
|
247
|
+
);
|
248
|
+
throw error;
|
249
|
+
}
|
250
|
+
}
|
251
|
+
|
252
|
+
/**
|
253
|
+
* Loads all collections from a collections/ directory
|
254
|
+
* Supports both YAML (.yaml, .yml) and TypeScript (.ts) files
|
255
|
+
* @param collectionsDir Path to the collections directory
|
256
|
+
* @param options Loading options
|
257
|
+
* @returns Array of loaded Collection objects
|
258
|
+
*/
|
259
|
+
public async loadCollections(
|
260
|
+
collectionsDir: string,
|
261
|
+
options: CollectionLoadOptions = {}
|
262
|
+
): Promise<Collection[]> {
|
263
|
+
if (!fs.existsSync(collectionsDir)) {
|
264
|
+
MessageFormatter.debug(
|
265
|
+
`Collections directory not found: ${collectionsDir}`,
|
266
|
+
{ prefix: "Config" }
|
267
|
+
);
|
268
|
+
return [];
|
269
|
+
}
|
270
|
+
|
271
|
+
const collections: Collection[] = [];
|
272
|
+
const unregister = register(); // Register tsx for TypeScript collections
|
273
|
+
|
274
|
+
try {
|
275
|
+
const collectionFiles = fs.readdirSync(collectionsDir);
|
276
|
+
MessageFormatter.success(
|
277
|
+
`Loading from collections directory: ${collectionFiles.length} files found`,
|
278
|
+
{ prefix: "Config" }
|
279
|
+
);
|
280
|
+
|
281
|
+
for (const file of collectionFiles) {
|
282
|
+
if (file === "index.ts") {
|
283
|
+
continue;
|
284
|
+
}
|
285
|
+
|
286
|
+
const filePath = path.join(collectionsDir, file);
|
287
|
+
let collection: CollectionCreate | null = null;
|
288
|
+
|
289
|
+
// Handle YAML collections
|
290
|
+
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
|
291
|
+
collection = loadYamlCollection(filePath);
|
292
|
+
}
|
293
|
+
|
294
|
+
// Handle TypeScript collections
|
295
|
+
else if (file.endsWith(".ts")) {
|
296
|
+
try {
|
297
|
+
const fileUrl = pathToFileURL(filePath).href;
|
298
|
+
const collectionModule = await import(fileUrl);
|
299
|
+
const importedCollection: Collection | undefined =
|
300
|
+
collectionModule.default?.default ||
|
301
|
+
collectionModule.default ||
|
302
|
+
collectionModule;
|
303
|
+
|
304
|
+
if (importedCollection) {
|
305
|
+
collection = importedCollection as CollectionCreate;
|
306
|
+
// Ensure importDefs are properly loaded
|
307
|
+
if (collectionModule.importDefs || collection.importDefs) {
|
308
|
+
collection.importDefs =
|
309
|
+
collectionModule.importDefs || collection.importDefs;
|
310
|
+
}
|
311
|
+
}
|
312
|
+
} catch (error) {
|
313
|
+
MessageFormatter.error(
|
314
|
+
`Error loading TypeScript collection: ${file}`,
|
315
|
+
error instanceof Error ? error : undefined,
|
316
|
+
{ prefix: "Config" }
|
317
|
+
);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
|
321
|
+
if (collection) {
|
322
|
+
const collectionName = collection.name || collection.$id || file;
|
323
|
+
|
324
|
+
// Check for naming conflicts if existingNames provided
|
325
|
+
if (options.existingNames?.has(collectionName)) {
|
326
|
+
MessageFormatter.warning(
|
327
|
+
`Skipping duplicate collection '${collectionName}' (already loaded)`,
|
328
|
+
{ prefix: "Config" }
|
329
|
+
);
|
330
|
+
continue;
|
331
|
+
}
|
332
|
+
|
333
|
+
// Mark as tables if requested
|
334
|
+
if (options.markAsTablesDir) {
|
335
|
+
(collection as any)._isFromTablesDir = true;
|
336
|
+
}
|
337
|
+
|
338
|
+
collections.push(collection as Collection);
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
MessageFormatter.success(
|
343
|
+
`Loaded ${collections.length} collection(s) from ${collectionsDir}`,
|
344
|
+
{ prefix: "Config" }
|
345
|
+
);
|
346
|
+
} catch (error) {
|
347
|
+
MessageFormatter.error(
|
348
|
+
`Error loading collections from ${collectionsDir}`,
|
349
|
+
error instanceof Error ? error : undefined,
|
350
|
+
{ prefix: "Config" }
|
351
|
+
);
|
352
|
+
} finally {
|
353
|
+
unregister(); // Unregister tsx when done
|
354
|
+
}
|
355
|
+
|
356
|
+
return collections;
|
357
|
+
}
|
358
|
+
|
359
|
+
/**
|
360
|
+
* Loads all tables from a tables/ directory
|
361
|
+
* Supports both YAML (.yaml, .yml) and TypeScript (.ts) files
|
362
|
+
* @param tablesDir Path to the tables directory
|
363
|
+
* @param options Loading options
|
364
|
+
* @returns Array of loaded table objects
|
365
|
+
*/
|
366
|
+
public async loadTables(
|
367
|
+
tablesDir: string,
|
368
|
+
options: CollectionLoadOptions = {}
|
369
|
+
): Promise<any[]> {
|
370
|
+
if (!fs.existsSync(tablesDir)) {
|
371
|
+
MessageFormatter.debug(`Tables directory not found: ${tablesDir}`, {
|
372
|
+
prefix: "Config",
|
373
|
+
});
|
374
|
+
return [];
|
375
|
+
}
|
376
|
+
|
377
|
+
const tables: any[] = [];
|
378
|
+
const unregister = register(); // Register tsx for TypeScript tables
|
379
|
+
|
380
|
+
try {
|
381
|
+
const tableFiles = fs.readdirSync(tablesDir);
|
382
|
+
MessageFormatter.success(
|
383
|
+
`Loading from tables directory: ${tableFiles.length} files found`,
|
384
|
+
{ prefix: "Config" }
|
385
|
+
);
|
386
|
+
|
387
|
+
for (const file of tableFiles) {
|
388
|
+
if (file === "index.ts") {
|
389
|
+
continue;
|
390
|
+
}
|
391
|
+
|
392
|
+
const filePath = path.join(tablesDir, file);
|
393
|
+
let table: any | null = null;
|
394
|
+
|
395
|
+
// Handle YAML tables
|
396
|
+
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
|
397
|
+
table = loadYamlTable(filePath);
|
398
|
+
}
|
399
|
+
|
400
|
+
// Handle TypeScript tables
|
401
|
+
else if (file.endsWith(".ts")) {
|
402
|
+
try {
|
403
|
+
const fileUrl = pathToFileURL(filePath).href;
|
404
|
+
const tableModule = await import(fileUrl);
|
405
|
+
const importedTable: any =
|
406
|
+
tableModule.default?.default || tableModule.default || tableModule;
|
407
|
+
|
408
|
+
if (importedTable) {
|
409
|
+
table = importedTable;
|
410
|
+
// Ensure importDefs are properly loaded
|
411
|
+
if (tableModule.importDefs || table.importDefs) {
|
412
|
+
table.importDefs = tableModule.importDefs || table.importDefs;
|
413
|
+
}
|
414
|
+
}
|
415
|
+
} catch (error) {
|
416
|
+
MessageFormatter.error(
|
417
|
+
`Error loading TypeScript table: ${file}`,
|
418
|
+
error instanceof Error ? error : undefined,
|
419
|
+
{ prefix: "Config" }
|
420
|
+
);
|
421
|
+
}
|
422
|
+
}
|
423
|
+
|
424
|
+
if (table) {
|
425
|
+
const tableName = table.name || table.tableId || table.$id || file;
|
426
|
+
|
427
|
+
// Check for naming conflicts if existingNames provided
|
428
|
+
if (options.existingNames?.has(tableName)) {
|
429
|
+
MessageFormatter.warning(
|
430
|
+
`Skipping duplicate table '${tableName}' (already loaded from collections/)`,
|
431
|
+
{ prefix: "Config" }
|
432
|
+
);
|
433
|
+
continue;
|
434
|
+
}
|
435
|
+
|
436
|
+
// Always mark tables as coming from tables directory
|
437
|
+
table._isFromTablesDir = true;
|
438
|
+
|
439
|
+
tables.push(table);
|
440
|
+
}
|
441
|
+
}
|
442
|
+
|
443
|
+
MessageFormatter.success(
|
444
|
+
`Loaded ${tables.length} table(s) from ${tablesDir}`,
|
445
|
+
{ prefix: "Config" }
|
446
|
+
);
|
447
|
+
} catch (error) {
|
448
|
+
MessageFormatter.error(
|
449
|
+
`Error loading tables from ${tablesDir}`,
|
450
|
+
error instanceof Error ? error : undefined,
|
451
|
+
{ prefix: "Config" }
|
452
|
+
);
|
453
|
+
} finally {
|
454
|
+
unregister(); // Unregister tsx when done
|
455
|
+
}
|
456
|
+
|
457
|
+
return tables;
|
458
|
+
}
|
459
|
+
|
460
|
+
/**
|
461
|
+
* Loads collections and tables with conflict detection
|
462
|
+
* Collections take priority over tables when names conflict
|
463
|
+
* @param collectionsDir Path to collections directory
|
464
|
+
* @param tablesDir Path to tables directory
|
465
|
+
* @returns Object containing combined arrays and conflict information
|
466
|
+
*/
|
467
|
+
public async loadCollectionsAndTables(
|
468
|
+
collectionsDir: string,
|
469
|
+
tablesDir: string
|
470
|
+
): Promise<{
|
471
|
+
items: Collection[];
|
472
|
+
fromCollections: number;
|
473
|
+
fromTables: number;
|
474
|
+
conflicts: Array<{ name: string; source1: string; source2: string }>;
|
475
|
+
}> {
|
476
|
+
const items: Collection[] = [];
|
477
|
+
const loadedNames = new Set<string>();
|
478
|
+
const conflicts: Array<{ name: string; source1: string; source2: string }> = [];
|
479
|
+
|
480
|
+
// Load from collections/ directory first (higher priority)
|
481
|
+
if (fs.existsSync(collectionsDir)) {
|
482
|
+
const collections = await this.loadCollections(collectionsDir);
|
483
|
+
for (const collection of collections) {
|
484
|
+
const name = collection.name || collection.$id || "";
|
485
|
+
loadedNames.add(name);
|
486
|
+
items.push(collection);
|
487
|
+
}
|
488
|
+
}
|
489
|
+
|
490
|
+
// Load from tables/ directory second (lower priority, check for conflicts)
|
491
|
+
if (fs.existsSync(tablesDir)) {
|
492
|
+
const tables = await this.loadTables(tablesDir, {
|
493
|
+
existingNames: loadedNames,
|
494
|
+
markAsTablesDir: true,
|
495
|
+
});
|
496
|
+
|
497
|
+
for (const table of tables) {
|
498
|
+
const name = table.name || table.tableId || table.$id || "";
|
499
|
+
|
500
|
+
// Check for conflicts
|
501
|
+
if (loadedNames.has(name)) {
|
502
|
+
conflicts.push({
|
503
|
+
name,
|
504
|
+
source1: "collections/",
|
505
|
+
source2: "tables/",
|
506
|
+
});
|
507
|
+
} else {
|
508
|
+
loadedNames.add(name);
|
509
|
+
items.push(table);
|
510
|
+
}
|
511
|
+
}
|
512
|
+
}
|
513
|
+
|
514
|
+
const fromCollections = items.filter((item: any) => !item._isFromTablesDir)
|
515
|
+
.length;
|
516
|
+
const fromTables = items.filter((item: any) => item._isFromTablesDir).length;
|
517
|
+
|
518
|
+
return {
|
519
|
+
items,
|
520
|
+
fromCollections,
|
521
|
+
fromTables,
|
522
|
+
conflicts,
|
523
|
+
};
|
524
|
+
}
|
525
|
+
|
526
|
+
/**
|
527
|
+
* Validates that a configuration file can be loaded
|
528
|
+
* @param configPath Path to the configuration file
|
529
|
+
* @returns True if the file can be loaded, false otherwise
|
530
|
+
*/
|
531
|
+
public canLoadConfig(configPath: string): boolean {
|
532
|
+
if (!fs.existsSync(configPath)) {
|
533
|
+
return false;
|
534
|
+
}
|
535
|
+
|
536
|
+
const ext = path.extname(configPath).toLowerCase();
|
537
|
+
return ext === ".yaml" || ext === ".yml" || ext === ".ts" || ext === ".json";
|
538
|
+
}
|
539
|
+
|
540
|
+
/**
|
541
|
+
* Gets the type of a configuration file
|
542
|
+
* @param configPath Path to the configuration file
|
543
|
+
* @returns Configuration type or null if unknown
|
544
|
+
*/
|
545
|
+
public getConfigType(
|
546
|
+
configPath: string
|
547
|
+
): "yaml" | "typescript" | "json" | null {
|
548
|
+
const ext = path.extname(configPath).toLowerCase();
|
549
|
+
|
550
|
+
if (ext === ".yaml" || ext === ".yml") {
|
551
|
+
return "yaml";
|
552
|
+
} else if (ext === ".ts") {
|
553
|
+
return "typescript";
|
554
|
+
} else if (ext === ".json") {
|
555
|
+
return "json";
|
556
|
+
}
|
557
|
+
|
558
|
+
return null;
|
559
|
+
}
|
560
|
+
}
|