From 27e8fd8df8d9d5f8b58dc91d710a50d7734f15a4 Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Sun, 9 Mar 2025 19:46:47 +0100 Subject: [PATCH 1/2] Simplification --- lib/install/bootstrap/install.rb | 2 +- lib/install/helpers.rb | 88 ++++++++++++++++++++++++-------- lib/tasks/cssbundling/build.rake | 6 +-- 3 files changed, 71 insertions(+), 25 deletions(-) diff --git a/lib/install/bootstrap/install.rb b/lib/install/bootstrap/install.rb index f712775..b1b82cd 100644 --- a/lib/install/bootstrap/install.rb +++ b/lib/install/bootstrap/install.rb @@ -37,7 +37,7 @@ add_package_json_script("build:css:compile", "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules") add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css") add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix") -add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \\\"#{bundler_run_cmd} build:css\\\"", false) +add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \\\"#{bundler_run_cmd} build:css\\\"", run_script: false) gsub_file "Procfile.dev", "build:css --watch", "watch:css" diff --git a/lib/install/helpers.rb b/lib/install/helpers.rb index cf5b612..4e5860a 100644 --- a/lib/install/helpers.rb +++ b/lib/install/helpers.rb @@ -1,48 +1,94 @@ +# frozen_string_literal: true + require 'json' module Helpers + TOOLS_COMMANDS = { + yarn: { cmd: 'yarn', run: 'yarn', x: 'npx' }, + bun: { cmd: 'bun', run: 'bun run', x: 'bunx' }, + pnpm: { cmd: 'pnpm', run: 'pnpm run', x: 'pnpx' }, + npm: { cmd: 'npm', run: 'npm run', x: 'npx' }, + }.freeze + SUPPORTED_TOOLS = TOOLS_COMMANDS.keys.freeze + DEFAULT_TOOL = :yarn + def bundler_cmd - using_bun? ? "bun" : "yarn" + TOOLS_COMMANDS.dig(package_manager, :cmd) end def bundler_run_cmd - using_bun? ? "bun run" : "yarn" + TOOLS_COMMANDS.dig(package_manager, :run) end def bundler_x_cmd - using_bun? ? "bunx" : "npx" + TOOLS_COMMANDS.dig(package_manager, :x) end def using_bun? - tool_exists?('bun') && (File.exist?('bun.lockb') || - File.exist?('bun.lock') || - File.exist?('yarn.lock')) + package_manager == :bun end - def tool_exists?(tool) - system "command -v #{tool} > /dev/null" + def package_manager + @package_manager ||= tool_determined_by_config_file || tool_determined_by_executable || DEFAULT_TOOL 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.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 + def add_package_json_script(name, script, run_script: true) + case package_manager + when :yarn + npx_version = `npx -v`.to_f + + if npx_version >= 7.1 && npx_version < 8.0 say "Add #{name} script" run %(npm set-script #{name} "#{script}") - run %(yarn #{name}) if run_script - when (8.0..) + elsif npx_version >= 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 + return end + when :npm + say "Add #{name} script" + npx_version = `npx -v`.to_f + + if npx_version >= 7.1 && npx_version < 8.0 + run %(npm set-script #{name} "#{script}") + else + run %(npm pkg set scripts.#{name}="#{script}") + end + when :pnpm + say "Add #{name} script" + run %(pnpm pkg set scripts.#{name}="#{script}") + when :bun + say "Add #{name} script to package.json manually" + 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)) + end + + run %(#{bundler_run_cmd} #{name}) if run_script + end + + private + + def tool_exists?(tool) + system "command -v #{tool} > /dev/null" + end + + def tool_determined_by_config_file + case + when File.exist?("yarn.lock") then :yarn + when File.exist?("bun.lockb") then :bun + when File.exist?("bun.lock") then :bun + when File.exist?("pnpm-lock.yaml") then :pnpm + when File.exist?("package-lock.json") then :npm + end + end + + def tool_determined_by_executable + SUPPORTED_TOOLS.each do |tool| + return tool if tool_exists?(tool) end end end diff --git a/lib/tasks/cssbundling/build.rake b/lib/tasks/cssbundling/build.rake index ddce5ab..ed4ff33 100644 --- a/lib/tasks/cssbundling/build.rake +++ b/lib/tasks/cssbundling/build.rake @@ -22,16 +22,16 @@ module Cssbundling extend self LOCK_FILES = { - bun: %w[bun.lockb bun.lock yarn.lock], yarn: %w[yarn.lock], + bun: %w[bun.lockb bun.lock], pnpm: %w[pnpm-lock.yaml], npm: %w[package-lock.json] } def install_command case - when using_tool?(:bun) then "bun install" when using_tool?(:yarn) then "yarn install" + when using_tool?(:bun) then "bun install" when using_tool?(:pnpm) then "pnpm install" when using_tool?(:npm) then "npm install" else raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies" @@ -40,8 +40,8 @@ module Cssbundling def build_command case - when using_tool?(:bun) then "bun run build:css" when using_tool?(:yarn) then "yarn build:css" + when using_tool?(:bun) then "bun run build:css" when using_tool?(:pnpm) then "pnpm build:css" when using_tool?(:npm) then "npm run build:css" else raise "cssbundling-rails: No suitable tool found for building CSS" From 596456ac568b27022cd08fe4a3b7de30fad392be Mon Sep 17 00:00:00 2001 From: Navid EMAD Date: Tue, 11 Mar 2025 17:59:01 +0100 Subject: [PATCH 2/2] Bun lockfiles should come before yarn.lock, since Bun optionally generates a yarn.lock file --- lib/install/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/install/helpers.rb b/lib/install/helpers.rb index 4e5860a..f89e0a6 100644 --- a/lib/install/helpers.rb +++ b/lib/install/helpers.rb @@ -78,9 +78,9 @@ def tool_exists?(tool) def tool_determined_by_config_file case - when File.exist?("yarn.lock") then :yarn when File.exist?("bun.lockb") then :bun when File.exist?("bun.lock") 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 end