1#!/usr/bin/env bash
2#
3# ./examples/ts-type-to-grammar.sh "{a:string,b:string,c?:string}"
4# python examples/json_schema_to_grammar.py https://json.schemastore.org/tsconfig.json
5#
6set -euo pipefail
7
8readonly type="$1"
9
10# Create a temporary directory
11TMPDIR=""
12trap 'rm -fR "$TMPDIR"' EXIT
13TMPDIR=$(mktemp -d)
14
15DTS_FILE="$TMPDIR/type.d.ts"
16SCHEMA_FILE="$TMPDIR/schema.json"
17
18echo "export type MyType = $type" > "$DTS_FILE"
19
20# This is a fork of typescript-json-schema, actively maintained as of March 2024:
21# https://github.com/vega/ts-json-schema-generator
22npx ts-json-schema-generator --unstable --no-top-ref --path "$DTS_FILE" --type MyType -e none > "$SCHEMA_FILE"
23
24# Alternative, not actively maintained as of March 2024:
25# https://github.com/YousefED/typescript-json-schema
26# npx typescript-json-schema --defaultProps --required "$DTS_FILE" MyType | tee "$SCHEMA_FILE" >&2
27
28./examples/json_schema_to_grammar.py "$SCHEMA_FILE"