Repository navigation
Expand file tree
/
Copy pathhatch_build.py
More file actions
43 lines (31 loc) · 1.52 KB
/
Copy pathhatch_build.py
File metadata and controls
43 lines (31 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Builds the viewer bundle before hatchling packs a wheel or an sdist.
The HTML report inlines ``viewer/dist/viewer.js``, which esbuild produces from
the TypeScript sources. It is a build output, so git ignores it; this hook
builds it whenever Node.js is available and fails clearly when it is not,
unless the bundle is already there (as in a wheel built from an sdist).
"""
from __future__ import annotations
import shutil
import subprocess
from pathlib import Path
from typing import Any
from hatchling.builders.config import BuilderConfig
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
VIEWER_BUNDLE = "src/codemap/rendering/viewer/dist/viewer.js"
class ViewerBuildHook(BuildHookInterface[BuilderConfig]):
PLUGIN_NAME = "custom"
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
root = Path(self.root)
npm = shutil.which("npm")
if npm and (root / "package.json").is_file():
self._build(root, npm)
if not (root / VIEWER_BUNDLE).is_file():
raise RuntimeError(
f"{VIEWER_BUNDLE} is missing: install Node.js 22.6 or newer, "
"then run `npm ci && npm run build`"
)
def _build(self, root: Path, npm: str) -> None:
# npm ci wipes node_modules, so it runs only when the dependencies are not installed yet.
if not (root / "node_modules").is_dir():
subprocess.run([npm, "ci"], cwd=root, check=True)
subprocess.run([npm, "run", "build"], cwd=root, check=True)