Skip to content

Commit e48e7b4

Browse files
implement EJS
1 parent c6f4077 commit e48e7b4

File tree

10 files changed

+388
-163
lines changed

10 files changed

+388
-163
lines changed

index.js

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
#!/usr/bin/env node
22

33
import yargs from "yargs";
4-
import path from "path";
5-
import {Client, Database} from "node-appwrite";
6-
import {hideBin} from "yargs/helpers";
7-
import {mkdir, readFile, writeFile} from "fs/promises";
8-
import {existsSync} from "fs";
9-
import {exit} from "process";
10-
import {Typescript} from "./languages/typescript.js"
11-
import {Kotlin} from "./languages/kotlin.js"
12-
import {Php} from "./languages/php.js";
4+
import path, { dirname } from "path";
5+
import { Client, Database } from "node-appwrite";
6+
import { hideBin } from "yargs/helpers";
7+
import { mkdir, readFile, writeFile } from "fs/promises";
8+
import { existsSync } from "fs";
9+
import { exit } from "process";
10+
import { Typescript } from "./languages/typescript.js";
11+
import { Kotlin } from "./languages/kotlin.js";
12+
import { Php } from "./languages/php.js";
13+
import { renderFile } from "ejs";
14+
import { fileURLToPath } from "url";
1315

1416
const client = new Client();
1517
const database = new Database(client);
1618

1719
const languageClasses = {
18-
"typescript": Typescript,
19-
"kotlin": Kotlin,
20-
"php": Php
21-
}
20+
typescript: Typescript,
21+
kotlin: Kotlin,
22+
php: Php,
23+
};
2224

2325
/**
2426
* Load configuration for Appwrite.
2527
* @param {string} file
2628
* @returns {Promise<object>}
2729
*/
28-
const loadConfiguration = async file => {
29-
30+
const loadConfiguration = async (file) => {
3031
try {
3132
if (!existsSync(file)) {
32-
onError("Configuration not found.")
33+
onError("Configuration not found.");
3334
}
3435

35-
const data = await readFile(file, 'utf8');
36+
const data = await readFile(file, "utf8");
3637
const config = JSON.parse(data);
3738

3839
client
@@ -42,11 +43,11 @@ const loadConfiguration = async file => {
4243

4344
return config;
4445
} catch (err) {
45-
onError(err.message)
46+
onError(err.message);
4647
}
47-
}
48+
};
4849

49-
const generate = async ({output, config, language}) => {
50+
const generate = async ({ output, config, language }) => {
5051
await loadConfiguration(config);
5152

5253
const selectedClass = languageClasses[language];
@@ -57,73 +58,82 @@ const generate = async ({output, config, language}) => {
5758

5859
/** @type {Array<object>} collections*/
5960
const collections = (await database.listCollections()).collections;
60-
const types = buildCollectionTypes(collections)
61+
const types = buildCollectionTypes(collections);
6162

6263
if (!existsSync(output)) {
63-
await mkdir(output, {recursive: true});
64+
await mkdir(output, { recursive: true });
6465
}
6566

6667
for (const type of types) {
67-
const content = lang.generate(type)
68-
6968
try {
70-
const destination = path.join(output, `./${type.name}${lang.getFileExtension()}`);
69+
const content = await renderFile(path.join(fileURLToPath(dirname(import.meta.url)), `./templates/${language}.ejs`), {
70+
...type,
71+
getType: selectedClass.getType,
72+
getTypeFormatted: selectedClass.getTypeFormatted,
73+
});
74+
75+
const destination = path.join(
76+
output,
77+
`./${type.name}${lang.getFileExtension()}`
78+
);
7179
await writeFile(destination, content);
72-
console.log(`Generated ${destination}`)
80+
console.log(`Generated ${destination}`);
7381
} catch (err) {
74-
console.error(err.message)
82+
console.error(err.message);
7583
}
7684
}
77-
}
85+
};
7886

7987
const buildCollectionTypes = (collections) => {
8088
let types = [];
81-
collections.forEach(collection => {
89+
collections.forEach((collection) => {
8290
types.push({
8391
name: collection.name,
84-
attributes: collection.rules.map(rule => {
85-
return {
86-
name: rule.key,
87-
type: rule.type,
88-
array: rule.array,
89-
required: rule.required
90-
}
91-
}).sort((a, b) => {
92-
return (a.required === b.required)
93-
? 0
94-
: a.required
95-
? -1
96-
: 1;
97-
})
98-
})
99-
})
92+
attributes: collection.rules
93+
.map((rule) => {
94+
return {
95+
name: rule.key,
96+
type: rule.type,
97+
array: rule.array,
98+
required: rule.required,
99+
};
100+
})
101+
.sort((a, b) => {
102+
return a.required === b.required ? 0 : a.required ? -1 : 1;
103+
}),
104+
});
105+
});
100106
return types;
101-
}
107+
};
102108

