From b672e6a7395812c83c16f68de242a20d515caec4 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sun, 11 Sep 2022 16:10:20 -0800 Subject: [PATCH] autogenerate types via scripts --- Taskfile.yml | 27 ++++++++++++----- scripts/process-types.py | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 scripts/process-types.py diff --git a/Taskfile.yml b/Taskfile.yml index bea4fd4..71d38c7 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,16 +1,27 @@ version: "3" tasks: + generate: + cmds: + - cd backend && go generate ./... + - cd backend/app/api/ && swag fmt + - cd backend/app/api/ && swag init --dir=./,../../internal,../../pkgs + - | + npx swagger-typescript-api \ + --no-client \ + --clean-output \ + --modular \ + --path ./backend/app/api/docs/swagger.json \ + --output ./frontend/lib/api/types + + # Output cleanup for the generated types + # 1. Remove the prefix `Types` from each type generated + # 2. Remove the ?: from the type definition since types are not properly annotated in the swagger.json + + python3 ./scripts/process-types.py ./frontend/lib/api/types/data-contracts.ts api: cmds: - - cd backend/app/api/ && swag fmt - - cd backend/app/api/ && swag init --dir=./,../../internal,../../pkgs,../../ent - # - | - # npx swagger-typescript-api \ - # --path ./backend/app/api/docs/swagger.json \ - # --output ./client/auto-client \ - # --module-name-first-tag \ - # --modular + - task: generate - cd backend && go run ./app/api/ {{.CLI_ARGS}} silent: false sources: diff --git a/scripts/process-types.py b/scripts/process-types.py new file mode 100644 index 0000000..d3cc7d6 --- /dev/null +++ b/scripts/process-types.py @@ -0,0 +1,64 @@ +import re +import sys +from pathlib import Path + +""" +This script is used in conjunction with the swagger-typescript-api NPM package. +This does some post processing on the generated typescript files to make them +more compatible with the rest of the codebase. This performs a series of regex +replacements to better align types with what the server has. + +The following replacements are performed: + 1. Replaces all module precies of `Types` with empty string + 2. Replaces all optional fields with `:` instead of `?:` (due to lack of detailed swagger docs) + 3. Replaces all known date fields with `Date` instead of `string` +""" + +CWD = Path(__file__).parent + + +def date_types(*names: list[str]) -> dict[re.Pattern, str]: + return {re.compile(rf"{name}: string;"): rf"{name}: Date;" for name in names} + + +regex_replace: dict[re.Pattern, str] = { + re.compile(r"Types"): "", + re.compile(r"\?:"): ":", + **date_types( + "createdAt", + "updatedAt", + "soldTime", + "purchaseTime", + "warrantyExpires", + ), +} + + +def main(args: list[str]) -> bool: + path = Path(args[0]) + + print(f"Processing {path}") + + if not path.exists(): + print(f"File {path} does not exist") + return True + + text = "/* post-processed by ./scripts/process-types.py */\n" + with open(path, "r") as f: + text += f.read() + + for regex, replace in regex_replace.items(): + print(f"Replacing {regex} -> '{replace}'") + text = regex.sub(replace, text) + + with open(path, "w") as f: + f.write(text) + + return False + + +if __name__ == "__main__": + if error := main(sys.argv[1:]): + sys.exit(1) + + sys.exit(0)