Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arborist): add toJSON/toString methods to get shrinkwrap file contents without saving #4181

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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