-
Notifications
You must be signed in to change notification settings - Fork 30
/
generate-typescript-types.py
executable file
·97 lines (80 loc) · 2.35 KB
/
generate-typescript-types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# SPDX-FileCopyrightText: Contributors to the Fedora Project
#
# SPDX-License-Identifier: MIT
import os
import shutil
import signal
import socket
import time
from contextlib import closing, contextmanager
from subprocess import CalledProcessError, Popen, run
DEST = os.path.abspath(os.path.join(os.path.dirname(__file__), "frontend/src/api/generated.ts"))
def find_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
def find_cmd(cmd):
path = shutil.which(cmd)
if path is None:
raise RuntimeError(f"Can't find the executable {cmd}")
return path
def call(cmd):
print(" ".join(cmd))
run(cmd, check=True) # noqa: S603
@contextmanager
def running(cmd):
proc = Popen(cmd) # noqa: S603
try:
yield proc
finally:
proc.send_signal(signal.SIGINT)
proc.wait(timeout=10)
# The Uvicorn process now returns 1 when it recieves SIGINT
if proc.returncode not in (0, 1):
raise CalledProcessError(proc.returncode, proc.args, proc.stdout, proc.stderr)
@contextmanager
def pushd(directory):
curdir = os.getcwd()
os.chdir(directory)
try:
yield curdir
finally:
os.chdir(curdir)
def main():
port = find_free_port()
fmn_cmd = find_cmd("fmn")
npm_cmd = find_cmd("npm")
os.environ["DATABASE__SQLALCHEMY__URL"] = "sqlite:///"
print("Running API")
with running([fmn_cmd, "api", "serve", "--port", str(port)]):
time.sleep(3)
with pushd("frontend"):
print("Building Typescript types")
call(
[
npm_cmd,
"exec",
"--yes",
"openapi-typescript@latest",
"--",
f"http://127.0.0.1:{port}/openapi.json",
"--output",
DEST,
]
)
call(
[
npm_cmd,
"exec",
"prettier",
"--",
"-l",
"--write",
DEST,
]
)
print("Shutting down API")
if __name__ == "__main__":
main()