Skip to content

Commit

Permalink
Check to see if a Viewer is running and skip the build check (#74)
Browse files Browse the repository at this point in the history
* Add a check to see if a Viser viewer is already running on your machine. If so, skip building on start.

* Swap build dir check and process check

If no dir exists at all, the program will crash since it still tries to host one
  • Loading branch information
jonahbedouch authored Aug 6, 2023
1 parent 0e6519e commit 5e35930
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies = [
"rich>=13.3.3",
"trimesh>=3.21.7",
"nodeenv>=1.8.0",
"psutil>=5.9.5",
]

[project.optional-dependencies]
Expand Down
25 changes: 22 additions & 3 deletions viser/_client_autobuild.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import subprocess
import sys
import psutil
import rich
from pathlib import Path

client_dir = Path(__file__).absolute().parent / "client"
build_dir = client_dir / "build"


def _check_process(process_name: str) -> bool:
"""
Check if a process is running
"""
for process in psutil.process_iter():
if process_name == process.name():
return True
return False


def ensure_client_is_built() -> None:
"""Ensure that the client is built."""

Expand All @@ -19,12 +31,19 @@ def ensure_client_is_built() -> None:
# Do we need to re-trigger a build?
build = False
if not (build_dir / "index.html").exists():
print("[viser] No client build found. Building now...")
rich.print("[bold](viser)[/bold] No client build found. Building now...")
build = True
elif _check_process("Viser Viewer"):
rich.print(
"[bold](viser)[/bold] A Viser viewer is already running. Skipping build check..."
)
build = False
elif _modified_time_recursive(client_dir / "src") > _modified_time_recursive(
build_dir
):
print("[viser] Client build looks out of date. Building now...")
rich.print(
"[bold](viser)[/bold] Client build looks out of date. Building now..."
)
build = True

# Install nodejs and build if necessary. We assume bash is installed.
Expand All @@ -49,7 +68,7 @@ def _install_sandboxed_node() -> Path:
environment root."""
env_dir = client_dir / ".nodeenv"
if (env_dir / "bin" / "npx").exists():
print("[viser] nodejs is set up!")
rich.print("[bold](viser)[/bold] nodejs is set up!")
return env_dir

subprocess.run([sys.executable, "-m", "nodeenv", "--node=20.4.0", env_dir])
Expand Down
2 changes: 2 additions & 0 deletions viser/client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import svgrPlugin from "vite-plugin-svgr";
import eslint from "vite-plugin-eslint";
import browserslistToEsbuild from "browserslist-to-esbuild";

process.title = "Viser Viewer"

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), eslint(), viteTsconfigPaths(), svgrPlugin()],
Expand Down

0 comments on commit 5e35930

Please sign in to comment.