Skip to content

Commit

Permalink
✨💚 added automatic fetching of udr schema
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-runge committed Aug 20, 2023
1 parent 48a3b8a commit f848433
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ dist
.pnp.*

# generated files
/public
/public
/src/libraries/udr.d.ts
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"license": "MIT",
"private": false,
"scripts": {
"postinstall": "ts-node tools/fetchUDR.ts",
"app": "ts-node src/test/main.ts",
"app:watch": "nodemon src/test/main.ts",
"doc": "typedoc",
Expand All @@ -29,5 +30,8 @@
"ts-node": "^10.9.1",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
},
"dependencies": {
"json-schema-to-typescript": "^13.0.2"
}
}
7 changes: 7 additions & 0 deletions src/libraries/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function Try<T>(fn: () => T, err: (e: Error) => any): T {
try {
return fn();
} catch (e) {
return err(e as Error);
}
}
1 change: 1 addition & 0 deletions src/vision.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./libraries/udr";
export { IProps } from "./interfaces/IProps";
export { handleBars } from "./libraries/handleBars";
export { MyClass } from "./modules/MyClass";
43 changes: 43 additions & 0 deletions tools/fetchUDR.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { promises as fs } from "fs";
import { JSONSchema, compile } from "json-schema-to-typescript";
import { Try } from "../src/libraries/helpers";
import { format } from "prettier";

async function main(): Promise<void> {
let udrJsonSchema = await fetch(
"https://oshifty.gitlab.io/e173/udr-document.json",
).then((res) => res.text());

const schema = Try<JSONSchema>(
() => JSON.parse(udrJsonSchema),
() => {
throw new TypeError(`Error parsing JSON`);
},
);

let typeDefinitions = await compile(schema, "udr");

let currentTimestamp = new Date().toUTCString();

let dtsFile = `/**
* E1.73 (Uniform Device Representation)
*
* This file is generated by \`tools/fetchUDR.ts\`
* @see https://oshifty.gitlab.io/e173/udr-document.json
* @updated ${currentTimestamp}
*/
export namespace UDR {
${typeDefinitions}
}
`;

let options = await fs
.readFile(".prettierrc", "utf8")
.then((res) => JSON.parse(res));
options.parser = "typescript";
dtsFile = await format(dtsFile, options);

await fs.writeFile("src/libraries/udr.d.ts", dtsFile);
}

main();
Loading

0 comments on commit f848433

Please sign in to comment.