-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev): typegraph explorer (#859)
- Add a web version of tree-view which is more interactive - Enable typegraph serialization without `metatype.yml` config file ![image](https://github.com/user-attachments/assets/81771c07-1f2a-493a-81df-969c8182f9bf)
- Loading branch information
Showing
9 changed files
with
499 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/bin/env -S ghjk deno run -A | ||
|
||
// Copyright Metatype OÜ, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
|
||
/** | ||
* Usage: | ||
* deno run -A dev/tree-view.ts [<options>] <file.py> | ||
*/ | ||
|
||
import { TypeGraphDS } from "../src/typegate/src/typegraph/mod.ts"; | ||
import { projectDir } from "./utils.ts"; | ||
import { join, parseArgs } from "./deps.ts"; | ||
|
||
const dirname = import.meta.dirname ?? new URL(".", import.meta.url).pathname; | ||
const indexHtml = join(dirname, "tree-view/index.html"); | ||
|
||
const args = parseArgs(Deno.args, { | ||
string: ["port"], | ||
}); | ||
|
||
const argsPort = parseInt(args.port ?? ""); | ||
const envPort = parseInt(Deno.env.get("PORT") ?? ""); | ||
const port = isNaN(argsPort) ? (isNaN(envPort) ? 0 : envPort) : argsPort; | ||
|
||
const files = args._ as string[]; | ||
if (files.length === 0) { | ||
throw new Error("Path to typegraph definition module is required."); | ||
} | ||
const cmdBase = [ | ||
"cargo", | ||
"run", | ||
"--manifest-path", | ||
`${projectDir}/Cargo.toml`, | ||
"-p", | ||
"meta-cli", | ||
"--", | ||
"serialize", | ||
"-f", | ||
]; | ||
const tgs: TypeGraphDS[] = []; | ||
for (const file of files) { | ||
const cmd = [...cmdBase, file]; | ||
const { stdout, code } = await new Deno.Command(cmd[0], { | ||
args: cmd.slice(1), | ||
stdout: "piped", | ||
stderr: "inherit", | ||
}).output(); | ||
if (code !== 0) { | ||
console.log( | ||
`[error] command ${ | ||
cmd.map((c) => JSON.stringify(c)).join(" ") | ||
} failed with code ${code}`, | ||
); | ||
continue; | ||
} | ||
tgs.push(...JSON.parse(new TextDecoder().decode(stdout))); | ||
} | ||
|
||
if (tgs.length === 0) { | ||
console.log("[error] no typegraph found"); | ||
Deno.exit(1); | ||
} | ||
|
||
Deno.serve({ | ||
port, | ||
onListen({ port, hostname }) { | ||
console.log( | ||
`server running at http://${hostname ?? "localhost"}:${port}`, | ||
); | ||
}, | ||
}, async (req) => { | ||
const url = new URL(req.url); | ||
const pathname = url.pathname; | ||
console.log(`[info] method=${req.method} url=${req.url}`); | ||
|
||
if (req.method !== "GET") { | ||
console.log(`[error] method '${req.method}' not allowed`); | ||
return new Response("Method not allowed", { status: 405 }); | ||
} | ||
if (pathname === "/") { | ||
return new Response(await Deno.readTextFile(indexHtml), { | ||
headers: { "content-type": "text/html" }, | ||
}); | ||
} | ||
// TODO typegraph list and typegraph by name | ||
if (pathname === "/tg.json") { | ||
return new Response(JSON.stringify(tgs), { | ||
headers: { "content-type": "application/json" }, | ||
}); | ||
} | ||
console.log(`[error] path '${pathname}' not found`); | ||
return new Response("Not found", { status: 404 }); | ||
}); |
Oops, something went wrong.