forked from softwaretechnik-berlin/dbml-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (52 loc) · 1.31 KB
/
index.ts
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
#!/usr/bin/env node
import fs from "fs";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { run } from "./api";
import { Format } from "./renderer";
const args = yargs(hideBin(process.argv))
.scriptName("dbml-renderer")
.usage("Usage: $0 [options]")
.example("$0 -i schema.dbml", "Render the given file and output to stdout")
.alias("h", "help")
.option("input", {
demandOption: true,
alias: "i",
type: "string",
default: "-",
description: "DBML file",
coerce: (arg) => {
if (!fs.existsSync(arg) && !(arg === "-")) {
throw new Error(`Could not find file '${arg}'`);
}
return arg === "-"
? fs.readFileSync(0, "utf-8")
: fs.readFileSync(arg, "utf-8");
},
})
.option("format", {
alias: "f",
type: "string",
choices: ["dot", "svg"],
default: "svg",
description: "Output format",
coerce: (arg) => arg as Format,
})
.option("output", {
alias: "o",
type: "string",
default: "-",
description: "Output file",
coerce: (arg) => {
return arg === "-"
? console.log
: (content: string) => fs.writeFileSync(arg, content);
},
})
.parseSync();
try {
args.output(run(args.input, args.format));
} catch (e) {
console.error((e as any).message || e);
process.exit(1);
}