You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

install.sh 7.2 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/bin/sh
  2. set -e
  3. reset="\033[0m"
  4. red="\033[31m"
  5. green="\033[32m"
  6. yellow="\033[33m"
  7. cyan="\033[36m"
  8. white="\033[37m"
  9. gpg_key=23E7166788B63E1E
  10. yarn_get_tarball() {
  11. printf "$cyan> Downloading tarball...$reset\n"
  12. if [ "$1" = '--nightly' ]; then
  13. url=https://nightly.yarnpkg.com/latest.tar.gz
  14. elif [ "$1" = '--rc' ]; then
  15. url=https://yarnpkg.com/latest-rc.tar.gz
  16. elif [ "$1" = '--version' ]; then
  17. # Validate that the version matches MAJOR.MINOR.PATCH to avoid garbage-in/garbage-out behavior
  18. version=$2
  19. if echo $version | grep -qE "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$"; then
  20. url="https://yarnpkg.com/downloads/$version/yarn-v$version.tar.gz"
  21. else
  22. printf "$red> Version number must match MAJOR.MINOR.PATCH.$reset\n"
  23. exit 1;
  24. fi
  25. else
  26. url=https://yarnpkg.com/latest.tar.gz
  27. fi
  28. # Get both the tarball and its GPG signature
  29. tarball_tmp=`mktemp -t yarn.tar.gz.XXXXXXXXXX`
  30. if curl --fail -L -o "$tarball_tmp#1" "$url{,.asc}"; then
  31. yarn_verify_integrity $tarball_tmp
  32. printf "$cyan> Extracting to ~/.yarn...$reset\n"
  33. # All this dance is because `tar --strip=1` does not work everywhere
  34. temp=$(mktemp -d yarn.XXXXXXXXXX)
  35. tar zxf $tarball_tmp -C "$temp"
  36. mkdir .yarn
  37. mv "$temp"/*/* .yarn
  38. rm -rf "$temp"
  39. rm $tarball_tmp*
  40. else
  41. printf "$red> Failed to download $url.$reset\n"
  42. exit 1;
  43. fi
  44. }
  45. # Verifies the GPG signature of the tarball
  46. yarn_verify_integrity() {
  47. # Check if GPG is installed
  48. if [[ -z "$(command -v gpg)" ]]; then
  49. printf "$yellow> WARNING: GPG is not installed, integrity can not be verified!$reset\n"
  50. return
  51. fi
  52. if [ "$YARN_GPG" == "no" ]; then
  53. printf "$cyan> WARNING: Skipping GPG integrity check!$reset\n"
  54. return
  55. fi
  56. printf "$cyan> Verifying integrity...$reset\n"
  57. # Grab the public key if it doesn't already exist
  58. gpg --list-keys $gpg_key >/dev/null 2>&1 || (curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --import)
  59. if [ ! -f "$1.asc" ]; then
  60. printf "$red> Could not download GPG signature for this Yarn release. This means the release can not be verified!$reset\n"
  61. yarn_verify_or_quit "> Do you really want to continue?"
  62. return
  63. fi
  64. # Actually perform the verification
  65. if gpg --verify "$1.asc" $1; then
  66. printf "$green> GPG signature looks good$reset\n"
  67. else
  68. printf "$red> GPG signature for this Yarn release is invalid! This is BAD and may mean the release has been tampered with. It is strongly recommended that you report this to the Yarn developers.$reset\n"
  69. yarn_verify_or_quit "> Do you really want to continue?"
  70. fi
  71. }
  72. yarn_link() {
  73. printf "$cyan> Adding to \$PATH...$reset\n"
  74. YARN_PROFILE="$(yarn_detect_profile)"
  75. SOURCE_STR="\nexport PATH=\"\$HOME/.yarn/bin:\$HOME/.config/yarn/global/node_modules/.bin:\$PATH\"\n"
  76. if [ -z "${YARN_PROFILE-}" ] ; then
  77. printf "$red> Profile not found. Tried ${YARN_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.\n"
  78. echo "> Create one of them and run this script again"
  79. echo "> Create it (touch ${YARN_PROFILE}) and run this script again"
  80. echo " OR"
  81. printf "> Append the following lines to the correct file yourself:$reset\n"
  82. command printf "${SOURCE_STR}"
  83. else
  84. if ! grep -q 'yarn/bin' "$YARN_PROFILE"; then
  85. if [[ $YARN_PROFILE == *"fish"* ]]; then
  86. command fish -c 'set -U fish_user_paths $fish_user_paths ~/.yarn/bin'
  87. printf "$cyan> We've added ~/.yarn/bin to your fish_user_paths universal variable\n"
  88. else
  89. command printf "$SOURCE_STR" >> "$YARN_PROFILE"
  90. printf "$cyan> We've added the following to your $YARN_PROFILE\n"
  91. fi
  92. echo "> If this isn't the profile of your current shell then please add the following to your correct profile:"
  93. printf " $SOURCE_STR$reset\n"
  94. fi
  95. version=`$HOME/.yarn/bin/yarn --version` || (
  96. printf "$red> Yarn was installed, but doesn't seem to be working :(.$reset\n"
  97. exit 1;
  98. )
  99. printf "$green> Successfully installed Yarn $version! Please open another terminal where the \`yarn\` command will now be available.$reset\n"
  100. fi
  101. }
  102. yarn_detect_profile() {
  103. if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
  104. echo "${PROFILE}"
  105. return
  106. fi
  107. local DETECTED_PROFILE
  108. DETECTED_PROFILE=''
  109. local SHELLTYPE
  110. SHELLTYPE="$(basename "/$SHELL")"
  111. if [ "$SHELLTYPE" = "bash" ]; then
  112. if [ -f "$HOME/.bashrc" ]; then
  113. DETECTED_PROFILE="$HOME/.bashrc"
  114. elif [ -f "$HOME/.bash_profile" ]; then
  115. DETECTED_PROFILE="$HOME/.bash_profile"
  116. fi
  117. elif [ "$SHELLTYPE" = "zsh" ]; then
  118. DETECTED_PROFILE="$HOME/.zshrc"
  119. elif [ "$SHELLTYPE" = "fish" ]; then
  120. DETECTED_PROFILE="$HOME/.config/fish/config.fish"
  121. fi
  122. if [ -z "$DETECTED_PROFILE" ]; then
  123. if [ -f "$HOME/.profile" ]; then
  124. DETECTED_PROFILE="$HOME/.profile"
  125. elif [ -f "$HOME/.bashrc" ]; then
  126. DETECTED_PROFILE="$HOME/.bashrc"
  127. elif [ -f "$HOME/.bash_profile" ]; then
  128. DETECTED_PROFILE="$HOME/.bash_profile"
  129. elif [ -f "$HOME/.zshrc" ]; then
  130. DETECTED_PROFILE="$HOME/.zshrc"
  131. elif [ -f "$HOME/.config/fish/config.fish" ]; then
  132. DETECTED_PROFILE="$HOME/.config/fish/config.fish"
  133. fi
  134. fi
  135. if [ ! -z "$DETECTED_PROFILE" ]; then
  136. echo "$DETECTED_PROFILE"
  137. fi
  138. }
  139. yarn_reset() {
  140. unset -f yarn_install yarn_reset yarn_get_tarball yarn_link yarn_detect_profile yarn_verify_integrity yarn_verify_or_quit
  141. }
  142. yarn_install() {
  143. printf "${white}Installing Yarn!$reset\n"
  144. if [ -d "$HOME/.yarn" ]; then
  145. if which yarn; then
  146. local latest_url
  147. local specified_version
  148. local version_type
  149. if [ "$1" = '--nightly' ]; then
  150. latest_url=https://nightly.yarnpkg.com/latest-tar-version
  151. specified_version=`curl -sS $latest_url`
  152. version_type='latest'
  153. elif [ "$1" = '--version' ]; then
  154. specified_version=$2
  155. version_type='specified'
  156. elif [ "$1" = '--rc' ]; then
  157. latest_url=https://yarnpkg.com/latest-rc-version
  158. specified_version=`curl -sS $latest_url`
  159. version_type='rc'
  160. else
  161. latest_url=https://yarnpkg.com/latest-version
  162. specified_version=`curl -sS $latest_url`
  163. version_type='latest'
  164. fi
  165. yarn_version=`yarn -V`
  166. yarn_alt_version=`yarn --version`
  167. if [ "$specified_version" = "$yarn_version" -o "$specified_version" = "$yarn_alt_version" ]; then
  168. printf "$green> Yarn is already at the $specified_version version.$reset\n"
  169. exit 0
  170. else
  171. printf "$yellow> $yarn_alt_version is already installed, Specified version: $specified_version.$reset\n"
  172. rm -rf "$HOME/.yarn"
  173. fi
  174. else
  175. printf "$red> $HOME/.yarn already exists, possibly from a past Yarn install.$reset\n"
  176. printf "$red> Remove it (rm -rf $HOME/.yarn) and run this script again.$reset\n"
  177. exit 0
  178. fi
  179. fi
  180. yarn_get_tarball $1 $2
  181. yarn_link
  182. yarn_reset
  183. }
  184. yarn_verify_or_quit() {
  185. read -p "$1 [y/N] " -n 1 -r
  186. echo
  187. if [[ ! $REPLY =~ ^[Yy]$ ]]
  188. then
  189. printf "$red> Aborting$reset\n"
  190. exit 1
  191. fi
  192. }
  193. cd ~
  194. yarn_install $1 $2

js yarn包管理组件依赖分析

Contributors (1)