-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·139 lines (123 loc) · 3.73 KB
/
index.js
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
#!/usr/bin/env node
import yargs from "yargs";
import path, { dirname } from "path";
import { Client, Database } from "node-appwrite";
import { hideBin } from "yargs/helpers";
import { mkdir, readFile, writeFile } from "fs/promises";
import { existsSync } from "fs";
import { exit } from "process";
import { Typescript } from "./languages/typescript.js";
import { Kotlin } from "./languages/kotlin.js";
import { Php } from "./languages/php.js";
import { renderFile } from "ejs";
import { fileURLToPath } from "url";
const client = new Client();
const database = new Database(client);
const languageClasses = {
typescript: Typescript,
kotlin: Kotlin,
php: Php,
};
/**
* Load configuration for Appwrite.
* @param {string} file
* @returns {Promise<object>}
*/
const loadConfiguration = async (file) => {
try {
if (!existsSync(file)) {
onError("Configuration not found.");
}
const data = await readFile(file, "utf8");
const config = JSON.parse(data);
client
.setEndpoint(config.endpoint)
.setProject(config.project)
.setKey(config.key);
return config;
} catch (err) {
onError(err.message);
}
};
const generate = async ({ output, config, language }) => {
await loadConfiguration(config);
const selectedClass = languageClasses[language];
if (!selectedClass) {
onError(`Language must be one of [${Object.keys(languageClasses)}]`);
}
const lang = new selectedClass();
/** @type {Array<object>} collections*/
const collections = (await database.listCollections()).collections;
const types = buildCollectionTypes(collections);
if (!existsSync(output)) {
await mkdir(output, { recursive: true });
}
for (const type of types) {
try {
const content = await renderFile(path.join(fileURLToPath(dirname(import.meta.url)), `./templates/${language}.ejs`), {
...type,
getType: selectedClass.getType,
getTypeFormatted: selectedClass.getTypeFormatted,
});
const destination = path.join(
output,
`./${type.name}${lang.getFileExtension()}`
);
await writeFile(destination, content);
console.log(`Generated ${destination}`);
} catch (err) {
console.error(err.message);
}
}
};
const buildCollectionTypes = (collections) => {
let types = [];
collections.forEach((collection) => {
types.push({
name: collection.name,
attributes: collection.rules
.map((rule) => {
return {
name: rule.key,
type: rule.type,
array: rule.array,
required: rule.required,
};
})
.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,
})
.option("language", {
alias: "l",
type: "string",
description: "Output language",
default: "typescript",
})
.demandCommand(1).argv;