Skip to content

Commit

Permalink
feat: ensure the packageManager field is set when installing
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 25, 2024
1 parent b3e0554 commit e20c1f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/install/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,28 @@ def package_json
babel["presets"] ||= []
babel["presets"].push("./node_modules/shakapacker/package/babel/preset.js")

package_manager = pj.fetch("packageManager", nil)

if package_manager.nil?
manager_checker = Shakapacker::ManagerChecker.new

package_manager = "#{manager_checker.guess_manager}@#{manager_checker.guess_manager_version}"
end

{
"name" => "app",
"private" => true,
"version" => "0.1.0",
"babel" => babel,
"browserslist" => [
"defaults"
]
],
"packageManager" => package_manager
}.merge(pj)
end

Shakapacker::ManagerChecker.new.warn_unless_package_manager_is_obvious!

# Ensure there is `system!("bin/yarn")` command in `./bin/setup` file
if (setup_path = Rails.root.join("bin/setup")).exist?
native_install_command = package_json.manager.native_install_command.join(" ")
Expand Down
7 changes: 5 additions & 2 deletions spec/generator_specs/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "json"
require "shakapacker/utils/misc"
require "shakapacker/utils/version_syntax_converter"
require "shakapacker/manager_checker"
require "package_json"

GEM_ROOT = Pathname.new(File.expand_path("../../..", __FILE__))
Expand Down Expand Up @@ -58,10 +59,12 @@
expect(actual_content).to eq expected_content
end

it "replaces the package.json completely" do
it "ensures the 'packageManager' field is set" do
package_json = PackageJson.read(path_in_the_app)

expect(package_json.fetch("name", "")).to eq("app")
manager_name = fallback_manager.split("_")[0]

expect(package_json.fetch("packageManager", "")).to match(/#{manager_name}@\d+\.\d+\.\d+/)
end

it "creates webpack config directory and its files" do
Expand Down

0 comments on commit e20c1f4

Please sign in to comment.