Module: Helpers
- Defined in:
- lib/install/helpers.rb
Instance Method Summary collapse
- #add_package_json_script(name, script, run_script = true) ⇒ Object
- #bundler_cmd ⇒ Object
- #bundler_run_cmd ⇒ Object
- #bundler_x_cmd ⇒ Object
- #tool_exists?(tool) ⇒ Boolean
- #using_bun? ⇒ Boolean
Instance Method Details
#add_package_json_script(name, script, run_script = true) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/install/helpers.rb', line 26 def add_package_json_script(name, script, run_script=true) if using_bun? package_json = JSON.parse(File.read("package.json")) package_json["scripts"] ||= {} package_json["scripts"][name] = script.gsub('\\"', '"') File.write("package.json", JSON.pretty_generate(package_json)) run %(bun run #{name}) if run_script else case `npx -v`.to_f when 7.1...8.0 say "Add #{name} script" run %(npm set-script #{name} "#{script}") run %(yarn #{name}) if run_script when (8.0..) say "Add #{name} script" run %(npm pkg set scripts.#{name}="#{script}") run %(yarn #{name}) if run_script else say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green end end end |
#bundler_cmd ⇒ Object
4 5 6 |
# File 'lib/install/helpers.rb', line 4 def bundler_cmd using_bun? ? "bun" : "yarn" end |
#bundler_run_cmd ⇒ Object
8 9 10 |
# File 'lib/install/helpers.rb', line 8 def bundler_run_cmd using_bun? ? "bun run" : "yarn" end |
#bundler_x_cmd ⇒ Object
12 13 14 |
# File 'lib/install/helpers.rb', line 12 def bundler_x_cmd using_bun? ? "bunx" : "npx" end |
#tool_exists?(tool) ⇒ Boolean
22 23 24 |
# File 'lib/install/helpers.rb', line 22 def tool_exists?(tool) system "command -v #{tool} > /dev/null" end |
#using_bun? ⇒ Boolean
16 17 18 19 20 |
# File 'lib/install/helpers.rb', line 16 def using_bun? tool_exists?('bun') && (File.exist?('bun.lockb') || File.exist?('bun.lock') || File.exist?('yarn.lock')) end |