Skip to content

Commit 09e87e6

Browse files
authored
Merge pull request appwrite#1100 from appwrite/pla-3171
chore: add type generation improvements for ts
2 parents aaf0804 + 1ccbf52 commit 09e87e6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
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(`Invalid output path: ${rawOutputPath}. 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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TypeScript extends LanguageMeta {
4040
if (attribute.array) {
4141
type += "[]";
4242
}
43-
if (!attribute.required) {
43+
if (!attribute.required && attribute.default === null) {
4444
type += " | null";
4545
}
4646
return type;
@@ -64,15 +64,23 @@ class TypeScript extends LanguageMeta {
6464
return "appwrite";
6565
}
6666

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

7077
<% for (const collection of collections) { -%>
7178
<% for (const attribute of collection.attributes) { -%>
7279
<% if (attribute.format === 'enum') { -%>
7380
export enum <%- toPascalCase(attribute.key) %> {
74-
<% for (const [index, element] of Object.entries(attribute.elements)) { -%>
75-
<%- toUpperSnakeCase(element) %> = "<%- element %>",
81+
<% const entries = Object.entries(attribute.elements); -%>
82+
<% for (let i = 0; i < entries.length; i++) { -%>
83+
<%- toUpperSnakeCase(entries[i][1]) %> = "<%- entries[i][1] %>"<% if (i !== entries.length - 1) { %>,<% } %>
7684
<% } -%>
7785
}
7886

0 commit comments

Comments
 (0)