-
Notifications
You must be signed in to change notification settings - Fork 8
/
typescript-to-json-schema.ts
83 lines (76 loc) · 2.8 KB
/
typescript-to-json-schema.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as path from "path";
import * as ts from "typescript";
import * as commander from "commander";
import * as stringify from "json-stable-stringify";
import { DEFAULT_CONFIG, ProgramConfig } from "./factory/config";
import { createGenerator } from "./factory/generator";
import { BaseError } from "./src/Error/BaseError";
import { UnknownNodeError } from "./src/Error/UnknownNodeError";
import { DiagnosticError } from "./src/Error/DiagnosticError";
const args: any = commander
.option("-p, --path <path>", "Typescript path")
.option("-t, --type <name>", "Type name")
.option(
"-e, --expose <expose>",
"Type exposing",
/^(all|none|export)$/,
"export",
)
.option(
"-r, --topRef",
"Create a top-level $ref definition",
(v: any) => v === "true" || v === "yes" || v === "1",
true,
)
.option(
"-j, --jsDoc <extended>",
"Read JsDoc annotations",
/^(extended|none|basic)$/,
"extended",
)
.option(
"-s, --sortProps",
"Sort properties for stable output",
(v: any) => v === "true" || v === "yes" || v === "1",
true,
)
.parse(process.argv);
const programConfig: ProgramConfig = {
...DEFAULT_CONFIG,
...args,
};
try {
const schema = createGenerator(programConfig).createSchema(args.type);
process.stdout.write(programConfig.sortProps ?
stringify(schema, {space: 2}) :
JSON.stringify(schema, null, 2));
} catch (error) {
if (error instanceof BaseError) {
process.stderr.write(formatError(error));
process.exit(1);
} else {
throw error;
}
}
function getNodeLocation(node: ts.Node): [string, number, number] {
const sourceFile = node.getSourceFile();
const lineAndChar = ts.getLineAndCharacterOfPosition(sourceFile, node.getStart(sourceFile));
return [sourceFile.fileName, lineAndChar.line + 1, lineAndChar.character];
}
function formatError(error: BaseError): string {
if (error instanceof DiagnosticError) {
const rootDir = process.cwd().split(path.sep)[0] || "/";
return ts.formatDiagnostics(error.getDiagnostics(), {
getCanonicalFileName: (fileName: string) => fileName,
getCurrentDirectory: () => rootDir,
getNewLine: () => "\n",
});
} else if (error instanceof UnknownNodeError) {
const unknownNode = error.getReference() || error.getNode();
const nodeFullText = unknownNode.getFullText().trim().split("\n")[0].trim();
const [sourceFile, lineNumber, charPos] = getNodeLocation(unknownNode);
return `${error.name}: Unknown node "${nodeFullText}" (ts.SyntaxKind = ${error.getNode().kind}) ` +
`at ${sourceFile}(${lineNumber},${charPos})\n`;
}
return `${error.name}: ${error.message}\n`;
}