Skip to content

Commit

Permalink
auto-detect package manager in the plugin manager (#821)
Browse files Browse the repository at this point in the history
* auto-detect package manager in the plugin installer

* linting

* linting

* memoize plugin manager

* fix linting issues
  • Loading branch information
KonnorRogers authored Nov 1, 2023
1 parent 1bf58eb commit 722cac7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions bridgetown-core/lib/bridgetown-core/plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ def self.legacy_yarn_and_register(required_gems, skip_yarn: false)
"Required #{required_gems.map(&:name).join(", ")}")
end

def self.package_manager
@package_manager ||= if File.exist?("yarn.lock")
"yarn"
elsif File.exist?("package-lock.json")
"npm"
elsif File.exist?("pnpm-lock.yaml")
"pnpm"
else
""
end
end

def self.package_manager_install_command
package_manager == "npm" ? "install" : "add"
end

# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

# Iterates through loaded gems and finds yard-add gemspec metadata.
# If that exact package hasn't been installed, execute yarn add
#
Expand All @@ -146,18 +164,22 @@ def self.install_yarn_dependencies(required_gems = bundler_specs, name: nil)
required_gems
end

# all right, time to install the package
gems_to_search.each do |loaded_gem|
yarn_dependency = find_yarn_dependency(loaded_gem)
next unless add_yarn_dependency?(yarn_dependency, package_json)

# all right, time to install the package
cmd = "yarn add #{yarn_dependency.join("@")}"
next if package_manager.empty?

cmd = "#{package_manager} #{package_manager_install_command} #{yarn_dependency.join("@")}"
system cmd
end

gems_to_search
end

# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

def self.find_yarn_dependency(loaded_gem)
yarn_dependency = loaded_gem.to_spec&.metadata&.dig("yarn-add")&.match(YARN_DEPENDENCY_REGEXP)
return nil if yarn_dependency&.length != 3 || yarn_dependency[2] == ""
Expand Down

0 comments on commit 722cac7

Please sign in to comment.