Skip to content

Commit

Permalink
[arborist] [refactor] Shrinkwrap: add toJSON method to get shrink…
Browse files Browse the repository at this point in the history
…wrap contents without saving
  • Loading branch information
ljharb committed Dec 18, 2021
1 parent d726504 commit 06fad9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,9 +1085,9 @@ class Shrinkwrap {
return lock
}

save (options = {}) {
toJSON (options = {}) {
if (!this.data) {
throw new Error('run load() before saving data')
throw new Error('run load() before getting or setting data')
}

const { format = true } = options
Expand All @@ -1096,7 +1096,14 @@ class Shrinkwrap {
: format || 0
const eol = format ? this.newline || '\n' : ''
const data = this.commit()
const json = stringify(data, swKeyOrder, indent).replace(/\n/g, eol)
return stringify(data, swKeyOrder, indent).replace(/\n/g, eol)
}

save (options = {}) {
if (!this.data) {
throw new Error('run load() before saving data')
}
const json = this.toJSON(options)
return Promise.all([
writeFile(this.filename, json).catch(er => {
if (this.hiddenLockfile) {
Expand Down
10 changes: 10 additions & 0 deletions workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14240,6 +14240,16 @@ exports[`test/shrinkwrap.js TAP saving dependency-free shrinkwrap object load fi
`

exports[`test/shrinkwrap.js TAP saving dependency-free shrinkwrap object load the unindented file, and generate expected contents > indented json output 1`] = `
{
"lockfileVersion": 2,
"requires": true,
"packages": {},
"dependencies": {}
}
`

exports[`test/shrinkwrap.js TAP saving dependency-free shrinkwrap object load the unindented file, and save it back default > indented json output 1`] = `
{
"lockfileVersion": 2,
Expand Down
14 changes: 14 additions & 0 deletions workspaces/arborist/test/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ t.test('throws when attempting to access data before loading', t => {
new Shrinkwrap().delete(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().add(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().toJSON(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().save(), Error('run load() before saving data'))
t.end()
Expand Down Expand Up @@ -481,6 +483,18 @@ t.test('saving dependency-free shrinkwrap object', t => {
t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'no indent json output')
})

t.test('load the unindented file, and generate expected contents', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
sw.filename,
resolve(`${dir}/package-lock.json`),
'correct filepath on shrinkwrap instance'
)
t.equal(sw.indent, '')
const json = await sw.toJSON()
t.matchSnapshot(json, 'indented json output')
})

t.test('load the unindented file, and save it back default', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
Expand Down

0 comments on commit 06fad9e

Please sign in to comment.