diff options
author | Jordan Harband <ljharb@gmail.com> | 2022-01-06 22:12:25 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-06 22:12:25 +0300 |
commit | 3cfae384011a8b291cc82cc02b56bc114557a9e5 (patch) | |
tree | abf1470aff9474c369ede4e5c6f35f61403617ff /workspaces/arborist/lib | |
parent | 0d3cf8b6e8cbc46d394a1bdfcc12ffa5f6a72859 (diff) |
[arborist] [refactor] `Shrinkwrap`: add `toJSON`/`toString` methods to get shrinkwrap contents without saving (#4181)
Diffstat (limited to 'workspaces/arborist/lib')
-rw-r--r-- | workspaces/arborist/lib/shrinkwrap.js | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index 2f0c0877c..a7a68c98c 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -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) { |