-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
68 lines (57 loc) · 2.07 KB
/
main.ts
File metadata and controls
68 lines (57 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Command } from "@cliffy/command";
import { createTopuServices } from "./module.ts";
import { NodeFileSystem } from "langium/node";
import { AstMetaData, Reduction, URI } from "langium";
import { resolve } from "node:path";
import * as AST from "./generated/ast.js";
import { extractNamespace } from "./generator.ts";
const { shared } = createTopuServices({ ...NodeFileSystem });
const cmd = new Command()
.name("📦 topu")
.description("atproto lexicon dsl")
.version("0.1.0")
.action(function () {
this.showHelp();
});
// COMPILE
cmd
.command(
"compile <source:file> [outdir:file]",
"compile a .topu file to lexicon json",
)
.action(async (_, source, outdir) => {
const dir = resolve(outdir ?? "./lexicons");
await Deno.mkdir(dir, { recursive: true });
const document = await getAST(source);
const model = document.parseResult.value as AST.Model;
const lexicons = extractNamespace(model.namespaces[0]);
for (const lexicon of lexicons) {
const path = resolve(dir, `${lexicon.id}.json`);
await Deno.writeTextFile(path, JSON.stringify(lexicon, null, 2));
console.log(`wrote ${path}`);
}
});
// CHECK
cmd.command("check <source:file>", "check a .topu file for errors")
.action(async (_, source) => {
await getAST(source);
});
await cmd.parse();
async function getAST(path: string) {
const uri = URI.file(resolve(path));
const document = await shared.workspace.LangiumDocuments
.getOrCreateDocument(
uri,
);
await shared.workspace.DocumentBuilder.build([document]);
const parseErrors = document.parseResult.parserErrors;
const linkErrors = (document.diagnostics ?? []).filter(
(d) => d.code === "linking-error",
);
if (parseErrors.length > 0 || linkErrors.length > 0) {
if (parseErrors.length) console.error("Parse errors:", parseErrors);
if (linkErrors.length) console.error("Link errors:", linkErrors);
Deno.exit(1);
}
return document;
}