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

Improved detection logic for package manager #171

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
2 changes: 1 addition & 1 deletion lib/install/bootstrap/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
88 changes: 67 additions & 21 deletions lib/install/helpers.rb
Original file line number Diff line number Diff line change
@@ -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?("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
end

def tool_determined_by_executable
SUPPORTED_TOOLS.each do |tool|
return tool if tool_exists?(tool)
end
end
end
6 changes: 3 additions & 3 deletions lib/tasks/cssbundling/build.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down