Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use path instead of proxy for dev frontend #3097

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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