Skip to content

Commit 27e8fd8

Browse files
committed
Simplification
1 parent 8eca54b commit 27e8fd8

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

Diff for: lib/install/bootstrap/install.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
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")
3838
add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
3939
add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix")
40-
add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \\\"#{bundler_run_cmd} build:css\\\"", false)
40+
add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \\\"#{bundler_run_cmd} build:css\\\"", run_script: false)
4141

4242
gsub_file "Procfile.dev", "build:css --watch", "watch:css"
4343

Diff for: lib/install/helpers.rb

+67-21
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,94 @@
1+
# frozen_string_literal: true
2+
13
require 'json'
24

35
module Helpers
6+
TOOLS_COMMANDS = {
7+
yarn: { cmd: 'yarn', run: 'yarn', x: 'npx' },
8+
bun: { cmd: 'bun', run: 'bun run', x: 'bunx' },
9+
pnpm: { cmd: 'pnpm', run: 'pnpm run', x: 'pnpx' },
10+
npm: { cmd: 'npm', run: 'npm run', x: 'npx' },
11+
}.freeze
12+
SUPPORTED_TOOLS = TOOLS_COMMANDS.keys.freeze
13+
DEFAULT_TOOL = :yarn
14+
415
def bundler_cmd
5-
using_bun? ? "bun" : "yarn"
16+
TOOLS_COMMANDS.dig(package_manager, :cmd)
617
end
718

819
def bundler_run_cmd
9-
using_bun? ? "bun run" : "yarn"
20+
TOOLS_COMMANDS.dig(package_manager, :run)
1021
end
1122

1223
def bundler_x_cmd
13-
using_bun? ? "bunx" : "npx"
24+
TOOLS_COMMANDS.dig(package_manager, :x)
1425
end
1526

1627
def using_bun?
17-
tool_exists?('bun') && (File.exist?('bun.lockb') ||
18-
File.exist?('bun.lock') ||
19-
File.exist?('yarn.lock'))
28+
package_manager == :bun
2029
end
2130

22-
def tool_exists?(tool)
23-
system "command -v #{tool} > /dev/null"
31+
def package_manager
32+
@package_manager ||= tool_determined_by_config_file || tool_determined_by_executable || DEFAULT_TOOL
2433
end
2534

26-
def add_package_json_script(name, script, run_script=true)
27-
if using_bun?
28-
package_json = JSON.parse(File.read("package.json"))
29-
package_json["scripts"] ||= {}
30-
package_json["scripts"][name] = script.gsub('\\"', '"')
31-
File.write("package.json", JSON.pretty_generate(package_json))
32-
run %(bun run #{name}) if run_script
33-
else
34-
case `npx -v`.to_f
35-
when 7.1...8.0
35+
def add_package_json_script(name, script, run_script: true)
36+
case package_manager
37+
when :yarn
38+
npx_version = `npx -v`.to_f
39+
40+
if npx_version >= 7.1 && npx_version < 8.0
3641
say "Add #{name} script"
3742
run %(npm set-script #{name} "#{script}")
38-
run %(yarn #{name}) if run_script
39-
when (8.0..)
43+
elsif npx_version >= 8.0
4044
say "Add #{name} script"
4145
run %(npm pkg set scripts.#{name}="#{script}")
42-
run %(yarn #{name}) if run_script
4346
else
4447
say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
48+
return
4549
end
50+
when :npm
51+
say "Add #{name} script"
52+
npx_version = `npx -v`.to_f
53+
54+
if npx_version >= 7.1 && npx_version < 8.0
55+
run %(npm set-script #{name} "#{script}")
56+
else
57+
run %(npm pkg set scripts.#{name}="#{script}")
58+
end
59+
when :pnpm
60+
say "Add #{name} script"
61+
run %(pnpm pkg set scripts.#{name}="#{script}")
62+
when :bun
63+
say "Add #{name} script to package.json manually"
64+
package_json = JSON.parse(File.read("package.json"))
65+
package_json["scripts"] ||= {}
66+
package_json["scripts"][name] = script.gsub('\\"', '"')
67+
File.write("package.json", JSON.pretty_generate(package_json))
68+
end
69+
70+
run %(#{bundler_run_cmd} #{name}) if run_script
71+
end
72+
73+
private
74+
75+
def tool_exists?(tool)
76+
system "command -v #{tool} > /dev/null"
77+
end
78+
79+
def tool_determined_by_config_file
80+
case
81+
when File.exist?("yarn.lock") then :yarn
82+
when File.exist?("bun.lockb") then :bun
83+
when File.exist?("bun.lock") then :bun
84+
when File.exist?("pnpm-lock.yaml") then :pnpm
85+
when File.exist?("package-lock.json") then :npm
86+
end
87+
end
88+
89+
def tool_determined_by_executable
90+
SUPPORTED_TOOLS.each do |tool|
91+
return tool if tool_exists?(tool)
4692
end
4793
end
4894
end

Diff for: lib/tasks/cssbundling/build.rake

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ module Cssbundling
2222
extend self
2323

2424
LOCK_FILES = {
25-
bun: %w[bun.lockb bun.lock yarn.lock],
2625
yarn: %w[yarn.lock],
26+
bun: %w[bun.lockb bun.lock],
2727
pnpm: %w[pnpm-lock.yaml],
2828
npm: %w[package-lock.json]
2929
}
3030

3131
def install_command
3232
case
33-
when using_tool?(:bun) then "bun install"
3433
when using_tool?(:yarn) then "yarn install"
34+
when using_tool?(:bun) then "bun install"
3535
when using_tool?(:pnpm) then "pnpm install"
3636
when using_tool?(:npm) then "npm install"
3737
else raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
@@ -40,8 +40,8 @@ module Cssbundling
4040

4141
def build_command
4242
case
43-
when using_tool?(:bun) then "bun run build:css"
4443
when using_tool?(:yarn) then "yarn build:css"
44+
when using_tool?(:bun) then "bun run build:css"
4545
when using_tool?(:pnpm) then "pnpm build:css"
4646
when using_tool?(:npm) then "npm run build:css"
4747
else raise "cssbundling-rails: No suitable tool found for building CSS"

0 commit comments

Comments
 (0)