Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for overriding build_command #203

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/jsbundling/tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Jsbundling
module Tasks
extend self

attr_writer :build_command

def install_command
case tool
when :bun then "bun install"
when :yarn then "yarn install"
when :pnpm then "pnpm install"
when :npm then "npm install"
else raise "jsbundling-rails: No suitable tool found for installing JavaScript dependencies"
end
end

def build_command
if @build_command
return @build_command
end

case tool
when :bun then "bun run build"
when :yarn then "yarn build"
when :pnpm then "pnpm 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?('pnpm-lock.yaml') then :pnpm
when File.exist?('package-lock.json') then :npm
when tool_exists?('bun') then :bun
when tool_exists?('yarn') then :yarn
when tool_exists?('pnpm') then :pnpm
when tool_exists?('npm') then :npm
end
end
end
end
45 changes: 2 additions & 43 deletions lib/tasks/jsbundling/build.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "jsbundling/tasks"

namespace :javascript do
desc "Install JavaScript dependencies"
task :install do
Expand All @@ -18,49 +20,6 @@ namespace :javascript do
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
end

module Jsbundling
module Tasks
extend self

def install_command
case tool
when :bun then "bun install"
when :yarn then "yarn install"
when :pnpm then "pnpm install"
when :npm then "npm install"
else raise "jsbundling-rails: No suitable tool found for installing JavaScript dependencies"
end
end

def build_command
case tool
when :bun then "bun run build"
when :yarn then "yarn build"
when :pnpm then "pnpm 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?('pnpm-lock.yaml') then :pnpm
when File.exist?('package-lock.json') then :npm
when tool_exists?('bun') then :bun
when tool_exists?('yarn') then :yarn
when tool_exists?('pnpm') then :pnpm
when tool_exists?('npm') then :npm
end
end
end
end

unless ENV["SKIP_JS_BUILD"]
if Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance(["javascript:build"])
Expand Down
15 changes: 15 additions & 0 deletions test/tasks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "test_helper"
require "jsbundling/tasks"

class TasksTest < ActiveSupport::TestCase
test "override build_command" do
original_build_command = Jsbundling::Tasks.build_command
Jsbundling::Tasks.build_command = "hello there"
assert_equal("hello there", Jsbundling::Tasks.build_command)

Jsbundling::Tasks.build_command = nil
assert_equal(original_build_command, Jsbundling::Tasks.build_command)
ensure
Jsbundling::Tasks.build_command = nil
end
end
Loading