From 06fad9e6a74d6686e06a17e2a5759d44182b99e8 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 17 Dec 2021 22:10:29 -0800 Subject: [PATCH] [arborist] [refactor] `Shrinkwrap`: add `toJSON` method to get shrinkwrap contents without saving --- workspaces/arborist/lib/shrinkwrap.js | 13 ++++++++++--- .../tap-snapshots/test/shrinkwrap.js.test.cjs | 10 ++++++++++ workspaces/arborist/test/shrinkwrap.js | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index 2f0c0877cf8e0..e568595cd0568 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -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 @@ -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) { diff --git a/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs b/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs index 39b1d97ecce65..050dda1ddd2fa 100644 --- a/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs +++ b/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs @@ -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, diff --git a/workspaces/arborist/test/shrinkwrap.js b/workspaces/arborist/test/shrinkwrap.js index 2bf43fcf97e1c..d40f16abb846e 100644 --- a/workspaces/arborist/test/shrinkwrap.js +++ b/workspaces/arborist/test/shrinkwrap.js @@ -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() @@ -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(