ts-type-to-grammar.sh 920 B

12345678910111213141516171819202122232425262728
  1. #!/bin/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. #
  6. set -euo pipefail
  7. readonly type="$1"
  8. # Create a temporary directory
  9. TMPDIR=""
  10. trap 'rm -fR "$TMPDIR"' EXIT
  11. TMPDIR=$(mktemp -d)
  12. DTS_FILE="$TMPDIR/type.d.ts"
  13. SCHEMA_FILE="$TMPDIR/schema.json"
  14. echo "export type MyType = $type" > "$DTS_FILE"
  15. # This is a fork of typescript-json-schema, actively maintained as of March 2024:
  16. # https://github.com/vega/ts-json-schema-generator
  17. npx ts-json-schema-generator --unstable --no-top-ref --path "$DTS_FILE" --type MyType -e none > "$SCHEMA_FILE"
  18. # Alternative, not actively maintained as of March 2024:
  19. # https://github.com/YousefED/typescript-json-schema
  20. # npx typescript-json-schema --defaultProps --required "$DTS_FILE" MyType | tee "$SCHEMA_FILE" >&2
  21. ./examples/json_schema_to_grammar.py "$SCHEMA_FILE"