Skip to content

Commit 8d7225b

Browse files
committed
chore: add type generation improvements for ts
1 parent aaf0804 commit 8d7225b

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

templates/cli/lib/commands/types.js.twig

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,22 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
6666

6767
const meta = createLanguageMeta(language);
6868

69-
const outputDirectory = path.resolve(rawOutputDirectory);
69+
const rawOutputPath = rawOutputDirectory;
70+
const outputExt = path.extname(rawOutputPath);
71+
const isFileOutput = !!outputExt;
72+
let outputDirectory = rawOutputPath;
73+
let singleFileDestination = null;
74+
75+
if (isFileOutput) {
76+
if (meta.isSingleFile()) {
77+
// Use the file path directly for single file languages
78+
outputDirectory = path.dirname(rawOutputPath);
79+
singleFileDestination = rawOutputPath;
80+
} else {
81+
throw new Error("Output path must be a directory for languages that generate multiple files.");
82+
}
83+
}
84+
7085
if (!fs.existsSync(outputDirectory)) {
7186
log(`Directory: ${outputDirectory} does not exist, creating...`);
7287
fs.mkdirSync(outputDirectory, { recursive: true });
@@ -95,7 +110,7 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
95110
getType: meta.getType
96111
});
97112

98-
const destination = path.join(outputDirectory, meta.getFileName());
113+
const destination = singleFileDestination || path.join(outputDirectory, meta.getFileName());
99114

100115
fs.writeFileSync(destination, content);
101116
log(`Added types to ${destination}`);

templates/cli/lib/type-generation/languages/typescript.js.twig

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ class TypeScript extends LanguageMeta {
4040
if (attribute.array) {
4141
type += "[]";
4242
}
43-
if (!attribute.required) {
44-
type += " | null";
45-
}
4643
return type;
4744
}
4845

@@ -64,15 +61,23 @@ class TypeScript extends LanguageMeta {
6461
return "appwrite";
6562
}
6663

64+
getCurrentDirectory() {
65+
return process.cwd();
66+
}
67+
6768
getTemplate() {
68-
return `import { Models } from '${this._getAppwriteDependency()}';
69+
return `import { type Models } from '${this._getAppwriteDependency()}';
70+
71+
// This file is auto-generated by the Appwrite CLI.
72+
// You can regenerate it by running \`appwrite types -l ts ${this.getCurrentDirectory()}\`.
6973

7074
<% for (const collection of collections) { -%>
7175
<% for (const attribute of collection.attributes) { -%>
7276
<% if (attribute.format === 'enum') { -%>
7377
export enum <%- toPascalCase(attribute.key) %> {
74-
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
75-
<%- toUpperSnakeCase(element) %> = "<%- element %>",
78+
<% const entries = Object.entries(attribute.elements); -%>
79+
<% for (let i = 0; i < entries.length; i++) { -%>
80+
<%- toUpperSnakeCase(entries[i][1]) %> = "<%- entries[i][1] %>"<% if (i !== entries.length - 1) { %>,<% } %>
7681
<% } -%>
7782
}
7883

@@ -82,7 +87,7 @@ export enum <%- toPascalCase(attribute.key) %> {
8287
<% for (const collection of collections) { -%>
8388
export type <%- toPascalCase(collection.name) %> = Models.Document & {
8489
<% for (const attribute of collection.attributes) { -%>
85-
<%- toCamelCase(attribute.key) %>: <%- getType(attribute) %>;
90+
<%- toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>: <%- getType(attribute) %>;
8691
<% } -%>
8792
}
8893

0 commit comments

Comments
 (0)