Last active
August 10, 2018 07:28
-
-
Save acro5piano/b1a44b9cf9d40fdcb679e906651ec752 to your computer and use it in GitHub Desktop.
Rudimentary graphql to typescript definition generator. Accepts a graphql schema file (not json, nor js) and converts it to typescript defs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
cat "$1" \ | |
| perl -pe 's/^(\s*)#/\1\/\//' \ | |
| perl -pe 's/!//g' \ | |
| perl -pe 's/^input /export interface /g' \ | |
| perl -pe 's/^enum /export enum /g' \ | |
| perl -pe 's/^type /export interface /g' \ | |
| perl -pe 's/^union /export type /g'\ | |
| perl -pe 's/^schema /export interface schema /g' \ | |
| perl -pe 's/^(\s+[a-zA-Z0-9_-]+\s*)\([^\)]+\)(\s*:\s*)/\1\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)\[ID!?\]!?($|\s+|\))/\1string\[\]\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)ID!?($|\s+|\))/\1string\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)\[String!?\]!?($|\s+|\))/\1string\[\]\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)String!?($|\s+|\))/\1string\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)\[Int!?\]!?($|\s+|\))/\1number\[\]\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)Int!?($|\s+|\))/\1number\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)\[Float!?\]!?($|\s+|\))/\1number\[\]\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)Float!?($|\s+|\))/\1number\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)\[Boolean!?\]!?($|\s+|\))/\1boolean[\]\2/g' \ | |
| perl -pe 's/([:\=\|]\s*)Boolean!?($|\s+|\))/\1boolean\2/g' \ | |
| perl -pe 's/\[([a-zA-Z0-9_-]+)!?\]!?/\1\[\]/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perl version, as for not GNU sed