From d7482db8da6b7c554c2e9d7830e31e09918ec73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 1 Apr 2023 13:32:10 +0200 Subject: [PATCH] Use path instead of proxy for dev frontend (#3097) --- custom_components/hacs/frontend.py | 29 +++++------------------------ scripts/develop | 13 +++++++++++-- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/custom_components/hacs/frontend.py b/custom_components/hacs/frontend.py index 4adb32bab64..c49c35f46ef 100644 --- a/custom_components/hacs/frontend.py +++ b/custom_components/hacs/frontend.py @@ -1,10 +1,9 @@ """"Starting setup task: Frontend".""" from __future__ import annotations +import os from typing import TYPE_CHECKING -from aiohttp import web -from homeassistant.components.http import HomeAssistantView from homeassistant.core import HomeAssistant, callback from .const import DOMAIN, URL_BASE @@ -26,11 +25,13 @@ def async_register_frontend(hass: HomeAssistant, hacs: HacsBase) -> None: hacs.async_setup_frontend_endpoint_themes() # Register frontend - if hacs.configuration.frontend_repo_url: + if hacs.configuration.dev and (frontend_path := os.getenv("HACS_FRONTEND_DIR")): hacs.log.warning( " Frontend development mode enabled. Do not run in production!" ) - hass.http.register_view(HacsFrontendDev()) + hass.http.register_static_path( + f"{URL_BASE}/frontend", f"{frontend_path}/hacs_frontend", cache_headers=False + ) elif hacs.configuration.experimental: hacs.log.info(" Using experimental frontend") hass.http.register_static_path( @@ -72,23 +73,3 @@ def async_register_frontend(hass: HomeAssistant, hacs: HacsBase) -> None: # Setup plugin endpoint if needed hacs.async_setup_frontend_endpoint_plugin() - - -class HacsFrontendDev(HomeAssistantView): - """Dev View Class for HACS.""" - - requires_auth = False - name = "hacs_files:frontend" - url = r"/hacsfiles/frontend/{requested_file:.+}" - - async def get(self, request, requested_file): # pylint: disable=unused-argument - """Handle HACS Web requests.""" - hacs: HacsBase = request.app["hass"].data.get(DOMAIN) - requested = requested_file.split("/")[-1] - request = await hacs.session.get(f"{hacs.configuration.frontend_repo_url}/{requested}") - if request.status == 200: - result = await request.read() - response = web.Response(body=result) - response.headers["Content-Type"] = "application/javascript" - - return response diff --git a/scripts/develop b/scripts/develop index 98d4c7a39fb..2f9844be739 100755 --- a/scripts/develop +++ b/scripts/develop @@ -1,5 +1,7 @@ #!/usr/bin/env bash +declare frontend_dir + set -e cd "$(dirname "$0")/.." @@ -19,8 +21,15 @@ logger: " >> "configuration.yaml" fi +while getopts u:a:f: flag +do + case "${flag}" in + f) frontend_dir=${OPTARG};; + esac +done + echo "Installing HACS frontend" -bash "scripts/install/frontend" +[ -z "${frontend_dir}" ] && bash "scripts/install/frontend" # Start Home Assistant -hass -c . --debug \ No newline at end of file +HACS_FRONTEND_DIR="$(readlink -f ${frontend_dir})" hass -c . --debug \ No newline at end of file