Sha256: 375230d231e3bdad68db21e4ce03bff9ee46aa368f7827fc9d6bc644c6421824

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

require 'json'

module Helpers
  def bundler_cmd
    using_bun? ? "bun" : "yarn"
  end

  def bundler_run_cmd
    using_bun? ? "bun run" : "yarn"
  end

  def using_bun?
    File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
  end

  def tool_exists?(tool)
    system "command -v #{tool} > /dev/null"
  end

  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
      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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cssbundling-rails-1.3.3 lib/install/helpers.rb
cssbundling-rails-1.3.2 lib/install/helpers.rb
cssbundling-rails-1.3.1 lib/install/helpers.rb
cssbundling-rails-1.3.0 lib/install/helpers.rb