Skip to content

Instantly share code, notes, and snippets.

@weibeu
Created May 17, 2020 10:10
Show Gist options
  • Save weibeu/59211b0453d76c20d993524da27e8deb to your computer and use it in GitHub Desktop.
Save weibeu/59211b0453d76c20d993524da27e8deb to your computer and use it in GitHub Desktop.
A script which builds react application and moves the build files to Python web framework template directories.
import os
import shutil
JS_BASE_DIR = "build/static/js/"
CSS_BASE_DIR = "build/static/css/"
JS_TARGET_DIR = "build/js/"
CSS_TARGET_DIR = "build/css/"
REACT_BUILD_DIR = "build/"
DJANGO_STATIC_DIR = "../AmpleView/ampleview/static/"
DJANGO_TEMPLATE_DIR = "../AmpleView/ampleview/templates/"
def build():
os.system("npm run-script build")
def clean():
shutil.rmtree(DJANGO_STATIC_DIR)
shutil.rmtree(DJANGO_TEMPLATE_DIR)
os.mkdir(DJANGO_STATIC_DIR)
os.mkdir(DJANGO_TEMPLATE_DIR)
def move(from_dir, to_dir):
for f in os.listdir(from_dir):
from_file = os.path.join(from_dir, f)
to_file = os.path.join(to_dir, f)
os.replace(from_file, to_file)
if __name__ == "__main__":
# Build React application.
clean()
build()
# Move JS files.
move(JS_BASE_DIR, JS_TARGET_DIR)
# Move CSS files.
move(CSS_BASE_DIR, CSS_TARGET_DIR)
# Move build files to Django static.
move(REACT_BUILD_DIR, DJANGO_STATIC_DIR)
# Move index.html from Django static files to templates.
os.replace(f"{DJANGO_STATIC_DIR}index.html", f"{DJANGO_TEMPLATE_DIR}index.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment