Skip to content

Commit

Permalink
Use path instead of proxy for dev frontend (#3097)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Apr 1, 2023
1 parent ea25a61 commit d7482db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
29 changes: 5 additions & 24 deletions custom_components/hacs/frontend.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
"<HacsFrontend> 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("<HacsFrontend> Using experimental frontend")
hass.http.register_static_path(
Expand Down Expand Up @@ -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
13 changes: 11 additions & 2 deletions scripts/develop
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

declare frontend_dir

set -e

cd "$(dirname "$0")/.."
Expand All @@ -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
HACS_FRONTEND_DIR="$(readlink -f ${frontend_dir})" hass -c . --debug

0 comments on commit d7482db

Please sign in to comment.