Add JSX compiler script.
This commit is contained in:
parent
d39a660eab
commit
aeb574838d
53
bemani/utils/jsx.py
Normal file
53
bemani/utils/jsx.py
Normal file
@ -0,0 +1,53 @@
|
||||
import argparse
|
||||
import os
|
||||
from react.jsx import JSXTransformer # type: ignore
|
||||
|
||||
|
||||
SCRIPT_PATH: str = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Prebuild all JSX files so they can be statically served by nginx or similar.")
|
||||
parser.add_argument("-d", "--output-directory", help="Output directory for the JSX files.", type=str, default="./build/jsx")
|
||||
args = parser.parse_args()
|
||||
|
||||
outdir = os.path.abspath(args.output_directory)
|
||||
print(f"Compiling all files into {outdir}...")
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
|
||||
basedir = os.path.abspath(os.path.join(SCRIPT_PATH, "../frontend/static"))
|
||||
if not basedir.endswith("/"):
|
||||
basedir += "/"
|
||||
|
||||
dirs = [basedir]
|
||||
files = []
|
||||
while dirs:
|
||||
curdir = dirs.pop()
|
||||
for dirpath, dnames, fnames in os.walk(curdir):
|
||||
for fname in fnames:
|
||||
if fname.endswith(".react.js"):
|
||||
fullpath = os.path.join(dirpath, fname)
|
||||
if fullpath.startswith(basedir):
|
||||
files.append(fullpath[len(basedir):])
|
||||
for dname in dnames:
|
||||
if dname == "." or dname == "..":
|
||||
continue
|
||||
dirs.append(os.path.join(dirpath, dname))
|
||||
|
||||
transformer = JSXTransformer()
|
||||
for fname in sorted(list(set(files))):
|
||||
print(f"Building {fname}...")
|
||||
infile = os.path.join(basedir, fname)
|
||||
outfile = os.path.join(outdir, fname)
|
||||
os.makedirs(os.path.dirname(outfile), exist_ok=True)
|
||||
|
||||
with open(infile, 'rb') as f:
|
||||
jsx = transformer.transform_string(f.read().decode('utf-8')).encode('utf-8')
|
||||
|
||||
print(f"Writing {outfile}...")
|
||||
with open(outfile, 'wb') as f:
|
||||
f.write(jsx)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
11
jsx
Executable file
11
jsx
Executable file
@ -0,0 +1,11 @@
|
||||
#! /usr/bin/env python3
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
path = os.path.abspath(os.path.dirname(__file__))
|
||||
name = os.path.basename(__file__)
|
||||
|
||||
import sys
|
||||
sys.path.append(path)
|
||||
|
||||
import runpy
|
||||
runpy.run_module(f"bemani.utils.{name}", run_name="__main__")
|
Loading…
Reference in New Issue
Block a user