103109
const onError = function catchError(error) {
104-
console.error(error)
110+
console.error(error);
105111
exit(1);
106-
}
112+
};
107113

108114
yargs(hideBin(process.argv))
109-
.command("generate", "Fetches Collection and creates Typescript Definitions", () => { }, generate)
115+
.command(
116+
"generate",
117+
"Fetches Collection and creates Typescript Definitions",
118+
() => { },
119+
generate
120+
)
110121
.option("output", {
111122
alias: "o",
112123
type: "string",
113124
description: "Output",
114-
default: "./"
125+
default: "./",
115126
})
116127
.option("config", {
117128
alias: "c",
118129
type: "string",
119130
description: "Configuration file.",
120-
required: true
131+
required: true,
121132
})
122133
.option("language", {
123134
alias: "l",
124135
type: "string",
125136
description: "Output language",
126-
default: "typescript"
137+
default: "typescript",
127138
})
128-
.demandCommand(1)
129-
.argv;
139+
.demandCommand(1).argv;

languages/kotlin.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
import camelCase from "camelcase";
2-
import {Language} from "./language.js"
3-
import {AttributeTypes} from "../attributes.js";
1+
import { Language } from "./language.js";
2+
import { AttributeTypes } from "../attributes.js";
3+
import camelcase from "camelcase";
44

