From 21ac451d8e8c4c1f8d821999141f1a488904138a Mon Sep 17 00:00:00 2001 From: ochafik Date: Sun, 3 Mar 2024 22:22:21 +0000 Subject: [PATCH] Create ts-type-to-grammar.sh --- examples/ts-type-to-grammar.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 examples/ts-type-to-grammar.sh diff --git a/examples/ts-type-to-grammar.sh b/examples/ts-type-to-grammar.sh new file mode 100755 index 000000000..33c1eec2c --- /dev/null +++ b/examples/ts-type-to-grammar.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# +# ./examples/ts-type-to-grammar.sh "{a:string,b:string,c?:string}" +# python examples/json-schema-to-grammar.py https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/tsconfig.json +# +set -euo pipefail + +readonly type="$1" + +# Create a temporary directory +TMPDIR="" +trap 'rm -fR "$TMPDIR"' EXIT +TMPDIR=$(mktemp -d) + +DTS_FILE="$TMPDIR/type.d.ts" +SCHEMA_FILE="$TMPDIR/schema.json" + +echo "export type MyType = $type" > "$DTS_FILE" + +# https://github.com/YousefED/typescript-json-schema +# npx typescript-json-schema --defaultProps --required "$DTS_FILE" MyType | tee "$SCHEMA_FILE" >&2 + +# https://github.com/vega/ts-json-schema-generator +npx ts-json-schema-generator --path "$DTS_FILE" --type MyType -e none | tee "$SCHEMA_FILE" >&2 + +./examples/json-schema-to-grammar.py "$SCHEMA_FILE" \ No newline at end of file