Skip to content

Commit ae8ee6c

Browse files
committed
Package manager detection
1 parent 8eca54b commit ae8ee6c

File tree

3 files changed

+88
-30
lines changed

3 files changed

+88
-30
lines changed

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

lib/install/helpers.rb

+84-26
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,106 @@
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+
13+
SUPPORTED_TOOLS = TOOLS_COMMANDS.keys.freeze
14+
DEFAULT_TOOL = :yarn
15+
16+
SCRIPT_STRATEGIES = {
17+
yarn: lambda do |helper, name, script, run_script|
18+
npx_version = `npx -v`.to_f
19+
20+
if npx_version >= 7.1 && npx_version < 8.0
21+
helper.say "Add #{name} script"
22+
helper.run %(npm set-script #{name} "#{script}")
23+
helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script
24+
elsif npx_version >= 8.0
25+
helper.say "Add #{name} script"
26+
helper.run %(npm pkg set scripts.#{name}="#{script}")
27+
helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script
28+
else
29+
helper.say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
30+
end
31+
end,
32+
33+
bun: lambda do |helper, name, script, run_script|
34+
package_json = JSON.parse(File.read("package.json"))
35+
package_json["scripts"] ||= {}
36+
package_json["scripts"][name] = script.gsub('\\"', '"')
37+
File.write("package.json", JSON.pretty_generate(package_json))
38+
helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script
39+
end,
40+
41+
pnpm: lambda do |helper, name, script, run_script|
42+
helper.say "Add #{name} script"
43+
helper.run %(pnpm pkg set scripts.#{name}="#{script}")
44+
helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script
45+
end,
46+
47+
npm: lambda do |helper, name, script, run_script|
48+
helper.say "Add #{name} script"
49+
npx_version = `npx -v`.to_f
50+
51+
if npx_version >= 7.1 && npx_version < 8.0
52+
helper.run %(npm set-script #{name} "#{script}")
53+
else
54+
helper.run %(npm pkg set scripts.#{name}="#{script}")
55+
end
56+
57+
helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script
58+
end
59+
}.freeze
60+
461
def bundler_cmd
5-
using_bun? ? "bun" : "yarn"
62+
TOOLS_COMMANDS.dig(package_manager, :cmd)
663
end
764

865
def bundler_run_cmd
9-
using_bun? ? "bun run" : "yarn"
66+
TOOLS_COMMANDS.dig(package_manager, :run)
1067
end
1168

1269
def bundler_x_cmd
13-
using_bun? ? "bunx" : "npx"
70+
TOOLS_COMMANDS.dig(package_manager, :x)
1471
end
1572

1673
def using_bun?
17-
tool_exists?('bun') && (File.exist?('bun.lockb') ||
18-
File.exist?('bun.lock') ||
19-
File.exist?('yarn.lock'))
74+
package_manager == :bun
75+
end
76+
77+
def package_manager
78+
@package_manager ||= tool_determined_by_config_file || tool_determined_by_executable || DEFAULT_TOOL
2079
end
2180

81+
def add_package_json_script(name, script, run_script: true)
82+
SCRIPT_STRATEGIES[package_manager].call(self, name, script, run_script)
83+
end
84+
85+
private
86+
2287
def tool_exists?(tool)
2388
system "command -v #{tool} > /dev/null"
2489
end
2590

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
36-
say "Add #{name} script"
37-
run %(npm set-script #{name} "#{script}")
38-
run %(yarn #{name}) if run_script
39-
when (8.0..)
40-
say "Add #{name} script"
41-
run %(npm pkg set scripts.#{name}="#{script}")
42-
run %(yarn #{name}) if run_script
43-
else
44-
say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
45-
end
91+
def tool_determined_by_config_file
92+
case
93+
when File.exist?("yarn.lock") then :yarn
94+
when File.exist?("bun.lockb") then :bun
95+
when File.exist?("bun.lock") then :bun
96+
when File.exist?("pnpm-lock.yaml") then :pnpm
97+
when File.exist?("package-lock.json") then :npm
98+
end
99+
end
100+
101+
def tool_determined_by_executable
102+
SUPPORTED_TOOLS.keys.each do |tool|
103+
return tool if tool_exists?(tool)
46104
end
47105
end
48106
end

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)