mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-18 13:06:32 +00:00
chore: rewrite python script in go (#201)
This commit is contained in:
parent
891d41b75f
commit
490a0ece86
4 changed files with 75 additions and 71 deletions
|
@ -26,7 +26,7 @@ tasks:
|
||||||
--modular \
|
--modular \
|
||||||
--path ./backend/app/api/static/docs/swagger.json \
|
--path ./backend/app/api/static/docs/swagger.json \
|
||||||
--output ./frontend/lib/api/types
|
--output ./frontend/lib/api/types
|
||||||
- python3 ./scripts/process-types.py ./frontend/lib/api/types/data-contracts.ts
|
- go run ./scripts/process-types/*.go ./frontend/lib/api/types/data-contracts.ts
|
||||||
sources:
|
sources:
|
||||||
- "./backend/app/api/**/*"
|
- "./backend/app/api/**/*"
|
||||||
- "./backend/internal/data/**"
|
- "./backend/internal/data/**"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* post-processed by ./scripts/process-types.py */
|
/* post-processed by ./scripts/process-types.go */
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
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" PaginationResultRepo"): "PaginationResult",
|
|
||||||
re.compile(r" Repo"): " ",
|
|
||||||
re.compile(r" Services"): " ",
|
|
||||||
re.compile(r" V1"): " ",
|
|
||||||
re.compile(r"\?:"): ":",
|
|
||||||
**date_types(
|
|
||||||
"createdAt",
|
|
||||||
"updatedAt",
|
|
||||||
"soldTime",
|
|
||||||
"purchaseTime",
|
|
||||||
"warrantyExpires",
|
|
||||||
"expiresAt",
|
|
||||||
"date",
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
73
scripts/process-types/main.go
Normal file
73
scripts/process-types/main.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dateTypes(names []string) map[*regexp.Regexp]string {
|
||||||
|
result := make(map[*regexp.Regexp]string)
|
||||||
|
for _, name := range names {
|
||||||
|
result[regexp.MustCompile(fmt.Sprintf(`%s: string`, name))] = fmt.Sprintf(`%s: Date`, name)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) != 2 {
|
||||||
|
fmt.Println("Please provide a file path as an argument")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := os.Args[1]
|
||||||
|
|
||||||
|
fmt.Printf("Processing %s\n", path)
|
||||||
|
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
fmt.Printf("File %s does not exist\n", path)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
text := "/* post-processed by ./scripts/process-types.go */\n"
|
||||||
|
data, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
text += string(data)
|
||||||
|
|
||||||
|
regexReplace := map[*regexp.Regexp]string{
|
||||||
|
regexp.MustCompile(` PaginationResultRepo`): " PaginationResult",
|
||||||
|
regexp.MustCompile(` Repo`): " ",
|
||||||
|
regexp.MustCompile(` Services`): " ",
|
||||||
|
regexp.MustCompile(` V1`): " ",
|
||||||
|
regexp.MustCompile(`\?:`): ":",
|
||||||
|
}
|
||||||
|
|
||||||
|
for regex, replace := range dateTypes([]string{
|
||||||
|
"createdAt",
|
||||||
|
"updatedAt",
|
||||||
|
"soldTime",
|
||||||
|
"purchaseTime",
|
||||||
|
"warrantyExpires",
|
||||||
|
"expiresAt",
|
||||||
|
"date",
|
||||||
|
}) {
|
||||||
|
regexReplace[regex] = replace
|
||||||
|
}
|
||||||
|
|
||||||
|
for regex, replace := range regexReplace {
|
||||||
|
fmt.Printf("Replacing '%v' -> '%s'\n", regex, replace)
|
||||||
|
text = regex.ReplaceAllString(text, replace)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(path, []byte(text), 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
Loading…
Reference in a new issue