55
export class Kotlin extends Language {
6-
7-
getType(type) {
6+
static getType(type) {
87
switch (type) {
98
case AttributeTypes.TEXT:
109
case AttributeTypes.EMAIL:
1110
case AttributeTypes.URL:
1211
case AttributeTypes.IP:
1312
case AttributeTypes.WILDCARD:
1413
case AttributeTypes.MARKDOWN:
15-
return "String"
14+
return "String";
1615
case AttributeTypes.NUMERIC:
17-
return "Number"
16+
return "Number";
1817
case AttributeTypes.BOOLEAN:
19-
return "Boolean"
18+
return "Boolean";
2019
}
2120
}
2221

22+
static getTypeFormatted(name) {
23+
return camelcase(name, {pascalCase: true});
24+
}
25+
2326
getTypeDefault(attribute) {
2427
if (!attribute.required) {
2528
return "null";
2629
}
2730
if (attribute.array) {
28-
return `listOf<${this.getType(attribute.type)}>()`;
31+
return `listOf<${Kotlin.getType(attribute.type)}>()`;
2932
}
3033
switch (attribute.type) {
3134
case AttributeTypes.TEXT:
@@ -43,18 +46,6 @@ export class Kotlin extends Language {
4346
}
4447

4548
getFileExtension() {
46-
return ".kt"
47-
}
48-
49-
getTypeOpenLine(name) {
50-
return `data class ${camelCase(name, {pascalCase: true})} (\n`
51-
}
52-
53-
getTypePropertyLine(attribute) {
54-
return `\t${attribute.name}: ${attribute.array ? "List<" : ""}${this.getType(attribute.type) ?? "Any"}${attribute.array ? ">" : ""}${attribute.required ? "" : "?"}${attribute.required ? "" : ` = ${this.getTypeDefault(attribute)}`},\n`;
55-
}
56-
57-
getTypeCloseLine() {
58-
return ")\n"
49+
return ".kt";
5950
}
60-
}
51+
}

languages/language.js

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ export class Language {
1212
* @param type
1313
* @return {string}
1414
*/
15-
getType(type) {
15+
static getType(type) {
16+
throw new TypeError("Stub.");
17+
}
18+
19+
/**
20+
* Return formatted Type Name.
21+
* @param {string} attribute
22+
* @returns {string}
23+
*/
24+
static getTypeFormatted(name) {
1625
throw new TypeError("Stub.");
1726
}
1827

@@ -33,44 +42,4 @@ export class Language {
3342
getFileExtension() {
3443
throw new TypeError("Stub.");
3544
}
36-
37-
/**
38-
* Get a string that defines the opening line for a new type with the given name in this language.
39-
*
40-
* @param name
41-
*/
42-
getTypeOpenLine(name) {
43-
throw new TypeError("Stub.");
44-
}
45-
46-
/**
47-
* Get a string that defines a property line for the given attribute for this language.
48-
*
49-
* @param attribute
50-
*/
51-
getTypePropertyLine(attribute) {
52-
throw new TypeError("Stub.");
53-
}
54-
55-
/**
56-
* Get a string that closes a type definition for this language.
57-
*
58-
*/
59-
getTypeCloseLine() {
60-
throw new TypeError("Stub.");
61-
}
62-
63-
/**
64-
* Generate (as a string) a type definition with the given type for this language.
65-
*
66-
* @param type
67-
* @returns {string}
68-
*/
69-
generate(type) {
70-
return this.getTypeOpenLine(type.name)
71-
+ (type.attributes.map(attr => {
72-
return this.getTypePropertyLine(attr)
73-
}).join(""))
74-
+ this.getTypeCloseLine();
75-
}
7645
}

languages/php.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import camelCase from "camelcase";
2-
import {Language} from "./language.js"
3-
import {AttributeTypes} from "../attributes.js";
1+
import { Language } from "./language.js";
2+
import { AttributeTypes } from "../attributes.js";
3+
import camelcase from "camelcase";
44

55
export class Php extends Language {
6-
7-
getType(type) {
6+
static getType(type) {
87
switch (type) {
98
case AttributeTypes.TEXT:
109
case AttributeTypes.EMAIL:
@@ -14,12 +13,16 @@ export class Php extends Language {
1413
case AttributeTypes.MARKDOWN:
1514
return "string";
1615
case AttributeTypes.NUMERIC:
17-
return "number"
16+
return "int";
1817
case AttributeTypes.BOOLEAN:
19-
return "bool"
18+
return "bool";
2019
}
2120
}
2221

22+
static getTypeFormatted(name) {
23+
return camelcase(name, {pascalCase: true});
24+
}
25+
2326
getTypeDefault(attribute) {
2427
if (!attribute.required) {
2528
return "null";
@@ -43,18 +46,6 @@ export class Php extends Language {
4346
}
4447

4548
getFileExtension() {
46-
return ".php"
47-
}
48-
49-
getTypeOpenLine(name) {
50-
return `<?php\nclass ${camelCase(name, {pascalCase: true})} \n{\n\tpublic function __construct(\n`
51-
}
52-
53-
getTypePropertyLine(attribute) {
54-
return `\t\tpublic ${attribute.required ? "" : "?"}${attribute.array ? "array" : this.getType(attribute.type) ?? ""} $${attribute.name}${attribute.required ? "" : ` = ${this.getTypeDefault(attribute)}`},\n`;
55-
}
56-
57-
getTypeCloseLine() {
58-
return "\t)\n}"
49+
return ".php";
5950
}
60-
}
51+
}

0 commit comments

Comments
 (0)