Skip to content

Commit

Permalink
Merge pull request #180 from ksylvest/ksylvest/npm-build-tool-selecti…
Browse files Browse the repository at this point in the history
…on-fix

Simplify build / install tool (bun / yarn / npm) logic
  • Loading branch information
rafaelfranca committed Jan 8, 2024
2 parents ff78e2b + 092ef9f commit 44919fe
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/tasks/jsbundling/build.rake
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,37 @@ module Jsbundling
extend self

def install_command
return "bun install" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
return "yarn install" if File.exist?('yarn.lock') || (tool_exists?('yarn') && !File.exist?('package-lock.json'))
return "npm install" if File.exist?('package-lock.json') || tool_exists?('npm')
raise "jsbundling-rails: No suitable tool found for installing JavaScript dependencies"
case tool
when :bun then "bun install"
when :yarn then "yarn install"
when :npm then "npm install"
else raise "jsbundling-rails: No suitable tool found for installing JavaScript dependencies"
end
end

def build_command
return "bun run build" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
return "yarn build" if File.exist?('yarn.lock') || (tool_exists?('yarn') && !File.exist?('package-lock.json'))
return "npm run build" if File.exist?('package-lock.json') || tool_exists?('npm')
raise "jsbundling-rails: No suitable tool found for building JavaScript"
case tool
when :bun then "bun run build"
when :yarn then "yarn build"
when :npm then "npm run build"
else raise "jsbundling-rails: No suitable tool found for building JavaScript"
end
end

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

def tool
case
when File.exist?('bun.lockb') then :bun
when File.exist?('yarn.lock') then :yarn
when File.exist?('package-lock.json') then :npm
when tool_exists?('bun') then :bun
when tool_exists?('yarn') then :yarn
when tool_exists?('npm') then :npm
end
end
end
end

Expand Down

0 comments on commit 44919fe

Please sign in to comment.