react native 相关知识

RN - mac OS 10.14配置开发环境

2018-11-02  本文已影响300人  捕梦少女的梦想

安装环境:

Mac os X 10.14

必须安装的软件 : Home-brew & Node

推荐安装的工具:Watchman & Flow

Home-brew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

打开终端命令,输入以上的代码,中间会需要按一次return,输入一次开机密码。

xcode-select: error: invalid developer directory '/Library/Developer/CommandLineTools'
Failed during: /usr/bin/sudo /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools
#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere (which is
# unsupported) you can untar https://github.com/Homebrew/brew/tarball/master
# anywhere you like.
HOMEBREW_PREFIX = "/usr/local".freeze
HOMEBREW_REPOSITORY = "/usr/local/Homebrew".freeze
HOMEBREW_CACHE = "#{ENV["HOME"]}/Library/Caches/Homebrew".freeze
HOMEBREW_OLD_CACHE = "/Library/Caches/Homebrew".freeze
BREW_REPO = "https://github.com/Homebrew/brew".freeze
CORE_TAP_REPO = "https://github.com/Homebrew/homebrew-core".freeze
 
# TODO: bump version when new macOS is released
MACOS_LATEST_SUPPORTED = "10.13".freeze
# TODO: bump version when new macOS is released
MACOS_OLDEST_SUPPORTED = "10.11".freeze
 
# no analytics during installation
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
ENV["HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT"] = "1"
 
module Tty
  module_function
 
  def blue
    bold 34
  end
 
  def red
    bold 31
  end
 
  def reset
    escape 0
  end
 
  def bold(n = 39)
    escape "1;#{n}"
  end
 
  def underline
    escape "4;39"
  end
 
  def escape(n)
    "\033[#{n}m" if STDOUT.tty?
  end
end
 
class Array
  def shell_s
    cp = dup
    first = cp.shift
    cp.map { |arg| arg.gsub " ", "\\ " }.unshift(first).join(" ")
  end
end
 
def ohai(*args)
  puts "#{Tty.blue}==>#{Tty.bold} #{args.shell_s}#{Tty.reset}"
end
 
def warn(warning)
  puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
end
 
def system(*args)
  abort "Failed during: #{args.shell_s}" unless Kernel.system(*args)
end
 
def sudo(*args)
  args.unshift("-A") unless ENV["SUDO_ASKPASS"].nil?
  ohai "/usr/bin/sudo", *args
  system "/usr/bin/sudo", *args
end
 
def getc
  system "/bin/stty raw -echo"
  if STDIN.respond_to?(:getbyte)
    STDIN.getbyte
  else
    STDIN.getc
  end
ensure
  system "/bin/stty -raw echo"
end
 
def wait_for_user
  puts
  puts "Press RETURN to continue or any other key to abort"
  c = getc
  # we test for \r and \n because some stuff does \r instead
  abort unless (c == 13) || (c == 10)
end
 
class Version
  include Comparable
  attr_reader :parts
 
  def initialize(str)
    @parts = str.split(".").map { |p| p.to_i }
  end
 
  def <=>(other)
    parts <=> self.class.new(other).parts
  end
end
 
def force_curl?
  ARGV.include?("--force-curl")
end
 
