appwrite-utils-cli 1.6.2 → 1.6.4
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/cli/commands/databaseCommands.js +23 -15
- package/dist/collections/attributes.d.ts +1 -1
- package/dist/collections/attributes.js +163 -66
- package/dist/collections/indexes.js +3 -17
- package/dist/collections/methods.js +38 -0
- 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 +2 -2
- 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 -0
- package/dist/utils/yamlConverter.js +21 -4
- 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/cli/commands/databaseCommands.ts +34 -20
- package/src/collections/attributes.ts +195 -150
- package/src/collections/indexes.ts +4 -19
- package/src/collections/methods.ts +46 -0
- 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 +2 -2
- 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 +28 -2
- package/src/utilsController.ts +99 -187
@@ -0,0 +1,410 @@
|
|
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 { MessageFormatter } from "../../shared/messageFormatter.js";
|
7
|
+
import { normalizeYamlData } from "../../utils/yamlConverter.js";
|
8
|
+
import { loadYamlConfig } from "../yamlConfig.js";
|
9
|
+
import { loadAppwriteProjectConfig, projectConfigToAppwriteConfig, getCollectionsFromProject } from "../../utils/projectConfig.js";
|
10
|
+
import { loadYamlCollection, loadYamlTable, } from "../../utils/configDiscovery.js";
|
11
|
+
/**
|
12
|
+
* Service for loading and parsing Appwrite configuration files.
|
13
|
+
*
|
14
|
+
* Supports:
|
15
|
+
* - YAML configuration files (.appwrite/config.yaml)
|
16
|
+
* - TypeScript configuration files (appwriteConfig.ts)
|
17
|
+
* - JSON project configuration files (appwrite.json)
|
18
|
+
* - Collection and table YAML/TypeScript definitions
|
19
|
+
*
|
20
|
+
* Features:
|
21
|
+
* - Auto-detects configuration file type
|
22
|
+
* - Converts between different configuration formats
|
23
|
+
* - Loads collections and tables from directories
|
24
|
+
* - Handles session preservation
|
25
|
+
* - Validates and normalizes configuration data
|
26
|
+
*/
|
27
|
+
export class ConfigLoaderService {
|
28
|
+
/**
|
29
|
+
* Loads configuration from a discovered path, auto-detecting the type
|
30
|
+
* @param configPath Path to the configuration file
|
31
|
+
* @param sessionOptions Optional session preservation options
|
32
|
+
* @returns Parsed AppwriteConfig
|
33
|
+
*/
|
34
|
+
async loadFromPath(configPath, sessionOptions) {
|
35
|
+
const ext = path.extname(configPath).toLowerCase();
|
36
|
+
// Determine file type and load accordingly
|
37
|
+
if (ext === ".yaml" || ext === ".yml") {
|
38
|
+
return this.loadYaml(configPath, sessionOptions);
|
39
|
+
}
|
40
|
+
else if (ext === ".ts") {
|
41
|
+
return this.loadTypeScript(configPath);
|
42
|
+
}
|
43
|
+
else if (ext === ".json") {
|
44
|
+
const partialConfig = await this.loadProjectConfig(configPath);
|
45
|
+
// Convert partial config to full AppwriteConfig with sensible defaults
|
46
|
+
if (!partialConfig.appwriteEndpoint || !partialConfig.appwriteProject) {
|
47
|
+
throw new Error("JSON project config must contain at minimum 'endpoint' and 'projectId' fields");
|
48
|
+
}
|
49
|
+
return {
|
50
|
+
appwriteEndpoint: partialConfig.appwriteEndpoint,
|
51
|
+
appwriteProject: partialConfig.appwriteProject,
|
52
|
+
appwriteKey: partialConfig.appwriteKey || "",
|
53
|
+
apiMode: partialConfig.apiMode || "auto",
|
54
|
+
appwriteClient: null,
|
55
|
+
logging: partialConfig.logging || {
|
56
|
+
enabled: false,
|
57
|
+
level: "info",
|
58
|
+
console: false,
|
59
|
+
},
|
60
|
+
enableBackups: partialConfig.enableBackups ?? true,
|
61
|
+
backupInterval: partialConfig.backupInterval || 3600,
|
62
|
+
backupRetention: partialConfig.backupRetention || 30,
|
63
|
+
enableBackupCleanup: partialConfig.enableBackupCleanup ?? true,
|
64
|
+
enableMockData: partialConfig.enableMockData || false,
|
65
|
+
documentBucketId: partialConfig.documentBucketId || "documents",
|
66
|
+
usersCollectionName: partialConfig.usersCollectionName || "Members",
|
67
|
+
schemaConfig: partialConfig.schemaConfig || {
|
68
|
+
outputDirectory: "schemas",
|
69
|
+
yamlSchemaDirectory: ".yaml_schemas",
|
70
|
+
importDirectory: "importData",
|
71
|
+
collectionsDirectory: "collections",
|
72
|
+
tablesDirectory: "tables",
|
73
|
+
},
|
74
|
+
databases: partialConfig.databases || [],
|
75
|
+
buckets: partialConfig.buckets || [],
|
76
|
+
functions: partialConfig.functions || [],
|
77
|
+
collections: partialConfig.collections || [],
|
78
|
+
sessionCookie: partialConfig.sessionCookie,
|
79
|
+
authMethod: partialConfig.authMethod || "auto",
|
80
|
+
sessionMetadata: partialConfig.sessionMetadata,
|
81
|
+
};
|
82
|
+
}
|
83
|
+
throw new Error(`Unsupported configuration file type: ${ext}`);
|
84
|
+
}
|
85
|
+
/**
|
86
|
+
* Loads a YAML configuration file
|
87
|
+
* @param yamlPath Path to the YAML config file
|
88
|
+
* @param sessionOptions Optional session preservation options
|
89
|
+
* @returns Parsed AppwriteConfig
|
90
|
+
*/
|
91
|
+
async loadYaml(yamlPath, sessionOptions) {
|
92
|
+
if (!fs.existsSync(yamlPath)) {
|
93
|
+
throw new Error(`YAML config file not found: ${yamlPath}`);
|
94
|
+
}
|
95
|
+
try {
|
96
|
+
const config = await loadYamlConfig(yamlPath);
|
97
|
+
if (!config) {
|
98
|
+
throw new Error(`Failed to load YAML config from: ${yamlPath}`);
|
99
|
+
}
|
100
|
+
// Apply session preservation if provided
|
101
|
+
if (sessionOptions) {
|
102
|
+
if (sessionOptions.sessionCookie) {
|
103
|
+
config.sessionCookie = sessionOptions.sessionCookie;
|
104
|
+
}
|
105
|
+
if (sessionOptions.authMethod) {
|
106
|
+
config.authMethod = sessionOptions.authMethod;
|
107
|
+
}
|
108
|
+
if (sessionOptions.sessionMetadata) {
|
109
|
+
config.sessionMetadata = {
|
110
|
+
...config.sessionMetadata,
|
111
|
+
...sessionOptions.sessionMetadata,
|
112
|
+
};
|
113
|
+
}
|
114
|
+
}
|
115
|
+
MessageFormatter.success(`Loaded YAML config from: ${yamlPath}`, {
|
116
|
+
prefix: "Config",
|
117
|
+
});
|
118
|
+
return config;
|
119
|
+
}
|
120
|
+
catch (error) {
|
121
|
+
MessageFormatter.error(`Error loading YAML config from ${yamlPath}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
122
|
+
throw error;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
/**
|
126
|
+
* Loads a TypeScript configuration file
|
127
|
+
* @param tsPath Path to the TypeScript config file
|
128
|
+
* @returns Parsed AppwriteConfig
|
129
|
+
*/
|
130
|
+
async loadTypeScript(tsPath) {
|
131
|
+
if (!fs.existsSync(tsPath)) {
|
132
|
+
throw new Error(`TypeScript config file not found: ${tsPath}`);
|
133
|
+
}
|
134
|
+
const unregister = register(); // Register tsx enhancement
|
135
|
+
try {
|
136
|
+
const configUrl = pathToFileURL(tsPath).href;
|
137
|
+
const configModule = await import(configUrl);
|
138
|
+
const config = configModule.default?.default || configModule.default || configModule;
|
139
|
+
if (!config) {
|
140
|
+
throw new Error(`Failed to load TypeScript config from: ${tsPath}`);
|
141
|
+
}
|
142
|
+
MessageFormatter.success(`Loaded TypeScript config from: ${tsPath}`, {
|
143
|
+
prefix: "Config",
|
144
|
+
});
|
145
|
+
return config;
|
146
|
+
}
|
147
|
+
catch (error) {
|
148
|
+
MessageFormatter.error(`Error loading TypeScript config from ${tsPath}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
149
|
+
throw error;
|
150
|
+
}
|
151
|
+
finally {
|
152
|
+
unregister(); // Unregister tsx when done
|
153
|
+
}
|
154
|
+
}
|
155
|
+
/**
|
156
|
+
* Loads an appwrite.json project configuration file
|
157
|
+
* Converts projectId → appwriteProject and detects API mode
|
158
|
+
* @param jsonPath Path to the JSON config file
|
159
|
+
* @returns Partial AppwriteConfig (requires merging with defaults)
|
160
|
+
*/
|
161
|
+
async loadProjectConfig(jsonPath) {
|
162
|
+
if (!fs.existsSync(jsonPath)) {
|
163
|
+
throw new Error(`JSON config file not found: ${jsonPath}`);
|
164
|
+
}
|
165
|
+
try {
|
166
|
+
const projectConfig = loadAppwriteProjectConfig(jsonPath);
|
167
|
+
if (!projectConfig) {
|
168
|
+
throw new Error(`Failed to load project config from: ${jsonPath}`);
|
169
|
+
}
|
170
|
+
// Convert project config to AppwriteConfig format
|
171
|
+
const appwriteConfig = projectConfigToAppwriteConfig(projectConfig);
|
172
|
+
// Get collections from project config
|
173
|
+
const collections = getCollectionsFromProject(projectConfig);
|
174
|
+
if (collections.length > 0) {
|
175
|
+
appwriteConfig.collections = collections;
|
176
|
+
}
|
177
|
+
MessageFormatter.success(`Loaded project config from: ${jsonPath}`, {
|
178
|
+
prefix: "Config",
|
179
|
+
});
|
180
|
+
return appwriteConfig;
|
181
|
+
}
|
182
|
+
catch (error) {
|
183
|
+
MessageFormatter.error(`Error loading project config from ${jsonPath}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
184
|
+
throw error;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
/**
|
188
|
+
* Loads all collections from a collections/ directory
|
189
|
+
* Supports both YAML (.yaml, .yml) and TypeScript (.ts) files
|
190
|
+
* @param collectionsDir Path to the collections directory
|
191
|
+
* @param options Loading options
|
192
|
+
* @returns Array of loaded Collection objects
|
193
|
+
*/
|
194
|
+
async loadCollections(collectionsDir, options = {}) {
|
195
|
+
if (!fs.existsSync(collectionsDir)) {
|
196
|
+
MessageFormatter.debug(`Collections directory not found: ${collectionsDir}`, { prefix: "Config" });
|
197
|
+
return [];
|
198
|
+
}
|
199
|
+
const collections = [];
|
200
|
+
const unregister = register(); // Register tsx for TypeScript collections
|
201
|
+
try {
|
202
|
+
const collectionFiles = fs.readdirSync(collectionsDir);
|
203
|
+
MessageFormatter.success(`Loading from collections directory: ${collectionFiles.length} files found`, { prefix: "Config" });
|
204
|
+
for (const file of collectionFiles) {
|
205
|
+
if (file === "index.ts") {
|
206
|
+
continue;
|
207
|
+
}
|
208
|
+
const filePath = path.join(collectionsDir, file);
|
209
|
+
let collection = null;
|
210
|
+
// Handle YAML collections
|
211
|
+
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
|
212
|
+
collection = loadYamlCollection(filePath);
|
213
|
+
}
|
214
|
+
// Handle TypeScript collections
|
215
|
+
else if (file.endsWith(".ts")) {
|
216
|
+
try {
|
217
|
+
const fileUrl = pathToFileURL(filePath).href;
|
218
|
+
const collectionModule = await import(fileUrl);
|
219
|
+
const importedCollection = collectionModule.default?.default ||
|
220
|
+
collectionModule.default ||
|
221
|
+
collectionModule;
|
222
|
+
if (importedCollection) {
|
223
|
+
collection = importedCollection;
|
224
|
+
// Ensure importDefs are properly loaded
|
225
|
+
if (collectionModule.importDefs || collection.importDefs) {
|
226
|
+
collection.importDefs =
|
227
|
+
collectionModule.importDefs || collection.importDefs;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
}
|
231
|
+
catch (error) {
|
232
|
+
MessageFormatter.error(`Error loading TypeScript collection: ${file}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
233
|
+
}
|
234
|
+
}
|
235
|
+
if (collection) {
|
236
|
+
const collectionName = collection.name || collection.$id || file;
|
237
|
+
// Check for naming conflicts if existingNames provided
|
238
|
+
if (options.existingNames?.has(collectionName)) {
|
239
|
+
MessageFormatter.warning(`Skipping duplicate collection '${collectionName}' (already loaded)`, { prefix: "Config" });
|
240
|
+
continue;
|
241
|
+
}
|
242
|
+
// Mark as tables if requested
|
243
|
+
if (options.markAsTablesDir) {
|
244
|
+
collection._isFromTablesDir = true;
|
245
|
+
}
|
246
|
+
collections.push(collection);
|
247
|
+
}
|
248
|
+
}
|
249
|
+
MessageFormatter.success(`Loaded ${collections.length} collection(s) from ${collectionsDir}`, { prefix: "Config" });
|
250
|
+
}
|
251
|
+
catch (error) {
|
252
|
+
MessageFormatter.error(`Error loading collections from ${collectionsDir}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
253
|
+
}
|
254
|
+
finally {
|
255
|
+
unregister(); // Unregister tsx when done
|
256
|
+
}
|
257
|
+
return collections;
|
258
|
+
}
|
259
|
+
/**
|
260
|
+
* Loads all tables from a tables/ directory
|
261
|
+
* Supports both YAML (.yaml, .yml) and TypeScript (.ts) files
|
262
|
+
* @param tablesDir Path to the tables directory
|
263
|
+
* @param options Loading options
|
264
|
+
* @returns Array of loaded table objects
|
265
|
+
*/
|
266
|
+
async loadTables(tablesDir, options = {}) {
|
267
|
+
if (!fs.existsSync(tablesDir)) {
|
268
|
+
MessageFormatter.debug(`Tables directory not found: ${tablesDir}`, {
|
269
|
+
prefix: "Config",
|
270
|
+
});
|
271
|
+
return [];
|
272
|
+
}
|
273
|
+
const tables = [];
|
274
|
+
const unregister = register(); // Register tsx for TypeScript tables
|
275
|
+
try {
|
276
|
+
const tableFiles = fs.readdirSync(tablesDir);
|
277
|
+
MessageFormatter.success(`Loading from tables directory: ${tableFiles.length} files found`, { prefix: "Config" });
|
278
|
+
for (const file of tableFiles) {
|
279
|
+
if (file === "index.ts") {
|
280
|
+
continue;
|
281
|
+
}
|
282
|
+
const filePath = path.join(tablesDir, file);
|
283
|
+
let table = null;
|
284
|
+
// Handle YAML tables
|
285
|
+
if (file.endsWith(".yaml") || file.endsWith(".yml")) {
|
286
|
+
table = loadYamlTable(filePath);
|
287
|
+
}
|
288
|
+
// Handle TypeScript tables
|
289
|
+
else if (file.endsWith(".ts")) {
|
290
|
+
try {
|
291
|
+
const fileUrl = pathToFileURL(filePath).href;
|
292
|
+
const tableModule = await import(fileUrl);
|
293
|
+
const importedTable = tableModule.default?.default || tableModule.default || tableModule;
|
294
|
+
if (importedTable) {
|
295
|
+
table = importedTable;
|
296
|
+
// Ensure importDefs are properly loaded
|
297
|
+
if (tableModule.importDefs || table.importDefs) {
|
298
|
+
table.importDefs = tableModule.importDefs || table.importDefs;
|
299
|
+
}
|
300
|
+
}
|
301
|
+
}
|
302
|
+
catch (error) {
|
303
|
+
MessageFormatter.error(`Error loading TypeScript table: ${file}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
304
|
+
}
|
305
|
+
}
|
306
|
+
if (table) {
|
307
|
+
const tableName = table.name || table.tableId || table.$id || file;
|
308
|
+
// Check for naming conflicts if existingNames provided
|
309
|
+
if (options.existingNames?.has(tableName)) {
|
310
|
+
MessageFormatter.warning(`Skipping duplicate table '${tableName}' (already loaded from collections/)`, { prefix: "Config" });
|
311
|
+
continue;
|
312
|
+
}
|
313
|
+
// Always mark tables as coming from tables directory
|
314
|
+
table._isFromTablesDir = true;
|
315
|
+
tables.push(table);
|
316
|
+
}
|
317
|
+
}
|
318
|
+
MessageFormatter.success(`Loaded ${tables.length} table(s) from ${tablesDir}`, { prefix: "Config" });
|
319
|
+
}
|
320
|
+
catch (error) {
|
321
|
+
MessageFormatter.error(`Error loading tables from ${tablesDir}`, error instanceof Error ? error : undefined, { prefix: "Config" });
|
322
|
+
}
|
323
|
+
finally {
|
324
|
+
unregister(); // Unregister tsx when done
|
325
|
+
}
|
326
|
+
return tables;
|
327
|
+
}
|
328
|
+
/**
|
329
|
+
* Loads collections and tables with conflict detection
|
330
|
+
* Collections take priority over tables when names conflict
|
331
|
+
* @param collectionsDir Path to collections directory
|
332
|
+
* @param tablesDir Path to tables directory
|
333
|
+
* @returns Object containing combined arrays and conflict information
|
334
|
+
*/
|
335
|
+
async loadCollectionsAndTables(collectionsDir, tablesDir) {
|
336
|
+
const items = [];
|
337
|
+
const loadedNames = new Set();
|
338
|
+
const conflicts = [];
|
339
|
+
// Load from collections/ directory first (higher priority)
|
340
|
+
if (fs.existsSync(collectionsDir)) {
|
341
|
+
const collections = await this.loadCollections(collectionsDir);
|
342
|
+
for (const collection of collections) {
|
343
|
+
const name = collection.name || collection.$id || "";
|
344
|
+
loadedNames.add(name);
|
345
|
+
items.push(collection);
|
346
|
+
}
|
347
|
+
}
|
348
|
+
// Load from tables/ directory second (lower priority, check for conflicts)
|
349
|
+
if (fs.existsSync(tablesDir)) {
|
350
|
+
const tables = await this.loadTables(tablesDir, {
|
351
|
+
existingNames: loadedNames,
|
352
|
+
markAsTablesDir: true,
|
353
|
+
});
|
354
|
+
for (const table of tables) {
|
355
|
+
const name = table.name || table.tableId || table.$id || "";
|
356
|
+
// Check for conflicts
|
357
|
+
if (loadedNames.has(name)) {
|
358
|
+
conflicts.push({
|
359
|
+
name,
|
360
|
+
source1: "collections/",
|
361
|
+
source2: "tables/",
|
362
|
+
});
|
363
|
+
}
|
364
|
+
else {
|
365
|
+
loadedNames.add(name);
|
366
|
+
items.push(table);
|
367
|
+
}
|
368
|
+
}
|
369
|
+
}
|
370
|
+
const fromCollections = items.filter((item) => !item._isFromTablesDir)
|
371
|
+
.length;
|
372
|
+
const fromTables = items.filter((item) => item._isFromTablesDir).length;
|
373
|
+
return {
|
374
|
+
items,
|
375
|
+
fromCollections,
|
376
|
+
fromTables,
|
377
|
+
conflicts,
|
378
|
+
};
|
379
|
+
}
|
380
|
+
/**
|
381
|
+
* Validates that a configuration file can be loaded
|
382
|
+
* @param configPath Path to the configuration file
|
383
|
+
* @returns True if the file can be loaded, false otherwise
|
384
|
+
*/
|
385
|
+
canLoadConfig(configPath) {
|
386
|
+
if (!fs.existsSync(configPath)) {
|
387
|
+
return false;
|
388
|
+
}
|
389
|
+
const ext = path.extname(configPath).toLowerCase();
|
390
|
+
return ext === ".yaml" || ext === ".yml" || ext === ".ts" || ext === ".json";
|
391
|
+
}
|
392
|
+
/**
|
393
|
+
* Gets the type of a configuration file
|
394
|
+
* @param configPath Path to the configuration file
|
395
|
+
* @returns Configuration type or null if unknown
|
396
|
+
*/
|
397
|
+
getConfigType(configPath) {
|
398
|
+
const ext = path.extname(configPath).toLowerCase();
|
399
|
+
if (ext === ".yaml" || ext === ".yml") {
|
400
|
+
return "yaml";
|
401
|
+
}
|
402
|
+
else if (ext === ".ts") {
|
403
|
+
return "typescript";
|
404
|
+
}
|
405
|
+
else if (ext === ".json") {
|
406
|
+
return "json";
|
407
|
+
}
|
408
|
+
return null;
|
409
|
+
}
|
410
|
+
}
|
@@ -0,0 +1,208 @@
|
|
1
|
+
import type { AppwriteConfig } from "appwrite-utils";
|
2
|
+
import type { SessionAuthInfo } from "./SessionAuthService.js";
|
3
|
+
/**
|
4
|
+
* Configuration override options that can be applied from CLI arguments or environment
|
5
|
+
*/
|
6
|
+
export interface ConfigOverrides {
|
7
|
+
/** Appwrite endpoint URL (e.g., https://cloud.appwrite.io/v1) */
|
8
|
+
appwriteEndpoint?: string;
|
9
|
+
/** Appwrite project ID */
|
10
|
+
appwriteProject?: string;
|
11
|
+
/** API key for authentication */
|
12
|
+
appwriteKey?: string;
|
13
|
+
/** Session cookie for authentication */
|
14
|
+
sessionCookie?: string;
|
15
|
+
/** Explicitly set authentication method */
|
16
|
+
authMethod?: "session" | "apikey" | "auto";
|
17
|
+
}
|
18
|
+
/**
|
19
|
+
* Service responsible for merging configuration from multiple sources with proper priority handling.
|
20
|
+
*
|
21
|
+
* @description
|
22
|
+
* Handles configuration merging with the following priority order (highest to lowest):
|
23
|
+
* 1. CLI arguments/overrides (--endpoint, --apiKey, etc.)
|
24
|
+
* 2. Session cookie from ~/.appwrite/prefs.json
|
25
|
+
* 3. Config file values (YAML/TS/JSON)
|
26
|
+
* 4. Environment variables (APPWRITE_ENDPOINT, APPWRITE_PROJECT, etc.)
|
27
|
+
*
|
28
|
+
* The service ensures proper deep merging of nested objects while preserving array order
|
29
|
+
* and preventing undefined/null values from overwriting existing values.
|
30
|
+
*
|
31
|
+
* @example
|
32
|
+
* ```typescript
|
33
|
+
* const mergeService = new ConfigMergeService();
|
34
|
+
*
|
35
|
+
* // Merge session into config
|
36
|
+
* const configWithSession = mergeService.mergeSession(baseConfig, sessionInfo);
|
37
|
+
*
|
38
|
+
* // Apply CLI overrides
|
39
|
+
* const finalConfig = mergeService.applyOverrides(configWithSession, {
|
40
|
+
* appwriteEndpoint: 'https://custom-endpoint.com/v1',
|
41
|
+
* appwriteKey: 'custom-api-key'
|
42
|
+
* });
|
43
|
+
* ```
|
44
|
+
*/
|
45
|
+
export declare class ConfigMergeService {
|
46
|
+
/**
|
47
|
+
* Merges session authentication information into an existing configuration.
|
48
|
+
*
|
49
|
+
* @description
|
50
|
+
* Adds session authentication details to the configuration, setting:
|
51
|
+
* - sessionCookie: The authentication cookie from the session
|
52
|
+
* - authMethod: Set to "session" to indicate session-based auth
|
53
|
+
*
|
54
|
+
* The session endpoint and projectId are used for validation but not merged
|
55
|
+
* since they should match the config's values.
|
56
|
+
*
|
57
|
+
* @param config - Base configuration to merge session into
|
58
|
+
* @param session - Session authentication information
|
59
|
+
* @returns New configuration object with session merged (input config is not mutated)
|
60
|
+
*
|
61
|
+
* @example
|
62
|
+
* ```typescript
|
63
|
+
* const configWithSession = mergeService.mergeSession(config, {
|
64
|
+
* projectId: "my-project",
|
65
|
+
* endpoint: "https://cloud.appwrite.io/v1",
|
66
|
+
* sessionCookie: "eyJhbGc...",
|
67
|
+
* email: "user@example.com"
|
68
|
+
* });
|
69
|
+
* ```
|
70
|
+
*/
|
71
|
+
mergeSession(config: AppwriteConfig, session: SessionAuthInfo): AppwriteConfig;
|
72
|
+
/**
|
73
|
+
* Applies configuration overrides from CLI arguments or programmatic sources.
|
74
|
+
*
|
75
|
+
* @description
|
76
|
+
* Applies high-priority overrides that take precedence over all other config sources.
|
77
|
+
* Only applies values that are explicitly set (not undefined or null).
|
78
|
+
*
|
79
|
+
* This method is typically used to apply CLI arguments like:
|
80
|
+
* - --endpoint
|
81
|
+
* - --apiKey
|
82
|
+
* - --project
|
83
|
+
*
|
84
|
+
* @param config - Base configuration to apply overrides to
|
85
|
+
* @param overrides - Override values to apply
|
86
|
+
* @returns New configuration object with overrides applied (input config is not mutated)
|
87
|
+
*
|
88
|
+
* @example
|
89
|
+
* ```typescript
|
90
|
+
* const overriddenConfig = mergeService.applyOverrides(config, {
|
91
|
+
* appwriteEndpoint: 'https://custom.appwrite.io/v1',
|
92
|
+
* appwriteKey: 'my-api-key',
|
93
|
+
* authMethod: 'apikey'
|
94
|
+
* });
|
95
|
+
* ```
|
96
|
+
*/
|
97
|
+
applyOverrides(config: AppwriteConfig, overrides: ConfigOverrides): AppwriteConfig;
|
98
|
+
/**
|
99
|
+
* Merges environment variables into the configuration.
|
100
|
+
*
|
101
|
+
* @description
|
102
|
+
* Reads environment variables and merges them into the config with the lowest priority.
|
103
|
+
* Only applies environment variables that are set and have non-empty values.
|
104
|
+
*
|
105
|
+
* Supported environment variables:
|
106
|
+
* - APPWRITE_ENDPOINT: Appwrite API endpoint URL
|
107
|
+
* - APPWRITE_PROJECT: Appwrite project ID
|
108
|
+
* - APPWRITE_API_KEY: API key for authentication
|
109
|
+
* - APPWRITE_SESSION_COOKIE: Session cookie for authentication
|
110
|
+
*
|
111
|
+
* Environment variables are only applied if the corresponding config value is not already set.
|
112
|
+
*
|
113
|
+
* @param config - Base configuration to merge environment variables into
|
114
|
+
* @returns New configuration object with environment variables merged (input config is not mutated)
|
115
|
+
*
|
116
|
+
* @example
|
117
|
+
* ```typescript
|
118
|
+
* // With env vars: APPWRITE_ENDPOINT=https://cloud.appwrite.io/v1
|
119
|
+
* const configWithEnv = mergeService.mergeEnvironmentVariables(config);
|
120
|
+
* // configWithEnv.appwriteEndpoint will be set from env var if not already set
|
121
|
+
* ```
|
122
|
+
*/
|
123
|
+
mergeEnvironmentVariables(config: AppwriteConfig): AppwriteConfig;
|
124
|
+
/**
|
125
|
+
* Performs a deep merge of multiple partial configuration sources into a complete configuration.
|
126
|
+
*
|
127
|
+
* @description
|
128
|
+
* Merges multiple configuration sources using a deep merge strategy that:
|
129
|
+
* - Recursively merges nested objects
|
130
|
+
* - Preserves array order (arrays are replaced, not merged)
|
131
|
+
* - Skips undefined and null values (they don't overwrite existing values)
|
132
|
+
* - Later sources in the array take precedence over earlier ones
|
133
|
+
*
|
134
|
+
* This method is useful when combining:
|
135
|
+
* - Base defaults with file-based config
|
136
|
+
* - Multiple configuration files
|
137
|
+
* - Config fragments from different sources
|
138
|
+
*
|
139
|
+
* @param sources - Array of partial configurations to merge (order matters: later sources override earlier ones)
|
140
|
+
* @returns Complete merged configuration object
|
141
|
+
*
|
142
|
+
* @throws {Error} If no valid configuration can be produced from the sources
|
143
|
+
*
|
144
|
+
* @example
|
145
|
+
* ```typescript
|
146
|
+
* const merged = mergeService.mergeSources([
|
147
|
+
* envConfig, // Lowest priority
|
148
|
+
* fileConfig, // Medium priority
|
149
|
+
* sessionConfig, // Higher priority
|
150
|
+
* overrideConfig // Highest priority
|
151
|
+
* ]);
|
152
|
+
* ```
|
153
|
+
*/
|
154
|
+
mergeSources(sources: Array<Partial<AppwriteConfig>>): AppwriteConfig;
|
155
|
+
/**
|
156
|
+
* Internal helper for deep merging two configuration objects.
|
157
|
+
*
|
158
|
+
* @description
|
159
|
+
* Performs a deep merge of two objects with special handling for:
|
160
|
+
* - Plain objects: Recursively merged
|
161
|
+
* - Arrays: Target array is replaced by source array (not merged)
|
162
|
+
* - Undefined/null values in source: Skipped (don't overwrite target)
|
163
|
+
* - Primitive values: Source overwrites target
|
164
|
+
*
|
165
|
+
* This is used internally by mergeSources but can also be used directly
|
166
|
+
* for specific merge operations.
|
167
|
+
*
|
168
|
+
* @param target - Base configuration object
|
169
|
+
* @param source - Configuration to merge into target
|
170
|
+
* @returns New deeply merged configuration object
|
171
|
+
*
|
172
|
+
* @private
|
173
|
+
*/
|
174
|
+
private deepMergeConfigs;
|
175
|
+
/**
|
176
|
+
* Creates a configuration by merging all sources in the correct priority order.
|
177
|
+
*
|
178
|
+
* @description
|
179
|
+
* Convenience method that applies the full configuration merge pipeline:
|
180
|
+
* 1. Start with base config (from file)
|
181
|
+
* 2. Merge environment variables (lowest priority)
|
182
|
+
* 3. Merge session information (if provided)
|
183
|
+
* 4. Apply CLI/programmatic overrides (highest priority)
|
184
|
+
*
|
185
|
+
* This is the recommended way to build a final configuration from all sources.
|
186
|
+
*
|
187
|
+
* @param baseConfig - Configuration loaded from file (YAML/TS/JSON)
|
188
|
+
* @param options - Optional merge options
|
189
|
+
* @param options.session - Session authentication to merge
|
190
|
+
* @param options.overrides - CLI/programmatic overrides to apply
|
191
|
+
* @param options.includeEnv - Whether to include environment variables (default: true)
|
192
|
+
* @returns Final merged configuration with all sources applied in priority order
|
193
|
+
*
|
194
|
+
* @example
|
195
|
+
* ```typescript
|
196
|
+
* const finalConfig = mergeService.mergeAllSources(fileConfig, {
|
197
|
+
* session: sessionInfo,
|
198
|
+
* overrides: cliOverrides,
|
199
|
+
* includeEnv: true
|
200
|
+
* });
|
201
|
+
* ```
|
202
|
+
*/
|
203
|
+
mergeAllSources(baseConfig: AppwriteConfig, options?: {
|
204
|
+
session?: SessionAuthInfo;
|
205
|
+
overrides?: ConfigOverrides;
|
206
|
+
includeEnv?: boolean;
|
207
|
+
}): AppwriteConfig;
|
208
|
+
}
|