Skip to content

Commit

Permalink
refact: allow to force package manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pftg committed Mar 6, 2024
1 parent 0c08ab6 commit 2440116
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def commands
@commands ||= ViteRuby::Commands.new(self)
end

def package_manager(**)
def package_manager
@package_manager ||= ViteRuby::PackageManager.resolve(root: config.root)
end

Expand Down
29 changes: 25 additions & 4 deletions vite_ruby/lib/vite_ruby/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,35 @@

module ViteRuby::PackageManager
def self.resolve(root:)
package_manager_name = ENV.fetch('VITE_RUBY_PACKAGE_MANAGER', detect_package_manager(root)).to_sym
package_manager_class_for(package_manager_name).new(root:)
end

private

def self.package_manager_class_for(package_manager_name)
case package_manager_name
when :bun
ViteRuby::PackageManager::Bun
when :pnpm
ViteRuby::PackageManager::Pnpm
when :yarn
ViteRuby::PackageManager::Yarn
else
ViteRuby::PackageManager::Npm
end
end

def self.detect_package_manager(root)
if root.join('bun.lockb').exist?
ViteRuby::PackageManager::Bun.new(root: root)
:bun
elsif root.join('pnpm-lock.yaml').exist?
ViteRuby::PackageManager::Pnpm.new(root: root)
:pnpm
elsif root.join('yarn.lock').exist?
ViteRuby::PackageManager::Yarn.new(root: root)
:yarn
else
ViteRuby::PackageManager::Npm.new(root: root)
:npm
end
end
end

0 comments on commit 2440116

Please sign in to comment.