Skip to content

Commit 681306e

Browse files
committed
feat: install pnpm
1 parent 64dc10a commit 681306e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

devenv/lib/pnpm.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import shutil
5+
import stat
6+
import tempfile
7+
8+
from devenv.lib import archive
9+
from devenv.lib import fs
10+
from devenv.lib import proc
11+
12+
13+
def _install(url: str, sha256: str, into: str) -> None:
14+
with tempfile.TemporaryDirectory(dir=into) as tmpd:
15+
bin_file = archive.download(url, sha256, dest=f"{tmpd}/pnpm")
16+
17+
target = f"{into}/pnpm"
18+
os.replace(bin_file, target)
19+
mode = os.stat(target).st_mode
20+
os.chmod(target, mode | stat.S_IEXEC)
21+
22+
23+
def uninstall(binroot: str) -> None:
24+
try:
25+
os.remove(f"{binroot}/pnpm")
26+
except FileNotFoundError:
27+
# it's better to do this than to guard with
28+
# os.path.exists(fp) because if it's an invalid or circular
29+
# symlink the result'll be False!
30+
pass
31+
32+
33+
def _version(binpath: str) -> str:
34+
stdout = proc.run((binpath, "--version"), stdout=True)
35+
# 10.16.1
36+
return stdout.strip()
37+
38+
39+
def install(version: str, url: str, sha256: str, reporoot: str) -> None:
40+
binroot = fs.ensure_binroot(reporoot)
41+
binpath = f"{binroot}/pnpm"
42+
43+
if shutil.which("pnpm", path=binroot) == binpath:
44+
installed_version = _version(binpath)
45+
if version == installed_version:
46+
return
47+
print(f"installed pnpm {installed_version} is unexpected!")
48+
49+
print(f"installing pnpm {version}...")
50+
uninstall(binroot)
51+
_install(url, sha256, binroot)
52+
53+
installed_version = _version(binpath)
54+
if version != installed_version:
55+
raise SystemExit("Failed to install pnpm {version}!")

0 commit comments

Comments
 (0)