From aeb574838dad27c8c2614e0d49412997e304485a Mon Sep 17 00:00:00 2001 From: Jennifer Taylor Date: Thu, 4 Aug 2022 00:10:21 +0000 Subject: [PATCH] Add JSX compiler script. --- bemani/utils/jsx.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ jsx | 11 ++++++++++ 2 files changed, 64 insertions(+) create mode 100644 bemani/utils/jsx.py create mode 100755 jsx diff --git a/bemani/utils/jsx.py b/bemani/utils/jsx.py new file mode 100644 index 0000000..6b67d88 --- /dev/null +++ b/bemani/utils/jsx.py @@ -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() diff --git a/jsx b/jsx new file mode 100755 index 0000000..ce6f396 --- /dev/null +++ b/jsx @@ -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__") \ No newline at end of file