Skip to content

Commit

Permalink
[arborist] [refactor] Shrinkwrap: add toJSON/toString methods t…
Browse files Browse the repository at this point in the history
…o get shrinkwrap contents without saving (#4181)
  • Loading branch information
ljharb authored Jan 6, 2022
1 parent 0d3cf8b commit 3cfae38
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
19 changes: 15 additions & 4 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,18 +1085,29 @@ class Shrinkwrap {
return lock
}

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

return this.commit()
}

toString (options = {}) {
const data = this.toJSON()
const { format = true } = options
const defaultIndent = this.indent || 2
const indent = format === true ? defaultIndent
: 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.toString(options)
return Promise.all([
writeFile(this.filename, json).catch(er => {
if (this.hiddenLockfile) {
Expand Down
19 changes: 19 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,25 @@ 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 object output 1`] = `
Object {
"dependencies": Object {},
"lockfileVersion": 2,
"packages": Object {},
"requires": true,
}
`

exports[`test/shrinkwrap.js TAP saving dependency-free shrinkwrap object load the unindented file, and generate expected contents > indented json string 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
19 changes: 19 additions & 0 deletions workspaces/arborist/test/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ 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().toString(), 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 +485,21 @@ 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 object output')

const jsonString = await sw.toString()
t.matchSnapshot(jsonString, 'indented json string 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 3cfae38

Please sign in to comment.