Class: VueCli::Rails::NodeEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/vue_cli/rails/node_env.rb

Constant Summary collapse

NODE_BIN_LIST =
%i[node yarn npm npx vue].freeze
COMMAND_LINE =
{
  add: {
    yarn: 'yarn add',
    npm: 'npm i',
  },
  global_add: {
    yarn: 'yarn global add',
    npm: 'npm i -g',
  },
  install: {
    yarn: 'yarn',
    npm: 'npm i',
  },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ NodeEnv

Returns a new instance of NodeEnv.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
# File 'lib/vue_cli/rails/node_env.rb', line 6

def initialize
  @versions = {}
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(cmd, *args) ⇒ Object



68
69
70
# File 'lib/vue_cli/rails/node_env.rb', line 68

def method_missing(cmd, *args)
  exec(cmd, *args)
end

Instance Method Details

#exec(command, args = nil, extra = nil, env: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#package_managerObject



31
32
33
# File 'lib/vue_cli/rails/node_env.rb', line 31

def package_manager
  @pm
end

#resetObject



17
18
19
# File 'lib/vue_cli/rails/node_env.rb', line 17

def reset
  @versions = {}
end

#use!(manager) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
# 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