Skip to content

Commit

Permalink
feat: add fromContent method to set contents directly (#96)
Browse files Browse the repository at this point in the history
Currently to replace `read-package-json-fast` in `pacote` we already
have a manifest that needs normalizing. The only way to do that
currently is to pass in a string and re-JSON.parse it which is a waste.
This allows for content to be set directly.
  • Loading branch information
lukekarrys authored Apr 22, 2024
1 parent 9597b84 commit 17788d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ class PackageJson {
return this
}

fromContent (data) {
this.#manifest = data
this.#canSave = false
return this
}

// Load data from a comment
// /**package { "name": "foo", "version": "1.2.3", ... } **/
fromComment (data) {
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,11 @@ t.test('cannot update with no content', async t => {
message: /Can not update without content/,
})
})

t.test('can set data', async t => {
const p = new PackageJson().fromContent({ data: 1 })
t.strictSame(p.content, { data: 1 })
await t.rejects(p.save(), {
message: /No package\.json to save to/,
})
})

0 comments on commit 17788d0

Please sign in to comment.