Module: Brew::Npm::CLI

Defined in:
lib/brew/npm/cli.rb

Defined Under Namespace

Classes: Arguments

Constant Summary collapse

COMMANDS =
{
  "install"   => ("Install a brew npm, accepts an optional version argument\n" +
                  "            (e.g. brew npm install <name> [version])"),
  "upgrade"   => "Upgrade to the latest version of a brew npm",
  "uninstall" => "Uninstall a brew npm",
  "reinstall" => "Reinstall a brew npm",
  "info"      => "Show information for an installed npm",
  "formula"   => "Print out the generated formula for a npm",
  "version"   => "Show version of brew-npm",
  "help"      => "This message"
}

Class Method Summary collapse

Class Method Details

.expand_formula(name, version) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/brew/npm/cli.rb', line 86

def expand_formula(name, version)
  klass           = "Npm" + name.capitalize.gsub(/[-_.\s@\/]([a-zA-Z0-9])/) { $1.upcase }.gsub('+', 'x')
  sanitized_name  = sanitize_name(name)
  user_npmrc      = "#{ENV['HOME']}/.npmrc"
  template_file   = File.expand_path("../formula.rb.erb", __FILE__)
  template        = ERB.new(File.read(template_file))
  template.result(binding) # TODO: avoid binding. specify variables.
end

.fetch_version(name, version = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/brew/npm/cli.rb', line 49

def fetch_version(name, version = nil)
  npm_info = parse_json `npm info --json "#{name}"`
  abort "Could not find a valid npm '#{name}'" if npm_info.nil?
  version ||= npm_info["version"]
  version
end

.help_msgObject



44
45
46
47
# File 'lib/brew/npm/cli.rb', line 44

def help_msg
  (["Please specify a npm package name (e.g. brew npm command <name>)"] +
    COMMANDS.map {|name, desc| "  #{name} - #{desc}"}).join("\n")
end

.homebrew_prefixObject



82
83
84
# File 'lib/brew/npm/cli.rb', line 82

def homebrew_prefix
  ENV["HOMEBREW_PREFIX"] || `brew --prefix`.chomp
end

.parse_json(str) ⇒ Object



56
57
58
59
60
61
# File 'lib/brew/npm/cli.rb', line 56

def parse_json(str)
  JSON.parse(str)
rescue JSON::ParserError => err
  STDERR.puts err
  nil
end

.process_args(args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/brew/npm/cli.rb', line 63

def process_args(args)
  arguments = Arguments.new(args)
  command   = arguments.command
  abort help_msg unless command
  abort "unknown command: #{command}\n#{help_msg}" unless COMMANDS.keys.include?(command)

  if command == 'help'
    STDERR.puts help_msg
    exit 0
  end

  if command == 'version'
    STDERR.puts Brew::Npm::VERSION
    exit 0
  end

  arguments
end

.run(args = ARGV) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/brew/npm/cli.rb', line 108

def run(args = ARGV)
  arguments = process_args(args)
  name      = arguments.npm
  if name.nil?
    STDERR.puts 'Package name is missing!'
    STDERR.puts help_msg
    exit 0
  end
  version   = fetch_version(name, arguments.supplied_version)
  with_temp_formula(name, version) do |filename|
    case arguments.command
    when "formula"
      $stdout.puts File.read(filename)
    else
      system "brew #{arguments.to_args.shelljoin} --formula #{filename}"
      exit $?.exitstatus unless $?.success?
    end
  end
end

.sanitize_name(name) ⇒ Object



128
129
130
# File 'lib/brew/npm/cli.rb', line 128

def sanitize_name(name)
  name.gsub(%r{\A@}, '').gsub('/', '-') # for Scoped packages. https://docs.npmjs.com/cli/v8/using-npm/scope
end

.with_temp_formula(name, version) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/brew/npm/cli.rb', line 95

def with_temp_formula(name, version)
  sanitized_name = sanitize_name(name)
  filename = File.join Dir.tmpdir, "npm-#{sanitized_name}.rb"

  open(filename, "w") do |f|
    f.puts expand_formula(name, version)
  end

  yield filename
ensure
  File.unlink filename
end