Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·165 lines (144 loc) · 4.34 KB
/
Copy pathindex.js
File metadata and controls
executable file
·165 lines (144 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
import yargs from "yargs";
import path, { dirname } from "path";
import { Client, Databases } from "node-appwrite";
import { hideBin } from "yargs/helpers";
import { mkdir, readFile, appendFile, writeFile } from "fs/promises";
import { existsSync } from "fs";
import { exit } from "process";
import { Typescript } from "./typescript.js";
import { renderFile } from "ejs";
import { fileURLToPath } from "url";
/**
* Load configuration for Appwrite and initialize client and database.
* @param {string} file
* @returns {Promise<{config: object, client: Client, database: Databases}>}
*/
const loadConfigurationAndInitialize = async (file) => {
try {
if (!existsSync(file)) {
onError("Configuration not found.");
}
const data = await readFile(file, "utf8");
const config = JSON.parse(data);
const client = new Client();
client
.setEndpoint(config.endpoint)
.setProject(config.projectId)
.setKey(config.apiKey);
const database = new Databases(client);
return { config, client, database };
} catch (err) {
onError(err.message);
}
};
const generate = async ({ output, config, language }) => {
const { config: configVariables, database } =
await loadConfigurationAndInitialize(config);
const lang = new Typescript();
/** @type {Array<object>} collections*/
const collections = (
await database.listCollections(configVariables.databaseId)
).collections;
const types = buildCollectionTypes(collections);
if (!existsSync(output)) {
await mkdir(output, { recursive: true });
}
const destination = path.join(output, `appwrite${lang.getFileExtension()}`);
const imp = 'import type { Models } from "appwrite";\n\n';
await writeFile(destination, imp);
const enums = types.reduce((acc, type) => {
const enums = type.attributes.filter((attr) => attr.format === "enum");
return [...acc, ...enums];
}, []);
for (const element of enums) {
try {
const content = await renderFile(
path.join(
fileURLToPath(dirname(import.meta.url)),
`./templates/enum.ejs`
),
{ ...element, enumFormatted: Typescript.getEnumFormatted(element) }
);
await appendFile(destination, content + "\n\n");
console.log(`Generated enum ${destination} for ${element.name}`);
} catch (err) {
console.error("error", err.message);
}
}
for (const type of types) {
try {
const content = await renderFile(
path.join(
fileURLToPath(dirname(import.meta.url)),
`./templates/interface.ejs`
),
{
...type,
typeFormatted: Typescript.getTypeFormatted(type),
getRelationshipType: Typescript.getRelationshipType,
getType: Typescript.getType,
}
);
await appendFile(destination, content + "\n\n");
console.log(`Generated interface ${destination} for ${type.name}`);
} catch (err) {
console.error("error", err.message);
}
}
};
const buildCollectionTypes = (collections) => {
let types = [];
collections.forEach((collection) => {
types.push({
name: collection.name,
attributes: collection?.attributes
?.map((rule) => {
const isRelation = collections.find(
(item) => item.$id === rule.relatedCollection
);
return {
name: rule.key,
type: rule.type,
array: rule.array,
required: rule.required,
relationType: rule.relationType,
collection: collection.name,
elements: rule.elements,
format: rule.format,
relation: isRelation
? Typescript.getCamelCase(isRelation?.name)
: null,
};
})
.sort((a, b) => {
return a.required === b.required ? 0 : a.required ? -1 : 1;
}),
});
});
return types;
};
const onError = function catchError(error) {
console.error(error);
exit(1);
};
yargs(hideBin(process.argv))
.command(
"generate",
"Fetches Collection and creates Typescript Definitions",
() => {},
generate
)
.option("output", {
alias: "o",
type: "string",
description: "Output",
default: "./",
})
.option("config", {
alias: "c",
type: "string",
description: "Configuration file.",
required: true,
})
.demandCommand(1).argv;