Skip to content

Commit

Permalink
feat: support deleting properties from package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 12, 2023
1 parent ed2b004 commit 437362c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ package_json.merge! do |pj|
}
end

# deletes the "babel" property, if it exists
package_json.delete!("babel")

# runs the "lint" script with the "--fix" argument
package_json.manager.run("lint", ["--fix"])
```
Expand Down
10 changes: 10 additions & 0 deletions lib/package_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def merge!
write_package_json(pj.merge(yield read_package_json))
end

def delete!(key)
pj = read_package_json

value = pj.delete(key)

write_package_json(pj)

value
end

private

def package_json_path
Expand Down
34 changes: 34 additions & 0 deletions spec/package_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,38 @@
end
end
end

describe "#delete!" do
it "deletes the key from the package.json" do
with_package_json_file({ "version" => "1.0.0", "babel" => { "presets" => ["path/to/my/preset"] } }) do
package_json = described_class.new

package_json.delete!("babel")

expect_package_json_with_content({ "version" => "1.0.0" })
end
end

it "returns the value" do
with_package_json_file({ "version" => "1.0.0", "babel" => { "presets" => ["path/to/my/preset"] } }) do
package_json = described_class.new

expect(package_json.delete!("babel")).to eq({ "presets" => ["path/to/my/preset"] })
end
end

context "when the property does not exist" do
it "does not error" do
package_json = described_class.new

expect { package_json.delete!("does not exist") }.not_to raise_error
end

it "returns nil" do
package_json = described_class.new

expect(package_json.delete!("does not exist")).to be_nil
end
end
end
end

0 comments on commit 437362c

Please sign in to comment.