Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use autodoc inventory for automatic typegen #1

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(autotype): automatic version detection
  • Loading branch information
teaishealthy committed Jun 25, 2023
commit 4df09eefc7f49549321fafc8c6514f3be8ade6cd
67 changes: 45 additions & 22 deletions autotype/main.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ import {
EnumVariant,
} from "./types";
import { argv } from "process";
import { existsSync } from "fs";
import { createInterface } from "readline/promises";

const converters: Map<RegExp, (str: string) => string> = new Map([
[/^String$/, (_) => "string"],
@@ -20,6 +22,7 @@ const converters: Map<RegExp, (str: string) => string> = new Map([
[/^bool$/, (_) => "boolean"],
[/^TempFile$/, (_) => "unknown"],
[/^Box<.*>/, (str) => convertType(str.replace(/^Box<(.+)>$/, "$1"))],
[/^IpAddr$/, (_) => "string"],
]);

function switchCase(content: string, newCase: string | null): string {
@@ -55,7 +58,9 @@ function fieldToType(field: FieldInfo): string {
if (field.doc) {
doc = `/** ${convertDoc(field.doc)} */\n`;
}
return `${doc}${field.name}: ${convertType(field.field_type)}`;
return `${doc}${field.name}${field.ommitable ? "?" : ""}: ${convertType(
field.field_type
)}`;
}

function handleRoute(route: ItemInfo<RouteInfo>, routes: string[]) {
@@ -130,20 +135,21 @@ async function handleEnumVariant(
}
}
} else if (variant.type === VariantType.Struct) {
if (item.tag) {
if (variant.doc) {
doc = `/** ${convertDoc(variant.doc)} */`;
}
typeStr += ` ${item.tag}: "${switchCase(
variant.name,
item.rename_all
)}"\n`;
for (const field of variant.fields) {
if (field.flattened) {
bases.push(convertType(field.field_type));
} else {
typeStr += ` ${field.name}: ${convertType(field.field_type)}\n`;
}
if (!item.tag) {
return [];
}
if (variant.doc) {
doc = `/** ${convertDoc(variant.doc)} */`;
}
typeStr += ` ${item.tag}: "${switchCase(
variant.name,
item.rename_all
)}"\n`;
for (const field of variant.fields) {
if (field.flattened) {
bases.push(convertType(field.field_type));
} else {
typeStr += fieldToType(field) + "\n";
}
}
}
@@ -173,17 +179,34 @@ async function handleStruct(fh: FileHandle, info: ItemInfo<StructInfo>) {
}

async function main(inventoryIndex: string, output: string) {
let fh = await open(output, "w");
await fh.write("// This file was @generated by typegen\n");

let routes: string[] = [];

const inventory: string[] = await (
await fetch(`${inventoryIndex}/index.json`)
).json();
const inventory: {
version: string;
items: string[];
} = await (await fetch(`${inventoryIndex}/index.json`)).json();
console.log(inventory);

if (existsSync(`${output}/v${inventory["version"]}.ts`)) {
console.log(
`[INFO] v${inventory["version"]}.ts already exists, do you want to overwrite it?`
);
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await rl.question("Overwrite? (y/n) ");
rl.close();
if (answer !== "y") {
return;
}
}

let fh = await open(`${output}/v${inventory["version"]}.ts`, "w");
await fh.write("// This file was @generated by typegen\n");

await Promise.all(
inventory.map((url) => {
inventory.items.map((url) => {
buildTypes(fh, `${inventoryIndex}/${url}`, routes);
})
);