33 lines
929 B
Bash
Executable File
33 lines
929 B
Bash
Executable File
#!/usr/bin/env bash
|
|
# Run npm/npx with Chinese DNS overrides (WSL nameserver often hangs).
|
|
set -euo pipefail
|
|
ROOT="$(cd "$(dirname "$0")" && pwd)"
|
|
REAL_HOME="${HOME}"
|
|
DNS_DIR="${TMPDIR:-/tmp}/npm-dns-fix"
|
|
mkdir -p "$DNS_DIR"
|
|
printf 'nameserver 223.5.5.5\nnameserver 119.29.29.29\noptions timeout:2 attempts:2\n' > "$DNS_DIR/resolv.conf"
|
|
cat > "$DNS_DIR/hosts" <<'EOF'
|
|
127.0.0.1 localhost
|
|
::1 localhost
|
|
104.16.2.34 registry.npmjs.org
|
|
76.76.21.61 ui.shadcn.com
|
|
66.33.60.130 ui.shadcn.com
|
|
151.101.65.229 cdn.jsdelivr.net
|
|
47.96.233.62 npmmirror.com
|
|
EOF
|
|
|
|
CMD="${1:?usage: npm-cn <npm|npx> ...}"
|
|
shift
|
|
|
|
if unshare --user --map-root-user --mount true 2>/dev/null; then
|
|
exec unshare --user --map-root-user --mount bash -c "
|
|
mount --bind '$DNS_DIR/resolv.conf' /etc/resolv.conf
|
|
mount --bind '$DNS_DIR/hosts' /etc/hosts
|
|
export HOME='$REAL_HOME'
|
|
cd '$ROOT'
|
|
exec '$CMD' \"\$@\"
|
|
" bash "$@"
|
|
else
|
|
exec "$CMD" "$@"
|
|
fi
|