mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 17:10:30 +00:00
autogenerate types via scripts
This commit is contained in:
parent
bc5d937c7f
commit
b672e6a739
2 changed files with 83 additions and 8 deletions
27
Taskfile.yml
27
Taskfile.yml
|
@ -1,16 +1,27 @@
|
||||||
version: "3"
|
version: "3"
|
||||||
|
|
||||||
tasks:
|
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:
|
api:
|
||||||
cmds:
|
cmds:
|
||||||
- cd backend/app/api/ && swag fmt
|
- task: generate
|
||||||
- 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
|
|
||||||
- cd backend && go run ./app/api/ {{.CLI_ARGS}}
|
- cd backend && go run ./app/api/ {{.CLI_ARGS}}
|
||||||
silent: false
|
silent: false
|
||||||
sources:
|
sources:
|
||||||
|
|
64
scripts/process-types.py
Normal file
64
scripts/process-types.py
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue