Module: Nextgen::Actions::Javascript
- Included in:
- Nextgen::Actions
- Defined in:
- lib/nextgen/actions/javascript.rb
Instance Method Summary collapse
- #add_js_packages(*packages, dev: false) ⇒ Object (also: #add_js_package)
- #add_package_json_scripts(scripts) ⇒ Object (also: #add_package_json_script)
- #js_package_manager ⇒ Object
- #remove_js_packages(*packages, capture: false) ⇒ Object (also: #remove_js_package)
- #remove_package_json_script(name) ⇒ Object
- #run_js_command(cmd, capture: false) ⇒ Object
- #yarn? ⇒ Boolean
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" 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 25 |
# File 'lib/nextgen/actions/javascript.rb', line 18 def add_package_json_scripts(scripts) scripts.each do |name, script| script = script.sub(/^yarn run /, "npm run -- ") unless yarn? 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_manager ⇒ Object
34 35 36 |
# File 'lib/nextgen/actions/javascript.rb', line 34 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
28 29 30 31 32 |
# File 'lib/nextgen/actions/javascript.rb', line 28 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
42 43 44 45 46 47 48 49 |
# File 'lib/nextgen/actions/javascript.rb', line 42 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
38 39 40 |
# File 'lib/nextgen/actions/javascript.rb', line 38 def yarn? js_package_manager == :yarn end |