-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnpm.sh
110 lines (92 loc) · 2.9 KB
/
pnpm.sh
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
# From https://github.com/Homebrew/install/blob/master/install.sh
abort() {
printf "%s\n" "$@"
exit 1
}
# string formatters
if [ -t 1 ]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_blue="$(tty_mkbold 34)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
ohai() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$1"
}
# End from https://github.com/Homebrew/install/blob/master/install.sh
download() {
if command -v curl > /dev/null 2>&1; then
curl -fsSL "$1"
else
wget -qO- "$1"
fi
}
validate_url() {
local url="$1"
if command -v curl > /dev/null 2>&1; then
curl --output /dev/null --silent --show-error --location --head --fail "$url"
else
wget --spider --quiet "$url"
fi
}
detect_platform() {
local platform
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "${platform}" in
linux) platform="linux" ;;
darwin) platform="macos" ;;
windows) platform="win" ;;
esac
printf '%s' "${platform}"
}
detect_arch() {
local arch
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
case "${arch}" in
x86_64) arch="x64" ;;
amd64) arch="x64" ;;
armv*) arch="arm" ;;
arm64 | aarch64) arch="arm64" ;;
esac
# `uname -m` in some cases mis-reports 32-bit OS as 64-bit, so double check
if [ "${arch}" = "x64" ] && [ "$(getconf LONG_BIT)" -eq 32 ]; then
arch=i686
elif [ "${arch}" = "arm64" ] && [ "$(getconf LONG_BIT)" -eq 32 ]; then
arch=arm
fi
case "$arch" in
x64*) ;;
arm64*) ;;
*) return 1
esac
printf '%s' "${arch}"
}
download_and_install() {
local platform arch pkgName version_json version archive_url tmp_dir
platform="$(detect_platform)"
arch="$(detect_arch)" || abort "Sorry! pnpm currently only provides pre-built binaries for x86_64/arm64 architectures."
if [ -z "${PNPM_VERSION}" ]; then
version_json="$(download "https://registry.npmjs.org/@pnpm/exe")" || abort "Download Error!"
version="$(printf '%s' "${version_json}" | tr '{' '\n' | awk -F '"' '/latest/ { print $4 }')"
else
version="${PNPM_VERSION}"
fi
pkgName="@pnpm/${platform}-${arch}"
archive_url="https://registry.npmjs.org/${pkgName}/-/${platform}-${arch}-${version}.tgz"
validate_url "$archive_url" || abort "pnpm version '${version}' could not be found"
# install to PNPM_HOME, defaulting to ~/.pnpm
tmp_dir="$(mktemp -d)" || abort "Tmpdir Error!"
trap 'rm -rf "$tmp_dir"' EXIT INT TERM HUP
ohai "Extracting pnpm binaries ${version}"
# extract the files to the specified directory
download "$archive_url" | tar -xz -C "$tmp_dir" --strip-components=1 || return 1
# for some reason with pnpm v7 this artifact stopped being an executable
# so this is a workaround to make the CLI executable before running it
chmod +x "$tmp_dir/pnpm"
SHELL="$SHELL" "$tmp_dir/pnpm" setup || return 1
}
download_and_install || abort "Install Error!"