Updated build-frontend script, now uses deno, npm, bun in this order.
This commit is contained in:
+35
-34
@@ -1,47 +1,48 @@
|
|||||||
import os
|
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
from contextlib import contextmanager
|
from pathlib import Path
|
||||||
from sys import stderr
|
from sys import stderr
|
||||||
|
|
||||||
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
|
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
def run(cmd, **kwargs):
|
||||||
def chdir(path):
|
display_cmd = [Path(cmd[0]).name, *cmd[1:]]
|
||||||
original = os.getcwd()
|
stderr.write(f"### {' '.join(display_cmd)}\n")
|
||||||
os.chdir(path)
|
subprocess.run(cmd, check=True, **kwargs)
|
||||||
try:
|
|
||||||
yield
|
|
||||||
finally:
|
def find_build_tool():
|
||||||
os.chdir(original)
|
install = [
|
||||||
|
("deno", "install", "--allow-scripts=npm:vue-demi"),
|
||||||
|
("npm", "install"),
|
||||||
|
("bun", "--bun", "install"),
|
||||||
|
]
|
||||||
|
|
||||||
|
build = [
|
||||||
|
("deno", "task", "build"),
|
||||||
|
("npm", "run", "build"),
|
||||||
|
("bun", "--bun", "run", "build"),
|
||||||
|
]
|
||||||
|
|
||||||
|
for i, b in zip(install, build, strict=False):
|
||||||
|
if tool := shutil.which(i[0]):
|
||||||
|
return [tool, *i[1:]], [tool, *b[1:]]
|
||||||
|
|
||||||
|
raise RuntimeError("Deno, npm or Bun is required for building but none was found")
|
||||||
|
|
||||||
|
|
||||||
class CustomBuildHook(BuildHookInterface):
|
class CustomBuildHook(BuildHookInterface):
|
||||||
def initialize(self, version, build_data):
|
def initialize(self, version, build_data):
|
||||||
super().initialize(version, build_data)
|
super().initialize(version, build_data)
|
||||||
stderr.write(">>> Building the frontend\n")
|
stderr.write(">>> Building the frontend\n")
|
||||||
npm = None
|
|
||||||
bun = shutil.which("bun")
|
install_cmd, build_cmd = find_build_tool()
|
||||||
if bun is None:
|
|
||||||
npm = shutil.which("npm")
|
try:
|
||||||
if npm is None:
|
run(install_cmd, cwd="frontend")
|
||||||
raise RuntimeError(
|
stderr.write("\n")
|
||||||
"Bun or NodeJS `npm` is required for building but neither was found"
|
run(build_cmd, cwd="frontend")
|
||||||
)
|
except Exception as e:
|
||||||
# npm --prefix doesn't work on Windows, so we chdir instead
|
stderr.write(f"Error occurred while building frontend: {e}\n")
|
||||||
with chdir("frontend"):
|
raise
|
||||||
try:
|
|
||||||
if npm:
|
|
||||||
stderr.write("### npm install\n")
|
|
||||||
subprocess.run([npm, "install"], check=True) # noqa: S603
|
|
||||||
stderr.write("\n### npm run build\n")
|
|
||||||
subprocess.run([npm, "run", "build"], check=True) # noqa: S603
|
|
||||||
else:
|
|
||||||
assert bun
|
|
||||||
stderr.write("### bun --bun install\n")
|
|
||||||
subprocess.run([bun, "--bun", "install"], check=True) # noqa: S603
|
|
||||||
stderr.write("\n### bun --bun run build\n")
|
|
||||||
subprocess.run([bun, "--bun", "run", "build"], check=True) # noqa: S603
|
|
||||||
except Exception:
|
|
||||||
stderr.write("Error occurred while building frontend\n")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user