appwrite-utils-cli 0.10.86 → 1.0.2
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/.appwrite/.yaml_schemas/appwrite-config.schema.json +380 -0
- package/.appwrite/.yaml_schemas/collection.schema.json +255 -0
- package/.appwrite/collections/Categories.yaml +182 -0
- package/.appwrite/collections/ExampleCollection.yaml +36 -0
- package/.appwrite/collections/Posts.yaml +227 -0
- package/.appwrite/collections/Users.yaml +149 -0
- package/.appwrite/config.yaml +109 -0
- package/.appwrite/import/README.md +148 -0
- package/.appwrite/import/categories-import.yaml +129 -0
- package/.appwrite/import/posts-import.yaml +208 -0
- package/.appwrite/import/users-import.yaml +130 -0
- package/.appwrite/importData/categories.json +194 -0
- package/.appwrite/importData/posts.json +270 -0
- package/.appwrite/importData/users.json +220 -0
- package/.appwrite/schemas/categories.json +128 -0
- package/.appwrite/schemas/exampleCollection.json +52 -0
- package/.appwrite/schemas/posts.json +173 -0
- package/.appwrite/schemas/users.json +125 -0
- package/README.md +264 -33
- package/dist/collections/attributes.js +3 -2
- package/dist/collections/methods.js +56 -38
- package/dist/config/yamlConfig.d.ts +501 -0
- package/dist/config/yamlConfig.js +452 -0
- package/dist/databases/setup.d.ts +6 -0
- package/dist/databases/setup.js +119 -0
- package/dist/functions/methods.d.ts +1 -1
- package/dist/functions/methods.js +5 -2
- package/dist/functions/openapi.d.ts +4 -0
- package/dist/functions/openapi.js +60 -0
- package/dist/interactiveCLI.d.ts +5 -0
- package/dist/interactiveCLI.js +194 -49
- package/dist/main.js +91 -30
- package/dist/migrations/afterImportActions.js +2 -2
- package/dist/migrations/appwriteToX.d.ts +10 -0
- package/dist/migrations/appwriteToX.js +15 -4
- package/dist/migrations/backup.d.ts +16 -16
- package/dist/migrations/dataLoader.d.ts +83 -1
- package/dist/migrations/dataLoader.js +4 -4
- package/dist/migrations/importController.js +25 -18
- package/dist/migrations/importDataActions.js +2 -2
- package/dist/migrations/logging.d.ts +9 -1
- package/dist/migrations/logging.js +41 -22
- package/dist/migrations/migrationHelper.d.ts +4 -4
- package/dist/migrations/relationships.js +1 -1
- package/dist/migrations/services/DataTransformationService.d.ts +55 -0
- package/dist/migrations/services/DataTransformationService.js +158 -0
- package/dist/migrations/services/FileHandlerService.d.ts +75 -0
- package/dist/migrations/services/FileHandlerService.js +236 -0
- package/dist/migrations/services/ImportOrchestrator.d.ts +97 -0
- package/dist/migrations/services/ImportOrchestrator.js +488 -0
- package/dist/migrations/services/RateLimitManager.d.ts +138 -0
- package/dist/migrations/services/RateLimitManager.js +279 -0
- package/dist/migrations/services/RelationshipResolver.d.ts +120 -0
- package/dist/migrations/services/RelationshipResolver.js +332 -0
- package/dist/migrations/services/UserMappingService.d.ts +109 -0
- package/dist/migrations/services/UserMappingService.js +277 -0
- package/dist/migrations/services/ValidationService.d.ts +74 -0
- package/dist/migrations/services/ValidationService.js +260 -0
- package/dist/migrations/transfer.d.ts +0 -6
- package/dist/migrations/transfer.js +16 -132
- package/dist/migrations/yaml/YamlImportConfigLoader.d.ts +384 -0
- package/dist/migrations/yaml/YamlImportConfigLoader.js +375 -0
- package/dist/migrations/yaml/YamlImportIntegration.d.ts +87 -0
- package/dist/migrations/yaml/YamlImportIntegration.js +330 -0
- package/dist/migrations/yaml/generateImportSchemas.d.ts +17 -0
- package/dist/migrations/yaml/generateImportSchemas.js +575 -0
- package/dist/schemas/authUser.d.ts +9 -9
- package/dist/shared/attributeManager.d.ts +17 -0
- package/dist/shared/attributeManager.js +273 -0
- package/dist/shared/confirmationDialogs.d.ts +75 -0
- package/dist/shared/confirmationDialogs.js +236 -0
- package/dist/shared/functionManager.d.ts +48 -0
- package/dist/shared/functionManager.js +322 -0
- package/dist/shared/indexManager.d.ts +24 -0
- package/dist/shared/indexManager.js +150 -0
- package/dist/shared/jsonSchemaGenerator.d.ts +51 -0
- package/dist/shared/jsonSchemaGenerator.js +313 -0
- package/dist/shared/logging.d.ts +10 -0
- package/dist/shared/logging.js +46 -0
- package/dist/shared/messageFormatter.d.ts +37 -0
- package/dist/shared/messageFormatter.js +152 -0
- package/dist/shared/migrationHelpers.d.ts +173 -0
- package/dist/shared/migrationHelpers.js +142 -0
- package/dist/shared/operationLogger.d.ts +3 -0
- package/dist/shared/operationLogger.js +25 -0
- package/dist/shared/operationQueue.d.ts +13 -0
- package/dist/shared/operationQueue.js +79 -0
- package/dist/shared/progressManager.d.ts +62 -0
- package/dist/shared/progressManager.js +215 -0
- package/dist/shared/schemaGenerator.d.ts +18 -0
- package/dist/shared/schemaGenerator.js +523 -0
- package/dist/storage/methods.d.ts +3 -1
- package/dist/storage/methods.js +144 -55
- package/dist/storage/schemas.d.ts +56 -16
- package/dist/types.d.ts +2 -2
- package/dist/types.js +1 -1
- package/dist/users/methods.d.ts +16 -0
- package/dist/users/methods.js +276 -0
- package/dist/utils/configMigration.d.ts +1 -0
- package/dist/utils/configMigration.js +262 -0
- package/dist/utils/dataConverters.d.ts +46 -0
- package/dist/utils/dataConverters.js +139 -0
- package/dist/utils/loadConfigs.d.ts +15 -4
- package/dist/utils/loadConfigs.js +379 -51
- package/dist/utils/schemaStrings.js +2 -1
- package/dist/utils/setupFiles.d.ts +2 -1
- package/dist/utils/setupFiles.js +723 -28
- package/dist/utils/validationRules.d.ts +43 -0
- package/dist/utils/validationRules.js +42 -0
- package/dist/utils/yamlConverter.d.ts +48 -0
- package/dist/utils/yamlConverter.js +98 -0
- package/dist/utilsController.js +65 -43
- package/package.json +19 -15
- package/src/collections/attributes.ts +3 -2
- package/src/collections/methods.ts +85 -51
- package/src/config/yamlConfig.ts +488 -0
- package/src/{migrations/setupDatabase.ts → databases/setup.ts} +11 -5
- package/src/functions/methods.ts +8 -4
- package/src/functions/templates/count-docs-in-collection/package.json +25 -0
- package/src/functions/templates/count-docs-in-collection/tsconfig.json +28 -0
- package/src/functions/templates/typescript-node/package.json +24 -0
- package/src/functions/templates/typescript-node/tsconfig.json +28 -0
- package/src/functions/templates/uv/README.md +31 -0
- package/src/functions/templates/uv/pyproject.toml +29 -0
- package/src/interactiveCLI.ts +226 -61
- package/src/main.ts +111 -37
- package/src/migrations/afterImportActions.ts +2 -2
- package/src/migrations/appwriteToX.ts +17 -4
- package/src/migrations/dataLoader.ts +4 -4
- package/src/migrations/importController.ts +30 -22
- package/src/migrations/importDataActions.ts +2 -2
- package/src/migrations/relationships.ts +1 -1
- package/src/migrations/services/DataTransformationService.ts +196 -0
- package/src/migrations/services/FileHandlerService.ts +311 -0
- package/src/migrations/services/ImportOrchestrator.ts +669 -0
- package/src/migrations/services/RateLimitManager.ts +363 -0
- package/src/migrations/services/RelationshipResolver.ts +461 -0
- package/src/migrations/services/UserMappingService.ts +345 -0
- package/src/migrations/services/ValidationService.ts +349 -0
- package/src/migrations/transfer.ts +22 -228
- package/src/migrations/yaml/YamlImportConfigLoader.ts +427 -0
- package/src/migrations/yaml/YamlImportIntegration.ts +419 -0
- package/src/migrations/yaml/generateImportSchemas.ts +589 -0
- package/src/shared/attributeManager.ts +429 -0
- package/src/shared/confirmationDialogs.ts +327 -0
- package/src/shared/functionManager.ts +515 -0
- package/src/shared/indexManager.ts +253 -0
- package/src/shared/jsonSchemaGenerator.ts +403 -0
- package/src/shared/logging.ts +74 -0
- package/src/shared/messageFormatter.ts +195 -0
- package/src/{migrations/migrationHelper.ts → shared/migrationHelpers.ts} +22 -4
- package/src/{migrations/helper.ts → shared/operationLogger.ts} +7 -2
- package/src/{migrations/queue.ts → shared/operationQueue.ts} +1 -1
- package/src/shared/progressManager.ts +278 -0
- package/src/{migrations/schemaStrings.ts → shared/schemaGenerator.ts} +71 -17
- package/src/storage/methods.ts +199 -78
- package/src/types.ts +2 -2
- package/src/{migrations/users.ts → users/methods.ts} +2 -2
- package/src/utils/configMigration.ts +349 -0
- package/src/utils/loadConfigs.ts +416 -52
- package/src/utils/schemaStrings.ts +2 -1
- package/src/utils/setupFiles.ts +742 -40
- package/src/{migrations → utils}/validationRules.ts +1 -1
- package/src/utils/yamlConverter.ts +131 -0
- package/src/utilsController.ts +75 -54
- package/src/functions/templates/poetry/README.md +0 -30
- package/src/functions/templates/poetry/pyproject.toml +0 -16
- package/src/migrations/attributes.ts +0 -561
- package/src/migrations/backup.ts +0 -205
- package/src/migrations/databases.ts +0 -39
- package/src/migrations/dbHelpers.ts +0 -92
- package/src/migrations/indexes.ts +0 -40
- package/src/migrations/logging.ts +0 -29
- package/src/migrations/storage.ts +0 -538
- /package/src/{migrations → functions}/openapi.ts +0 -0
- /package/src/functions/templates/{poetry → uv}/src/__init__.py +0 -0
- /package/src/functions/templates/{poetry → uv}/src/index.py +0 -0
- /package/src/{migrations/converters.ts → utils/dataConverters.ts} +0 -0
@@ -0,0 +1,139 @@
|
|
1
|
+
import { converterFunctions } from "appwrite-utils";
|
2
|
+
import { cloneDeep, isPlainObject } from "es-toolkit";
|
3
|
+
/**
|
4
|
+
* Deeply converts all properties of an object (or array) to strings.
|
5
|
+
* @param data The input data to convert.
|
6
|
+
* @returns The data with all its properties converted to strings.
|
7
|
+
*/
|
8
|
+
export const deepAnyToString = (data) => {
|
9
|
+
if (Array.isArray(data)) {
|
10
|
+
return data.map((item) => deepAnyToString(item));
|
11
|
+
}
|
12
|
+
else if (isPlainObject(data)) {
|
13
|
+
return Object.keys(data).reduce((acc, key) => {
|
14
|
+
acc[key] = deepAnyToString(data[key]);
|
15
|
+
return acc;
|
16
|
+
}, {});
|
17
|
+
}
|
18
|
+
else {
|
19
|
+
return converterFunctions.anyToString(data);
|
20
|
+
}
|
21
|
+
};
|
22
|
+
/**
|
23
|
+
* Performs a deep conversion of all values in a nested structure to the specified type.
|
24
|
+
* Uses a conversion function like anyToString, anyToNumber, etc.
|
25
|
+
* @param data The data to convert.
|
26
|
+
* @param convertFn The conversion function to apply.
|
27
|
+
* @returns The converted data.
|
28
|
+
*/
|
29
|
+
export const deepConvert = (data, convertFn) => {
|
30
|
+
if (Array.isArray(data)) {
|
31
|
+
return data.map((item) => deepConvert(item, convertFn));
|
32
|
+
}
|
33
|
+
else if (isPlainObject(data)) {
|
34
|
+
return Object.keys(data).reduce((acc, key) => {
|
35
|
+
acc[key] = deepConvert(data[key], convertFn);
|
36
|
+
return acc;
|
37
|
+
}, {});
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
return convertFn(data);
|
41
|
+
}
|
42
|
+
};
|
43
|
+
/**
|
44
|
+
* Converts an entire object's properties to different types based on a provided schema.
|
45
|
+
* @param obj The object to convert.
|
46
|
+
* @param schema A mapping of object keys to conversion functions.
|
47
|
+
* @returns The converted object.
|
48
|
+
*/
|
49
|
+
export const convertObjectBySchema = (obj, schema) => {
|
50
|
+
return Object.keys(obj).reduce((acc, key) => {
|
51
|
+
const convertFn = schema[key];
|
52
|
+
acc[key] = convertFn ? convertFn(obj[key]) : obj[key];
|
53
|
+
return acc;
|
54
|
+
}, {});
|
55
|
+
};
|
56
|
+
/**
|
57
|
+
* Converts the keys of an object based on a provided attributeMappings.
|
58
|
+
* Each key in the object is checked against attributeMappings; if a matching entry is found,
|
59
|
+
* the key is renamed to the targetKey specified in attributeMappings.
|
60
|
+
*
|
61
|
+
* @param obj The object to convert.
|
62
|
+
* @param attributeMappings The attributeMappings defining how keys in the object should be converted.
|
63
|
+
* @returns The converted object with keys renamed according to attributeMappings.
|
64
|
+
*/
|
65
|
+
export const convertObjectByAttributeMappings = (obj, attributeMappings) => {
|
66
|
+
const result = {};
|
67
|
+
// Correctly handle [any] notation by mapping or aggregating over all elements or keys
|
68
|
+
const resolveValue = (obj, path) => {
|
69
|
+
const parts = path.split(".");
|
70
|
+
let current = obj;
|
71
|
+
for (let i = 0; i < parts.length; i++) {
|
72
|
+
if (parts[i] === "[any]") {
|
73
|
+
if (Array.isArray(current)) {
|
74
|
+
// If current is an array, apply resolution to each item
|
75
|
+
return current.map((item) => resolveValue(item, parts.slice(i + 1).join(".")));
|
76
|
+
}
|
77
|
+
else if (typeof current === "object" && current !== null) {
|
78
|
+
// If current is an object, aggregate values from all keys
|
79
|
+
return Object.values(current).map((value) => resolveValue(value, parts.slice(i + 1).join(".")));
|
80
|
+
}
|
81
|
+
}
|
82
|
+
else {
|
83
|
+
current = current[parts[i]];
|
84
|
+
if (current === undefined)
|
85
|
+
return undefined;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
return current;
|
89
|
+
};
|
90
|
+
for (const mapping of attributeMappings) {
|
91
|
+
if (mapping.valueToSet !== undefined) {
|
92
|
+
result[mapping.targetKey] = mapping.valueToSet;
|
93
|
+
}
|
94
|
+
else if (Array.isArray(mapping.oldKeys)) {
|
95
|
+
// Collect and flatten values from multiple oldKeys
|
96
|
+
const values = mapping.oldKeys
|
97
|
+
.map((oldKey) => resolveValue(obj, oldKey))
|
98
|
+
.flat(Infinity);
|
99
|
+
if (values.length > 0) {
|
100
|
+
result[mapping.targetKey] = values.filter((value) => value !== undefined);
|
101
|
+
}
|
102
|
+
else {
|
103
|
+
result[mapping.targetKey] = null;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
else if (mapping.oldKey) {
|
107
|
+
// Resolve single oldKey
|
108
|
+
const value = resolveValue(obj, mapping.oldKey);
|
109
|
+
if (value !== undefined) {
|
110
|
+
result[mapping.targetKey] = Array.isArray(value)
|
111
|
+
? value.flat(Infinity)
|
112
|
+
: value;
|
113
|
+
}
|
114
|
+
else {
|
115
|
+
result[mapping.targetKey] = value ? value : null;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
return result;
|
120
|
+
};
|
121
|
+
/**
|
122
|
+
* Ensures data conversion without mutating the original input.
|
123
|
+
* @param data The data to convert.
|
124
|
+
* @param convertFn The conversion function to apply.
|
125
|
+
* @returns The converted data.
|
126
|
+
*/
|
127
|
+
export const immutableConvert = (data, convertFn) => {
|
128
|
+
const clonedData = cloneDeep(data);
|
129
|
+
return convertFn(clonedData);
|
130
|
+
};
|
131
|
+
/**
|
132
|
+
* Validates a string against a regular expression and returns the string if valid, or null.
|
133
|
+
* @param value The string to validate.
|
134
|
+
* @param pattern The regex pattern to validate against.
|
135
|
+
* @returns The original string if valid, otherwise null.
|
136
|
+
*/
|
137
|
+
export const validateString = (value, pattern) => {
|
138
|
+
return pattern.test(value) ? value : null;
|
139
|
+
};
|
@@ -1,14 +1,25 @@
|
|
1
1
|
import { type AppwriteConfig } from "appwrite-utils";
|
2
2
|
/**
|
3
|
-
* Recursively searches for
|
3
|
+
* Recursively searches for configuration files starting from the given directory.
|
4
|
+
* Priority: 1) YAML configs in .appwrite directories, 2) appwriteConfig.ts files in subdirectories
|
4
5
|
* @param dir The directory to start the search from.
|
5
|
-
* @returns The path
|
6
|
+
* @returns The directory path where the config was found, suitable for passing to loadConfig().
|
6
7
|
*/
|
7
8
|
export declare const findAppwriteConfig: (dir: string) => string | null;
|
9
|
+
/**
|
10
|
+
* Loads the Appwrite configuration and returns both config and the path where it was found.
|
11
|
+
* @param configDir The directory to search for config files.
|
12
|
+
* @returns Object containing the config and the actual path where it was found.
|
13
|
+
*/
|
14
|
+
export declare const loadConfigWithPath: (configDir: string) => Promise<{
|
15
|
+
config: AppwriteConfig;
|
16
|
+
actualConfigPath: string;
|
17
|
+
}>;
|
8
18
|
/**
|
9
19
|
* Loads the Appwrite configuration and all collection configurations from a specified directory.
|
10
|
-
*
|
20
|
+
* Supports both YAML and TypeScript config formats with backward compatibility.
|
21
|
+
* @param configDir The directory containing the config file and collections folder.
|
11
22
|
* @returns The loaded Appwrite configuration including collections.
|
12
23
|
*/
|
13
24
|
export declare const loadConfig: (configDir: string) => Promise<AppwriteConfig>;
|
14
|
-
export declare const findFunctionsDir: (dir: string) => string | null;
|
25
|
+
export declare const findFunctionsDir: (dir: string, depth?: number) => string | null;
|
@@ -4,84 +4,412 @@ import {} from "appwrite-utils";
|
|
4
4
|
import { register } from "tsx/esm/api"; // Import the register function
|
5
5
|
import { pathToFileURL } from "node:url";
|
6
6
|
import chalk from "chalk";
|
7
|
+
import { findYamlConfig, loadYamlConfig } from "../config/yamlConfig.js";
|
8
|
+
import yaml from "js-yaml";
|
9
|
+
import { z } from "zod";
|
7
10
|
/**
|
8
|
-
* Recursively searches for
|
11
|
+
* Recursively searches for configuration files starting from the given directory.
|
12
|
+
* Priority: 1) YAML configs in .appwrite directories, 2) appwriteConfig.ts files in subdirectories
|
9
13
|
* @param dir The directory to start the search from.
|
10
|
-
* @returns The path
|
14
|
+
* @returns The directory path where the config was found, suitable for passing to loadConfig().
|
11
15
|
*/
|
12
16
|
export const findAppwriteConfig = (dir) => {
|
13
|
-
|
17
|
+
// First try to find YAML config (already searches recursively for .appwrite dirs)
|
18
|
+
const yamlConfig = findYamlConfig(dir);
|
19
|
+
if (yamlConfig) {
|
20
|
+
// Return the directory containing the config file
|
21
|
+
return path.dirname(yamlConfig);
|
22
|
+
}
|
23
|
+
// Fall back to TypeScript config search
|
24
|
+
const tsConfigPath = findAppwriteConfigTS(dir);
|
25
|
+
if (tsConfigPath) {
|
26
|
+
return path.dirname(tsConfigPath);
|
27
|
+
}
|
28
|
+
return null;
|
29
|
+
};
|
30
|
+
const shouldIgnoreDirectory = (dirName) => {
|
31
|
+
const ignoredDirs = [
|
32
|
+
'node_modules',
|
33
|
+
'dist',
|
34
|
+
'build',
|
35
|
+
'coverage',
|
36
|
+
'.next',
|
37
|
+
'.nuxt',
|
38
|
+
'.cache',
|
39
|
+
'.git',
|
40
|
+
'.svn',
|
41
|
+
'.hg',
|
42
|
+
'__pycache__',
|
43
|
+
'.pytest_cache',
|
44
|
+
'.mypy_cache',
|
45
|
+
'venv',
|
46
|
+
'.venv',
|
47
|
+
'env',
|
48
|
+
'.env',
|
49
|
+
'target',
|
50
|
+
'out',
|
51
|
+
'bin',
|
52
|
+
'obj',
|
53
|
+
'.vs',
|
54
|
+
'.vscode',
|
55
|
+
'.idea',
|
56
|
+
'temp',
|
57
|
+
'tmp',
|
58
|
+
'.tmp',
|
59
|
+
'logs',
|
60
|
+
'log',
|
61
|
+
'.DS_Store',
|
62
|
+
'Thumbs.db'
|
63
|
+
];
|
64
|
+
return ignoredDirs.includes(dirName) ||
|
65
|
+
dirName.startsWith('.git') ||
|
66
|
+
dirName.startsWith('node_modules') ||
|
67
|
+
dirName.startsWith('.');
|
68
|
+
};
|
69
|
+
const findAppwriteConfigTS = (dir, depth = 0) => {
|
70
|
+
// Limit search depth to prevent infinite recursion
|
71
|
+
if (depth > 10) {
|
14
72
|
return null;
|
15
73
|
}
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
74
|
+
if (shouldIgnoreDirectory(path.basename(dir))) {
|
75
|
+
return null;
|
76
|
+
}
|
77
|
+
try {
|
78
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
79
|
+
// First check current directory for appwriteConfig.ts
|
80
|
+
for (const entry of entries) {
|
81
|
+
if (entry.isFile() && entry.name === "appwriteConfig.ts") {
|
82
|
+
console.log(`Found appwriteConfig.ts at: ${path.join(dir, entry.name)}`);
|
83
|
+
return path.join(dir, entry.name);
|
84
|
+
}
|
22
85
|
}
|
23
|
-
|
24
|
-
|
86
|
+
// Then search subdirectories
|
87
|
+
for (const entry of entries) {
|
88
|
+
if (entry.isDirectory() && !shouldIgnoreDirectory(entry.name)) {
|
89
|
+
const result = findAppwriteConfigTS(path.join(dir, entry.name), depth + 1);
|
90
|
+
if (result)
|
91
|
+
return result;
|
92
|
+
}
|
25
93
|
}
|
26
94
|
}
|
95
|
+
catch (error) {
|
96
|
+
// Ignore directory access errors
|
97
|
+
console.log(`Error accessing directory ${dir}:`, error);
|
98
|
+
}
|
27
99
|
return null;
|
28
100
|
};
|
101
|
+
/**
|
102
|
+
* Loads the Appwrite configuration and returns both config and the path where it was found.
|
103
|
+
* @param configDir The directory to search for config files.
|
104
|
+
* @returns Object containing the config and the actual path where it was found.
|
105
|
+
*/
|
106
|
+
export const loadConfigWithPath = async (configDir) => {
|
107
|
+
let config = null;
|
108
|
+
let actualConfigPath = null;
|
109
|
+
// Check if we're given the .appwrite directory directly
|
110
|
+
if (configDir.endsWith('.appwrite')) {
|
111
|
+
// Look for config files directly in this directory
|
112
|
+
const possibleYamlFiles = ['config.yaml', 'config.yml', 'appwriteConfig.yaml', 'appwriteConfig.yml'];
|
113
|
+
for (const fileName of possibleYamlFiles) {
|
114
|
+
const yamlPath = path.join(configDir, fileName);
|
115
|
+
if (fs.existsSync(yamlPath)) {
|
116
|
+
config = await loadYamlConfig(yamlPath);
|
117
|
+
actualConfigPath = yamlPath;
|
118
|
+
break;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
else {
|
123
|
+
// Original logic: search for .appwrite directories
|
124
|
+
const yamlConfigPath = findYamlConfig(configDir);
|
125
|
+
if (yamlConfigPath) {
|
126
|
+
config = await loadYamlConfig(yamlConfigPath);
|
127
|
+
actualConfigPath = yamlConfigPath;
|
128
|
+
}
|
129
|
+
}
|
130
|
+
// Fall back to TypeScript config if YAML not found or failed to load
|
131
|
+
if (!config) {
|
132
|
+
const configPath = path.join(configDir, "appwriteConfig.ts");
|
133
|
+
// Only try to load TypeScript config if the file exists
|
134
|
+
if (fs.existsSync(configPath)) {
|
135
|
+
const unregister = register(); // Register tsx enhancement
|
136
|
+
try {
|
137
|
+
console.log(`Loading TypeScript config from: ${configPath}`);
|
138
|
+
const configUrl = pathToFileURL(configPath).href;
|
139
|
+
const configModule = (await import(configUrl));
|
140
|
+
config = configModule.default?.default || configModule.default || configModule;
|
141
|
+
if (!config) {
|
142
|
+
throw new Error("Failed to load config");
|
143
|
+
}
|
144
|
+
actualConfigPath = configPath;
|
145
|
+
}
|
146
|
+
finally {
|
147
|
+
unregister(); // Unregister tsx when done
|
148
|
+
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
if (!config || !actualConfigPath) {
|
152
|
+
throw new Error("No valid configuration found");
|
153
|
+
}
|
154
|
+
// Determine collections directory based on actual config file location
|
155
|
+
let collectionsDir;
|
156
|
+
const configFileDir = path.dirname(actualConfigPath);
|
157
|
+
// Check if config is in .appwrite directory
|
158
|
+
if (configFileDir.endsWith('.appwrite')) {
|
159
|
+
collectionsDir = path.join(configFileDir, "collections");
|
160
|
+
}
|
161
|
+
else {
|
162
|
+
// Config is in root or other directory
|
163
|
+
collectionsDir = path.join(configFileDir, "collections");
|
164
|
+
}
|
165
|
+
// Load collections if they exist
|
166
|
+
if (fs.existsSync(collectionsDir)) {
|
167
|
+
const unregister = register(); // Register tsx for collections
|
168
|
+
try {
|
169
|
+
const collectionFiles = fs.readdirSync(collectionsDir);
|
170
|
+
config.collections = [];
|
171
|
+
for (const file of collectionFiles) {
|
172
|
+
if (file === "index.ts") {
|
173
|
+
continue;
|
174
|
+
}
|
175
|
+
const filePath = path.join(collectionsDir, file);
|
176
|
+
// Handle YAML collections
|
177
|
+
if (file.endsWith('.yaml') || file.endsWith('.yml')) {
|
178
|
+
const collection = loadYamlCollection(filePath);
|
179
|
+
if (collection) {
|
180
|
+
config.collections.push(collection);
|
181
|
+
}
|
182
|
+
continue;
|
183
|
+
}
|
184
|
+
// Handle TypeScript collections
|
185
|
+
if (file.endsWith('.ts')) {
|
186
|
+
const fileUrl = pathToFileURL(filePath).href;
|
187
|
+
const collectionModule = (await import(fileUrl));
|
188
|
+
const collection = collectionModule.default?.default || collectionModule.default || collectionModule;
|
189
|
+
if (collection) {
|
190
|
+
// Ensure importDefs are properly loaded
|
191
|
+
if (collectionModule.importDefs || collection.importDefs) {
|
192
|
+
collection.importDefs = collectionModule.importDefs || collection.importDefs;
|
193
|
+
}
|
194
|
+
config.collections.push(collection);
|
195
|
+
}
|
196
|
+
}
|
197
|
+
}
|
198
|
+
}
|
199
|
+
finally {
|
200
|
+
unregister(); // Unregister tsx when done
|
201
|
+
}
|
202
|
+
}
|
203
|
+
return { config, actualConfigPath };
|
204
|
+
};
|
29
205
|
/**
|
30
206
|
* Loads the Appwrite configuration and all collection configurations from a specified directory.
|
31
|
-
*
|
207
|
+
* Supports both YAML and TypeScript config formats with backward compatibility.
|
208
|
+
* @param configDir The directory containing the config file and collections folder.
|
32
209
|
* @returns The loaded Appwrite configuration including collections.
|
33
210
|
*/
|
34
211
|
export const loadConfig = async (configDir) => {
|
35
|
-
|
36
|
-
|
212
|
+
let config = null;
|
213
|
+
let actualConfigPath = null;
|
214
|
+
// First try to find and load YAML config
|
215
|
+
const yamlConfigPath = findYamlConfig(configDir);
|
216
|
+
if (yamlConfigPath) {
|
217
|
+
console.log(`Loading YAML config from: ${yamlConfigPath}`);
|
218
|
+
config = await loadYamlConfig(yamlConfigPath);
|
219
|
+
actualConfigPath = yamlConfigPath;
|
220
|
+
}
|
221
|
+
// Fall back to TypeScript config if YAML not found or failed to load
|
222
|
+
if (!config) {
|
37
223
|
const configPath = path.join(configDir, "appwriteConfig.ts");
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
224
|
+
// Only try to load TypeScript config if the file exists
|
225
|
+
if (fs.existsSync(configPath)) {
|
226
|
+
const unregister = register(); // Register tsx enhancement
|
227
|
+
try {
|
228
|
+
console.log(`Loading TypeScript config from: ${configPath}`);
|
229
|
+
const configUrl = pathToFileURL(configPath).href;
|
230
|
+
const configModule = (await import(configUrl));
|
231
|
+
config = configModule.default?.default || configModule.default || configModule;
|
232
|
+
if (!config) {
|
233
|
+
throw new Error("Failed to load config");
|
234
|
+
}
|
235
|
+
actualConfigPath = configPath;
|
236
|
+
}
|
237
|
+
finally {
|
238
|
+
unregister(); // Unregister tsx when done
|
51
239
|
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
240
|
+
}
|
241
|
+
}
|
242
|
+
if (!config) {
|
243
|
+
throw new Error("No valid configuration found");
|
244
|
+
}
|
245
|
+
// Determine collections directory based on actual config file location
|
246
|
+
let collectionsDir;
|
247
|
+
if (actualConfigPath) {
|
248
|
+
const configFileDir = path.dirname(actualConfigPath);
|
249
|
+
// Check if config is in .appwrite directory
|
250
|
+
if (configFileDir.endsWith('.appwrite')) {
|
251
|
+
collectionsDir = path.join(configFileDir, "collections");
|
252
|
+
}
|
253
|
+
else {
|
254
|
+
// Config is in root or other directory
|
255
|
+
collectionsDir = path.join(configFileDir, "collections");
|
256
|
+
}
|
257
|
+
}
|
258
|
+
else {
|
259
|
+
// Fallback to original behavior if no actual config path found
|
260
|
+
collectionsDir = path.join(configDir, "collections");
|
261
|
+
}
|
262
|
+
// Load collections if they exist
|
263
|
+
if (fs.existsSync(collectionsDir)) {
|
264
|
+
const unregister = register(); // Register tsx for collections
|
265
|
+
try {
|
266
|
+
const collectionFiles = fs.readdirSync(collectionsDir);
|
267
|
+
config.collections = [];
|
268
|
+
for (const file of collectionFiles) {
|
269
|
+
if (file === "index.ts") {
|
270
|
+
continue;
|
271
|
+
}
|
272
|
+
const filePath = path.join(collectionsDir, file);
|
273
|
+
// Handle YAML collections
|
274
|
+
if (file.endsWith('.yaml') || file.endsWith('.yml')) {
|
275
|
+
const collection = loadYamlCollection(filePath);
|
276
|
+
if (collection) {
|
277
|
+
config.collections.push(collection);
|
278
|
+
}
|
279
|
+
continue;
|
280
|
+
}
|
281
|
+
// Handle TypeScript collections
|
282
|
+
if (file.endsWith('.ts')) {
|
283
|
+
const fileUrl = pathToFileURL(filePath).href;
|
284
|
+
const collectionModule = (await import(fileUrl));
|
285
|
+
const collection = collectionModule.default?.default || collectionModule.default || collectionModule;
|
286
|
+
if (collection) {
|
287
|
+
// Ensure importDefs are properly loaded
|
288
|
+
if (collectionModule.importDefs || collection.importDefs) {
|
289
|
+
collection.importDefs = collectionModule.importDefs || collection.importDefs;
|
290
|
+
}
|
291
|
+
config.collections.push(collection);
|
292
|
+
}
|
60
293
|
}
|
61
|
-
config.collections.push(collection);
|
62
294
|
}
|
63
295
|
}
|
64
|
-
|
296
|
+
finally {
|
297
|
+
unregister(); // Unregister tsx when done
|
298
|
+
}
|
65
299
|
}
|
66
|
-
|
67
|
-
|
300
|
+
else {
|
301
|
+
config.collections = config.collections || [];
|
68
302
|
}
|
303
|
+
return config;
|
69
304
|
};
|
70
|
-
export const findFunctionsDir = (dir) => {
|
71
|
-
|
305
|
+
export const findFunctionsDir = (dir, depth = 0) => {
|
306
|
+
// Limit search depth to prevent infinite recursion
|
307
|
+
if (depth > 5) {
|
72
308
|
return null;
|
73
309
|
}
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
}
|
79
|
-
|
80
|
-
|
310
|
+
if (shouldIgnoreDirectory(path.basename(dir))) {
|
311
|
+
return null;
|
312
|
+
}
|
313
|
+
try {
|
314
|
+
const files = fs.readdirSync(dir, { withFileTypes: true });
|
315
|
+
for (const entry of files) {
|
316
|
+
if (!entry.isDirectory() || shouldIgnoreDirectory(entry.name)) {
|
317
|
+
continue;
|
318
|
+
}
|
319
|
+
if (entry.name === "functions") {
|
320
|
+
return path.join(dir, entry.name);
|
321
|
+
}
|
322
|
+
const result = findFunctionsDir(path.join(dir, entry.name), depth + 1);
|
323
|
+
if (result)
|
324
|
+
return result;
|
81
325
|
}
|
82
|
-
|
83
|
-
|
84
|
-
|
326
|
+
}
|
327
|
+
catch (error) {
|
328
|
+
// Ignore directory access errors
|
85
329
|
}
|
86
330
|
return null;
|
87
331
|
};
|
332
|
+
// YAML Collection Schema
|
333
|
+
const YamlCollectionSchema = z.object({
|
334
|
+
name: z.string(),
|
335
|
+
id: z.string().optional(),
|
336
|
+
documentSecurity: z.boolean().default(false),
|
337
|
+
enabled: z.boolean().default(true),
|
338
|
+
permissions: z.array(z.object({
|
339
|
+
permission: z.string(),
|
340
|
+
target: z.string()
|
341
|
+
})).optional().default([]),
|
342
|
+
attributes: z.array(z.object({
|
343
|
+
key: z.string(),
|
344
|
+
type: z.string(),
|
345
|
+
size: z.number().optional(),
|
346
|
+
required: z.boolean().default(false),
|
347
|
+
array: z.boolean().optional(),
|
348
|
+
default: z.any().optional(),
|
349
|
+
description: z.string().optional(),
|
350
|
+
min: z.number().optional(),
|
351
|
+
max: z.number().optional(),
|
352
|
+
elements: z.array(z.string()).optional(),
|
353
|
+
relatedCollection: z.string().optional(),
|
354
|
+
relationType: z.string().optional(),
|
355
|
+
twoWay: z.boolean().optional(),
|
356
|
+
twoWayKey: z.string().optional(),
|
357
|
+
onDelete: z.string().optional(),
|
358
|
+
side: z.string().optional()
|
359
|
+
})).optional().default([]),
|
360
|
+
indexes: z.array(z.object({
|
361
|
+
key: z.string(),
|
362
|
+
type: z.string(),
|
363
|
+
attributes: z.array(z.string()),
|
364
|
+
orders: z.array(z.string()).optional()
|
365
|
+
})).optional().default([]),
|
366
|
+
importDefs: z.array(z.any()).optional().default([])
|
367
|
+
});
|
368
|
+
const loadYamlCollection = (filePath) => {
|
369
|
+
try {
|
370
|
+
const fileContent = fs.readFileSync(filePath, "utf8");
|
371
|
+
const yamlData = yaml.load(fileContent);
|
372
|
+
const parsedCollection = YamlCollectionSchema.parse(yamlData);
|
373
|
+
// Convert YAML collection to CollectionCreate format
|
374
|
+
const collection = {
|
375
|
+
name: parsedCollection.name,
|
376
|
+
$id: parsedCollection.id || parsedCollection.name.toLowerCase().replace(/\s+/g, '_'),
|
377
|
+
documentSecurity: parsedCollection.documentSecurity,
|
378
|
+
enabled: parsedCollection.enabled,
|
379
|
+
$permissions: parsedCollection.permissions.map(p => ({
|
380
|
+
permission: p.permission,
|
381
|
+
target: p.target
|
382
|
+
})),
|
383
|
+
attributes: parsedCollection.attributes.map(attr => ({
|
384
|
+
key: attr.key,
|
385
|
+
type: attr.type,
|
386
|
+
size: attr.size,
|
387
|
+
required: attr.required,
|
388
|
+
array: attr.array,
|
389
|
+
xdefault: attr.default,
|
390
|
+
description: attr.description,
|
391
|
+
min: attr.min,
|
392
|
+
max: attr.max,
|
393
|
+
elements: attr.elements,
|
394
|
+
relatedCollection: attr.relatedCollection,
|
395
|
+
relationType: attr.relationType,
|
396
|
+
twoWay: attr.twoWay,
|
397
|
+
twoWayKey: attr.twoWayKey,
|
398
|
+
onDelete: attr.onDelete,
|
399
|
+
side: attr.side
|
400
|
+
})),
|
401
|
+
indexes: parsedCollection.indexes.map(idx => ({
|
402
|
+
key: idx.key,
|
403
|
+
type: idx.type,
|
404
|
+
attributes: idx.attributes,
|
405
|
+
orders: idx.orders
|
406
|
+
})),
|
407
|
+
importDefs: parsedCollection.importDefs
|
408
|
+
};
|
409
|
+
return collection;
|
410
|
+
}
|
411
|
+
catch (error) {
|
412
|
+
console.error(`Error loading YAML collection from ${filePath}:`, error);
|
413
|
+
return null;
|
414
|
+
}
|
415
|
+
};
|
@@ -337,7 +337,8 @@ export class SchemaGenerator {
|
|
337
337
|
baseSchemaCode += ".nullish()";
|
338
338
|
}
|
339
339
|
break;
|
340
|
-
case "
|
340
|
+
case "double":
|
341
|
+
case "float": // Backward compatibility
|
341
342
|
baseSchemaCode = "z.number()";
|
342
343
|
if (finalAttribute.min !== undefined) {
|
343
344
|
baseSchemaCode += `.min(${finalAttribute.min}, "Minimum value of ${finalAttribute.min} not met")`;
|
@@ -1,3 +1,4 @@
|
|
1
1
|
export declare const customDefinitionsFile = "import type { ConverterFunctions, ValidationRules, AfterImportActions } from \"appwrite-utils\";\n\nexport const customConverterFunctions: ConverterFunctions = {\n // Add your custom converter functions here\n}\nexport const customValidationRules: ValidationRules = {\n // Add your custom validation rules here\n}\nexport const customAfterImportActions: AfterImportActions = {\n // Add your custom after import actions here\n}";
|
2
2
|
export declare const createEmptyCollection: (collectionName: string) => void;
|
3
|
-
export declare const
|
3
|
+
export declare const generateYamlConfig: (currentDir?: string, useAppwriteDir?: boolean) => string;
|
4
|
+
export declare const setupDirsFiles: (example?: boolean, currentDir?: string, useYaml?: boolean) => Promise<void>;
|