homebox/scripts/process-types.py
Hayden 95ab14b866
feat: items-editor (#5)
* format readme

* update logo

* format html

* add logo to docs

* repository for document and document tokens

* add attachments type and repository

* autogenerate types via scripts

* use autogenerated types

* attachment type updates

* add insured and quantity fields for items

* implement HasID interface for entities

* implement label updates for items

* implement service update method

* WIP item update client side actions

* check err on attachment

* finish types for basic items editor

* remove unused var

* house keeping
2022-09-12 14:47:27 -08:00

64 lines
1.6 KiB
Python

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)