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 525bc73
Show file tree
Hide file tree
Showing 2 changed files with 24 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
14 changes: 12 additions & 2 deletions spec/generator_specs/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@
# TODO: ideally "yarn_berry" should be here too, but it requires more complex setup
NODE_PACKAGE_MANAGERS.reject { |fm| fm == "yarn_berry" }.each do |fallback_manager|
context "when using package_json with #{fallback_manager} as the manager" do
let(:fake_manager_checker) { instance_double(Shakapacker::ManagerChecker).as_null_object }

before :all do
allow(Shakapacker::ManagerChecker).to receive(:new).and_return(fake_manager_checker)

allow(fake_manager_checker).to receive(:guess_manager).and_return(:yarn)
allow(fake_manager_checker).to receive(:guess_manager_version).and_return("1.2.3")

sh_opts = { fallback_manager: fallback_manager }

sh_in_dir(sh_opts, SPEC_PATH, "cp -r '#{BASE_RAILS_APP_PATH}' '#{TEMP_RAILS_APP_PATH}'")
Expand All @@ -58,10 +65,13 @@
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")
expect(package_json.fetch("packageManager", "")).to eq("[email protected]")

expect(fake_manager_checker).to have_received(:guess_manager)
expect(fake_manager_checker).to have_received(:guess_manager_version)
end

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

0 comments on commit 525bc73

Please sign in to comment.