Skip to content

Commit

Permalink
refact: encapsulates package managers' commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pftg committed Mar 6, 2024
1 parent d044bd6 commit 472163f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 47 deletions.
2 changes: 1 addition & 1 deletion test/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_build_command_with_argument
end

def test_command_capture
ViteRuby::Runner.stub_any_instance(:vite_executable, 'echo') {
ViteRuby::PackageManager::Base.stub_any_instance(:vite_executable, 'echo') {
stdout, stderr, status = ViteRuby.run(['"Hello"'])
assert_equal %("Hello" --mode production\n), stdout
assert_equal '', stderr
Expand Down
14 changes: 5 additions & 9 deletions vite_ruby/lib/tasks/vite.rake
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,13 @@ namespace :vite do

desc 'Ensure build dependencies like Vite are installed before bundling'
task :install_dependencies do
install_env_args = ENV['VITE_RUBY_SKIP_INSTALL_DEV_DEPENDENCIES'] == 'true' ? {} : { 'NODE_ENV' => 'development' }

if File.exist?('bun.lockb')
result = system(install_env_args, 'bun install')
next unless result.nil?
install_env_args = if ENV['VITE_RUBY_SKIP_INSTALL_DEV_DEPENDENCIES'] == 'true'
{}
else
{ 'NODE_ENV' => 'development' }
end

cmd = ViteRuby.commands.legacy_npm_version? ? 'npx ci --yes' : 'npx --yes ci'
result = system(install_env_args, cmd)
# Fallback to `yarn` if `npx` is not available.
system(install_env_args, 'yarn install --frozen-lockfile') if result.nil?
system(install_env_args, ViteRuby::PackageManager::Base.new.install_dependencies_command)
end

desc "Provide information on ViteRuby's environment"
Expand Down
10 changes: 3 additions & 7 deletions vite_ruby/lib/vite_ruby/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def install_js_dependencies
package_json = root.join('package.json')
write(package_json, '{}') unless package_json.exist?
deps = js_dependencies.join(' ')
run_with_capture("#{ npm_install } -D #{ deps }", stdin_data: "\n")
run_with_capture("#{ add_dependencies_command } -D #{ deps }", stdin_data: "\n")
end

# Internal: Adds compilation output dirs to git ignore.
Expand Down Expand Up @@ -116,12 +116,8 @@ def run_with_capture(*args, **options)
end

# Internal: Support all popular package managers.
def npm_install
return 'bun install' if root.join('bun.lockb').exist?
return 'yarn add' if root.join('yarn.lock').exist?
return 'pnpm install' if root.join('pnpm-lock.yaml').exist?

'npm install'
def add_dependencies_command(**)
ViteRuby::PackageManager::Base.new(root:).add_dependencies_command
end

# Internal: Avoid printing warning about missing vite.json, we will create one.
Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/cli/upgrade_packages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class ViteRuby::CLI::UpgradePackages < ViteRuby::CLI::Install
def call(**)
say 'Upgrading npm packages'
deps = js_dependencies.join(' ')
run_with_capture("#{ npm_install } -D #{ deps }")
run_with_capture("#{ add_dependencies_command } -D #{ deps }")
end
end
4 changes: 4 additions & 0 deletions vite_ruby/lib/vite_ruby/package_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module ViteRuby::PackageManager
end
89 changes: 89 additions & 0 deletions vite_ruby/lib/vite_ruby/package_manager/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true

class ViteRuby::PackageManager::Base
attr_reader :root

def initialize(root: ViteRuby.config.root)
@root = root
end

def install_dependencies_command(frozen: true)
return frozen ? 'bun install --frozen-lockfile' : 'bun install' if bun?
return frozen ? 'yarn install --frozen-lockfile' : 'yarn install' if yarn?
return frozen ? 'pnpm install --frozen-lockfile' : 'pnpm install' if pnpm?

if frozen
commands.legacy_npm_version? ? 'npm ci --yes' : 'npm --yes ci'
else
'npm install'
end
end

def add_dependencies_command
return 'bun install' if bun?
return 'yarn add' if yarn?
return 'pnpm install' if pnpm?

'npm install'
end

# Internal: Returns an Array with the command to run.
def command_for(args)
[config.to_env(env)].tap do |cmd|
args = args.clone
unless config.root.join('bun.lockb').exist?
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
end
cmd.push(*vite_executable)
cmd.push(*args)
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
end
end

private

def pnpm?
root.join('pnpm-lock.yaml').exist?
end

def bun?
root.join('bun.lockb').exist?
end

def yarn?
root.join('yarn.lock').exist?
end

# Internal: Resolves to an executable for Vite.
def vite_executable
bin_path = config.vite_bin_path

if File.exist?(bin_path)
# Forces a script or package to use Bun's runtime instead of Node.js (via symlinking node)
return ['bun', '--bun', bin_path] if bun?

return [bin_path]
end

if bun?
%w[bun --bun vite]
elsif yarn?
%w[yarn vite]
else
["#{ `npm bin`.chomp }/vite"]
end
end

def commands
ViteRuby.commands
end

def config
ViteRuby.config
end

def env
ViteRuby.env
end
end
30 changes: 1 addition & 29 deletions vite_ruby/lib/vite_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ def run(argv, exec: false)

# Internal: Returns an Array with the command to run.
def command_for(args)
[config.to_env(env)].tap do |cmd|
args = args.clone
unless config.root.join('bun.lockb').exist?
cmd.push('node', '--inspect-brk') if args.delete('--inspect')
cmd.push('node', '--trace-deprecation') if args.delete('--trace_deprecation')
end
cmd.push(*vite_executable)
cmd.push(*args)
cmd.push('--mode', config.mode) unless args.include?('--mode') || args.include?('-m')
end
end

# Internal: Resolves to an executable for Vite.
def vite_executable
bin_path = config.vite_bin_path

if File.exist?(bin_path)
return ['bun', '--bun', bin_path] if config.root.join('bun.lockb').exist?

return [bin_path]
end

if config.root.join('bun.lockb').exist?
%w[bun --bun vite]
elsif config.root.join('yarn.lock').exist?
%w[yarn vite]
else
["#{ `npm bin`.chomp }/vite"]
end
ViteRuby::PackageManager::Base.new(root: config.root).command_for(args)
end
end

0 comments on commit 472163f

Please sign in to comment.