Module: Nextgen::Actions::Javascript

Included in:
Nextgen::Actions
Defined in:
lib/nextgen/actions/javascript.rb

Instance Method Summary collapse

Instance Method Details

#add_js_packages(*packages, dev: false) ⇒ Object Also known as: add_js_package



5
6
7
8
9
# File 'lib/nextgen/actions/javascript.rb', line 5

def add_js_packages(*packages, dev: false)
  command = yarn? ? +"yarn add" : +"npm install --fund=false --audit-false"
  command << " -D" if dev
  run_js_command "#{command} #{packages.map(&:shellescape).join(" ")}"
end

#add_package_json_scripts(scripts) ⇒ Object Also known as: add_package_json_script



18
19
20
21
22
23
24
# File 'lib/nextgen/actions/javascript.rb', line 18

def add_package_json_scripts(scripts)
  scripts.each do |name, script|
    cmd = "npm pkg set scripts.#{name.to_s.shellescape}=#{script.shellescape}"
    say_status :run, cmd.truncate(60), :green
    run! cmd, verbose: false
  end
end

#js_package_managerObject



33
34
35
# File 'lib/nextgen/actions/javascript.rb', line 33

def js_package_manager
  File.exist?("yarn.lock") ? :yarn : :npm
end

#remove_js_packages(*packages, capture: false) ⇒ Object Also known as: remove_js_package



12
13
14
15
# File 'lib/nextgen/actions/javascript.rb', line 12

def remove_js_packages(*packages, capture: false)
  command = yarn? ? "yarn remove" : "npm uninstall"
  run_js_command "#{command} #{packages.map(&:shellescape).join(" ")}", capture:
end

#remove_package_json_script(name) ⇒ Object



27
28
29
30
31
# File 'lib/nextgen/actions/javascript.rb', line 27

def remove_package_json_script(name)
  cmd = "npm pkg delete scripts.#{name.to_s.shellescape}"
  say_status :run, cmd.truncate(60), :green
  run! cmd, verbose: false
end

#run_js_command(cmd, capture: false) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/nextgen/actions/javascript.rb', line 41

def run_js_command(cmd, capture: false)
  say_status(*cmd.split(" ", 2), :green)
  output = run! cmd, capture: true, verbose: false
  return output if capture
  return puts(output) unless output.match?(/^success /)

  puts output.lines.grep(/^(warning|success) /).join
end

#yarn?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/nextgen/actions/javascript.rb', line 37

def yarn?
  js_package_manager == :yarn
end