def macos_version
  @macos_version ||= Version.new(`/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/])
end
 
def should_install_command_line_tools?
  return false if force_curl?
  return false if macos_version < "10.9"
  if macos_version > "10.13"
    !File.exist?("/Library/Developer/CommandLineTools/usr/bin/git")
  else
    !File.exist?("/Library/Developer/CommandLineTools/usr/bin/git") ||
      !File.exist?("/usr/include/iconv.h")
  end
end
 
def git
  return false if force_curl?
 
  @git ||= if ENV["GIT"] && File.executable?(ENV["GIT"])
    ENV["GIT"]
  elsif Kernel.system "/usr/bin/which -s git"
    "git"
  else
    exe = `xcrun -find git 2>/dev/null`.chomp
    exe if $? && $?.success? && !exe.empty? && File.executable?(exe)
  end
 
  return unless @git
  # Github only supports HTTPS fetches on 1.7.10 or later:
  # https://help.github.com/articles/https-cloning-errors
  `#{@git} --version` =~ /git version (\d\.\d+\.\d+)/
  return if $1.nil?
  return if Version.new($1) < "1.7.10"
 
  @git
end
 
def user_only_chmod?(d)
  return false unless File.directory?(d)
  mode = File.stat(d).mode & 0777
  # u = (mode >> 6) & 07
  # g = (mode >> 3) & 07
  # o = (mode >> 0) & 07
  mode != 0755
end
 
def chmod?(d)
  File.exist?(d) && !(File.readable?(d) && File.writable?(d) && File.executable?(d))
end
 
def chown?(d)
  !File.owned?(d)
end
 
def chgrp?(d)
  !File.grpowned?(d)
end
 
# Invalidate sudo timestamp before exiting (if it wasn't active before).
Kernel.system "/usr/bin/sudo -n -v 2>/dev/null"
at_exit { Kernel.system "/usr/bin/sudo", "-k" } unless $?.success?
 
# The block form of Dir.chdir fails later if Dir.CWD doesn't exist which I
# guess is fair enough. Also sudo prints a warning message for no good reason
Dir.chdir "/usr"
 
####################################################################### script
abort "See Linuxbrew: http://linuxbrew.sh/" if RUBY_PLATFORM.to_s.downcase.include?("linux")
abort "Mac OS X too old, see: https://github.com/mistydemeo/tigerbrew" if macos_version < "10.5"
abort "Don't run this as root!" if Process.uid.zero?
abort <<-EOABORT unless `dsmemberutil checkmembership -U "#{ENV["USER"]}" -G admin`.include? "user is a member"
This script requires the user #{ENV["USER"]} to be an Administrator.
EOABORT
# Tests will fail if the prefix exists, but we don't have execution
# permissions. Abort in this case.
abort <<-EOABORT if File.directory?(HOMEBREW_PREFIX) && (!File.executable? HOMEBREW_PREFIX)
The Homebrew prefix, #{HOMEBREW_PREFIX}, exists but is not searchable. If this is
not intentional, please restore the default permissions and try running the
installer again:
    sudo chmod 775 #{HOMEBREW_PREFIX}
EOABORT
 
# TODO: bump version when new macOS is released
if macos_version > MACOS_LATEST_SUPPORTED ||
   macos_version < MACOS_OLDEST_SUPPORTED
 
  who = "We"
  if macos_version > MACOS_LATEST_SUPPORTED
    what = "pre-release version"
  elsif macos_version < MACOS_OLDEST_SUPPORTED
    who << " (and Apple)"
    what = "old version"
  else
    return
  end
  ohai "You are using macOS #{macos_version.parts.join(".")}."
  ohai "#{who} do not provide support for this #{what}."
 
  puts <<-EOS
This installation may not succeed.
After installation, you will encounter build failures and other breakages.
Please create pull-requests instead of asking for help on Homebrew's
GitHub, Discourse, Twitter or IRC. As you are running this #{what},
you are responsible for resolving any issues you experience.
 
  EOS
end
 
ohai "This script will install:"
puts "#{HOMEBREW_PREFIX}/bin/brew"
puts "#{HOMEBREW_PREFIX}/share/doc/homebrew"
puts "#{HOMEBREW_PREFIX}/share/man/man1/brew.1"
puts "#{HOMEBREW_PREFIX}/share/zsh/site-functions/_brew"
puts "#{HOMEBREW_PREFIX}/etc/bash_completion.d/brew"
puts HOMEBREW_REPOSITORY.to_s
 
group_chmods = %w[ bin bin/brew etc Frameworks include lib sbin share var
                   etc/bash_completion.d lib/pkgconfig var/log
                   share/aclocal share/doc share/info share/locale share/man
                   share/man/man1 share/man/man2 share/man/man3 share/man/man4
                   share/man/man5 share/man/man6 share/man/man7 share/man/man8].
               map { |d| File.join(HOMEBREW_PREFIX, d) }.
               select { |d| chmod?(d) }
# zsh refuses to read from these directories if group writable
zsh_dirs = %w[share/zsh share/zsh/site-functions].
           map { |d| File.join(HOMEBREW_PREFIX, d) }
user_chmods = zsh_dirs.select { |d| user_only_chmod?(d) }
chmods = group_chmods + user_chmods
chowns = chmods.select { |d| chown?(d) }
chgrps = chmods.select { |d| chgrp?(d) }
mkdirs = %w[Cellar Homebrew Frameworks bin etc include lib opt sbin share share/zsh share/zsh/site-functions var].
         map { |d| File.join(HOMEBREW_PREFIX, d) }.
         reject { |d| File.directory?(d) }
 
unless group_chmods.empty?
  ohai "The following existing directories will be made group writable:"
  puts(*group_chmods)
end
unless user_chmods.empty?
  ohai "The following existing directories will be made writable by user only:"
  puts(*user_chmods)
end
unless chowns.empty?
  ohai "The following existing directories will have their owner set to #{Tty.underline}#{ENV["USER"]}#{Tty.reset}:"
  puts(*chowns)
end
unless chgrps.empty?
  ohai "The following existing directories will have their group set to #{Tty.underline}admin#{Tty.reset}:"
  puts(*chgrps)
end
unless mkdirs.empty?
  ohai "The following new directories will be created:"
  puts(*mkdirs)
end
if should_install_command_line_tools?
  ohai "The Xcode Command Line Tools will be installed."
end
 
wait_for_user if STDIN.tty? && !ENV["TRAVIS"]
 
if File.directory? HOMEBREW_PREFIX
  sudo "/bin/chmod", "u+rwx", *chmods unless chmods.empty?
  sudo "/bin/chmod", "g+rwx", *group_chmods unless group_chmods.empty?
  sudo "/bin/chmod", "755", *user_chmods unless user_chmods.empty?
  sudo "/usr/sbin/chown", ENV["USER"], *chowns unless chowns.empty?
  sudo "/usr/bin/chgrp", "admin", *chgrps unless chgrps.empty?
else
  sudo "/bin/mkdir", "-p", HOMEBREW_PREFIX
  sudo "/usr/sbin/chown", "root:wheel", HOMEBREW_PREFIX
end
 
unless mkdirs.empty?
  sudo "/bin/mkdir", "-p", *mkdirs
  sudo "/bin/chmod", "g+rwx", *mkdirs
  sudo "/bin/chmod", "755", *zsh_dirs
  sudo "/usr/sbin/chown", ENV["USER"], *mkdirs
  sudo "/usr/bin/chgrp", "admin", *mkdirs
end
 
[HOMEBREW_CACHE, HOMEBREW_OLD_CACHE].each do |cache|
  sudo "/bin/mkdir", "-p", cache unless File.directory? cache
  sudo "/bin/chmod", "g+rwx", cache if chmod? cache
  sudo "/usr/sbin/chown", ENV["USER"], cache if chown? cache
  sudo "/usr/bin/chgrp", "admin", cache if chgrp? cache
end
 
if should_install_command_line_tools?
  ohai "Searching online for the Command Line Tools"
  # This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools
  clt_placeholder = "/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
  sudo "/usr/bin/touch", clt_placeholder
  clt_label = `/usr/sbin/softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | tail -n1`.chomp
  ohai "Installing #{clt_label}"
  sudo "/usr/sbin/softwareupdate", "-i", clt_label
  sudo "/bin/rm", "-f", clt_placeholder
  sudo "/usr/bin/xcode-select", "--switch", "/Applications/Xcode.app/Contents/Developer"
end
 
# Headless install may have failed, so fallback to original 'xcode-select' method
if should_install_command_line_tools? && STDIN.tty?
  ohai "Installing the Command Line Tools (expect a GUI popup):"
  sudo "/usr/bin/xcode-select", "--install"
  puts "Press any key when the installation has completed."
  getc
  sudo "/usr/bin/xcode-select", "--switch", "/Applications/Xcode.app/Contents/Developer"
end
 
abort <<-EOABORT if `/usr/bin/xcrun clang 2>&1` =~ /license/ && !$?.success?
You have not agreed to the Xcode license.
Before running the installer again please agree to the license by opening
Xcode.app or running:
    sudo xcodebuild -license
EOABORT
 
ohai "Downloading and installing Homebrew..."
Dir.chdir HOMEBREW_REPOSITORY do
  if git
    # we do it in four steps to avoid merge errors when reinstalling
    system git, "init", "-q"
 
    # "git remote add" will fail if the remote is defined in the global config
    system git, "config", "remote.origin.url", BREW_REPO
    system git, "config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"
 
    # ensure we don't munge line endings on checkout
    system git, "config", "core.autocrlf", "false"
 
    args = git, "fetch", "origin", "master:refs/remotes/origin/master",
           "--tags", "--force"
    system(*args)
 
    system git, "reset", "--hard", "origin/master"
 
    system "ln", "-sf", "#{HOMEBREW_REPOSITORY}/bin/brew", "#{HOMEBREW_PREFIX}/bin/brew"
 
    system "#{HOMEBREW_PREFIX}/bin/brew", "update", "--force"
  else
    # -m to stop tar erroring out if it can't modify the mtime for root owned directories
    # pipefail to cause the exit status from curl to propagate if it fails
    curl_flags = "fsSL"
    curl_flags += "k" if macos_version < "10.6"
    core_tap = "#{HOMEBREW_PREFIX}/Homebrew/Library/Taps/homebrew/homebrew-core"
    system "/bin/bash -o pipefail -c '/usr/bin/curl -#{curl_flags} #{BREW_REPO}/tarball/master | /usr/bin/tar xz -m --strip 1'"
 
    system "ln", "-sf", "#{HOMEBREW_REPOSITORY}/bin/brew", "#{HOMEBREW_PREFIX}/bin/brew"
 
    system "/bin/mkdir", "-p", core_tap
    Dir.chdir core_tap do
      system "/bin/bash -o pipefail -c '/usr/bin/curl -#{curl_flags} #{CORE_TAP_REPO}/tarball/master | /usr/bin/tar xz -m --strip 1'"
    end
  end
end
 
warn "#{HOMEBREW_PREFIX}/bin is not in your PATH." unless ENV["PATH"].split(":").include? "#{HOMEBREW_PREFIX}/bin"
 
ohai "Installation successful!"
puts
 
# Use the shell's audible bell.
print "\a"
 
# Use an extra newline and bold to avoid this being missed.
ohai "Homebrew has enabled anonymous aggregate formulae and cask analytics."
puts <<-EOS
#{Tty.bold}Read the analytics documentation (and how to opt-out) here:
  #{Tty.underline}https://docs.brew.sh/Analytics.html#{Tty.reset}
 
EOS
 
ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:"
puts <<-EOS
  #{Tty.underline}https://github.com/Homebrew/brew#donations#{Tty.reset}
EOS
 
if git
  Dir.chdir HOMEBREW_REPOSITORY do
    system git, "config", "--local", "--replace-all", "homebrew.analyticsmessage", "true"
    system git, "config", "--local", "--replace-all", "homebrew.caskanalyticsmessage", "true"
  end
end
 
ohai "Next steps:"
 
if macos_version < "10.9" && macos_version > "10.6"
  `/usr/bin/cc --version 2> /dev/null` =~ /clang-(\d{2,})/
  version = $1.to_i
  if version < 425
    puts "- Install the #{Tty.bold}Command Line Tools for Xcode:"
    puts "    #{Tty.underline}https://developer.apple.com/downloads#{Tty.reset}"
  end
elsif !File.exist? "/usr/bin/cc"
  puts "- Install #{Tty.bold}Xcode:"
  puts "    #{Tty.underline}https://developer.apple.com/xcode#{Tty.reset}"
end
 
unless git
  puts "- Run `brew update --force` to complete installation by installing:"
  puts "    #{HOMEBREW_PREFIX}/share/doc/homebrew"
  puts "    #{HOMEBREW_PREFIX}/share/man/man1/brew.1"
  puts "    #{HOMEBREW_PREFIX}/share/zsh/site-functions/_brew"
  puts "    #{HOMEBREW_PREFIX}/etc/bash_completion.d/brew"
  puts "    #{HOMEBREW_REPOSITORY}/.git"
end
 
puts "- Run `brew help` to get started"
puts "- Further documentation: "
puts "    #{Tty.underline}https://docs.brew.sh#{Tty.reset}"
# --------------------- 
# 作者:survivorsfyh 
# 来源:CSDN 
# 原文:https://blog.csdn.net/survivorsfyh/article/details/82314261 
# 版权声明:本文为博主原创文章,转载请附上博文链接!
  1. 用浏览器打开 https://raw.githubusercontent.com/Homebrew/install/master/install ,可以看到文件内容。
  2. 修改其安装 Homebrew 的脚本 code 指令后,越过"/Library/Developer/CommandLineTools/" 验证这一环节。

使用 brew 可以查看是否有日志,检查安装是否成功。

Node

使用 Homebrew 来安装Node.js

译注:在 Max OS X 10.11(El Capitan)版本中,homebrew 在安装软件时可能会碰到/usr/local目录不可写的权限问题。可以使用下面的命令修复:

sudo chown -R `whoami` /usr/local

安装完 node 后建议设置 npm 镜像以加速后面的过程(或使用科学上网工具)。注意:不要使用 cnpm!cnpm 安装的模块路径比较奇怪,packager 不能正常识别!

npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global

Yarn、React Native的命令行工具(react-native-cli)

Yarn是Facebook提供的替代npm的工具,可以加速node模块的下载。

npm install -g yarn react-native-cli

安装完 yarn 后同理也要设置镜像源:

yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global

如果你看到EACCES: permission denied这样的权限报错,那么请参照上文的 homebrew 译注,修复/usr/local目录的所有权:

sudo chown -R `whoami` /usr/local

安装完 yarn 之后就可以用 yarn 代替 npm 了,例如用yarn代替npm install命令,用yarn add 某第三方库名代替npm install --save 某第三方库名。

推荐安装的工具:

Watchman

Watchman是由 Facebook 提供的监视文件系统变更的工具。安装此工具可以提高开发时的性能(packager 可以快速捕捉文件的变化从而实现实时刷新)。译注:此工具官方虽然是推荐安装,但在实践中,我们认为此工具是必须安装,否则可能无法正常开发。

brew install watchman

Flow

Flow是一个静态的 JS 类型检查工具。译注:你在很多示例中看到的奇奇怪怪的冒号问号,以及方法参数中像类型一样的写法,都是属于这个 flow 工具的语法。这一语法并不属于 ES 标准,只是 Facebook 自家的代码规范。所以新手可以直接跳过(即不需要安装这一工具,也不建议去费力学习 flow 相关语法)。

brew install flow

参考链接

https://reactnative.cn/docs/0.51/getting-started/
http://www.cnblogs.com/ctaixw/p/9523848.html
https://blog.csdn.net/survivorsfyh/article/details/82314261

上一篇下一篇

猜你喜欢

热点阅读