Skip to content

Commit 17788d0

Browse files
authored
feat: add fromContent method to set contents directly (#96)
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.
1 parent 9597b84 commit 17788d0

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

lib/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ class PackageJson {
167167
return this
168168
}
169169

170+
fromContent (data) {
171+
this.#manifest = data
172+
this.#canSave = false
173+
return this
174+
}
175+
170176
// Load data from a comment
171177
// /**package { "name": "foo", "version": "1.2.3", ... } **/
172178
fromComment (data) {

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,11 @@ t.test('cannot update with no content', async t => {
225225
message: /Can not update without content/,
226226
})
227227
})
228+
229+
t.test('can set data', async t => {
230+
const p = new PackageJson().fromContent({ data: 1 })
231+
t.strictSame(p.content, { data: 1 })
232+
await t.rejects(p.save(), {
233+
message: /No package\.json to save to/,
234+
})
235+
})

0 commit comments

Comments
 (0)