Skip to content

Commit

Permalink
feat: enable nodeenv detection and auto-install
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Dec 17, 2023
1 parent 4489d58 commit 4fb1148
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
51 changes: 45 additions & 6 deletions litestar_vite/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import sys
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -66,6 +65,15 @@ def vite_group() -> None:
show_default=True,
is_flag=True,
)
@option(
"--no-install",
help="Do not execute the installation commands after generating templates.",
type=bool,
default=False,
required=False,
show_default=True,
is_flag=True,
)
def vite_init(
ctx: Context,
vite_port: int | None,
Expand All @@ -76,14 +84,20 @@ def vite_init(
overwrite: bool,
verbose: bool,
no_prompt: bool,
no_install: bool,
) -> None: # sourcery skip: low-code-quality
"""Run vite build."""
import os
import sys
from importlib.util import find_spec
from pathlib import Path

from litestar.cli._utils import (
console,
)
from rich.prompt import Confirm

from litestar_vite.commands import init_vite
from litestar_vite.commands import execute_command, init_vite
from litestar_vite.plugin import VitePlugin

if callable(ctx.obj):
Expand Down Expand Up @@ -134,6 +148,18 @@ def vite_init(
hot_file=hot_file,
litestar_port=env.port or 8000,
)
if not no_install:
if find_spec("nodeenv") is not None and plugin.config.detect_nodeenv:
"""Detect nodeenv installed in the current python env before using a global version"""
nodeenv_command = str(Path(Path(sys.executable) / "nodeenv")) or "nodeenv"
install_dir = os.environ.get("VIRTUAL_ENV", sys.prefix)
console.rule("[yellow]Starting Nodeenv installation process[/]", align="left")
console.print("Installing Node environment to %s:", install_dir)
execute_command([nodeenv_command, install_dir, "--force", "--quiet"])

console.rule("[yellow]Starting package installation process[/]", align="left")

execute_command(plugin.config.install_command)


@vite_group.command( # type: ignore # noqa: PGH003
Expand All @@ -143,17 +169,30 @@ def vite_init(
@option("--verbose", type=bool, help="Enable verbose output.", default=False, is_flag=True)
def vite_install(app: Litestar, verbose: bool) -> None:
"""Run vite build."""
from litestar.cli._utils import (
console,
)
import os
import sys
from importlib.util import find_spec
from pathlib import Path

from litestar.cli._utils import console

from litestar_vite.commands import execute_command
from litestar_vite.plugin import VitePlugin

if verbose:
app.debug = True
console.rule("[yellow]Starting Vite package installation process[/]", align="left")
plugin = app.plugins.get(VitePlugin)

if find_spec("nodeenv") is not None and plugin.config.detect_nodeenv:
"""Detect nodeenv installed in the current python env before using a global version"""
nodeenv_command = str(Path(Path(sys.executable) / "nodeenv")) or "nodeenv"
install_dir = os.environ.get("VIRTUAL_ENV", sys.prefix)
console.rule("[yellow]Starting Nodeenv installation process[/]", align="left")
console.print("Installing Node environment to %s:", install_dir)
execute_command([nodeenv_command, install_dir, "--force", "--quiet"])

console.rule("[yellow]Starting package installation process[/]", align="left")

execute_command(plugin.config.install_command)


Expand Down
6 changes: 4 additions & 2 deletions litestar_vite/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import platform
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, Any, MutableMapping
Expand Down Expand Up @@ -48,6 +49,7 @@ def init_vite(
) -> None:
"""Initialize a new vite project."""
from jinja2 import Environment, FileSystemLoader
from litestar.cli._utils import console

entry_point: list[str] = []
vite_template_env = Environment(
Expand Down Expand Up @@ -91,7 +93,7 @@ def init_vite(

for resource_name in enabled_resources:
with Path(resource_path / resource_name).open(mode="w") as file:
logger.info("Writing %s", resource_name)
console.print("Writing %s", resource_name)


def get_template(
Expand All @@ -105,4 +107,4 @@ def get_template(

def execute_command(command_to_run: list[str]) -> subprocess.CompletedProcess[bytes]:
"""Run Vite in a subprocess."""
return subprocess.run(command_to_run, check=False) # noqa: S603
return subprocess.run(command_to_run, check=False, shell=platform.system() == "Windows") # noqa: S603
2 changes: 2 additions & 0 deletions litestar_vite/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class ViteConfig:
"""Utilize the server lifespan hook to run Vite."""
dev_mode: bool = False
"""When True, Vite will run with HMR or watch build"""
detect_nodeenv: bool = True
"""When True, The initializer will install and configure nodeenv if present"""

def __post_init__(self) -> None:
"""Ensure that directory is set if engine is a class."""
Expand Down

0 comments on commit 4fb1148

Please sign in to comment.