class VueCli::Rails::NodeEnv

Constants

COMMAND_LINE
NODE_BIN_LIST

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 6
def initialize
  @versions = {}
  yield(self) if block_given?
end

Public Instance Methods

exec(command, args = nil, extra = nil, env: {}) click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 35
def exec(command, args = nil, extra = nil, env: {})
  cmd = COMMAND_LINE[command.to_sym] || {}
  cmd = if @pm == :yarn && cmd[:yarn]
    cmd[:yarn]
  elsif @pm == :npm && cmd[:npm]
    cmd[:npm]
  elsif cmd[:npx]
    @pm == :yarn ? "yarn exec #{cmd[:npx]}" : "npx #{cmd[:npx]}"
  else
    @pm == :yarn ? "yarn exec #{command}" : "npx #{command}"
  end

  cmd = "#{cmd} #{args}" if args.present?
  cmd = "#{cmd} #{@pm == :yarn ? '-- ' : ''}#{extra}" if extra.present?
  puts "run: #{cmd}"
  system(env, cmd)
end
method_missing(cmd, *args) click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 68
def method_missing(cmd, *args)
  exec(cmd, *args)
end
package_manager() click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 31
def package_manager
  @pm
end
reset() click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 17
def reset
  @versions = {}
end
use!(manager) click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 11
def use!(manager)
  @pm = manager.to_sym
  raise(ArgumentError, "Unsupported manager: #{@pm}") unless %i[npm yarn].include?(@pm)
  raise(ArgumentError, "Not installed: #{@pm}") unless try(:"#{@pm}?")
end

Private Instance Methods

get_version_of(bin) click to toggle source
# File lib/vue_cli/rails/node_env.rb, line 74
def get_version_of(bin)
  return @versions[bin] if @versions.key?(bin)

  r = begin
        %x`#{bin} --version`.strip.presence
      rescue StandardError
        nil
      end
  @versions[bin] = r&.start_with?('v') ? r[1..-1] : r
  @versions[bin]
end