Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Harband <ljharb@gmail.com>2022-01-18 22:24:53 +0300
committerGitHub <noreply@github.com>2022-01-18 22:24:53 +0300
commit510f0ecbc9970ed8c8993107cc03cf27b7b996dc (patch)
tree9e5f14ce48dd59af996bbf6b395143b012f4dbde /workspaces/arborist/lib
parenta92665c92940b93e3e41eb8396257d684ee95c5f (diff)
fix(arborist): ensure indentation is preserved (#4218)
It turns out that `new Arborist().buildIdealTree().meta.toString()` does not take into account the indentation in the package.json (tabs, in my case) the way `npm install --package-lock-only` does. This fixes that. Also included a bonus commit that removes redundant Promise stuff inside an `async function`.
Diffstat (limited to 'workspaces/arborist/lib')
-rw-r--r--workspaces/arborist/lib/arborist/build-ideal-tree.js6
-rw-r--r--workspaces/arborist/lib/shrinkwrap.js22
2 files changed, 16 insertions, 12 deletions
diff --git a/workspaces/arborist/lib/arborist/build-ideal-tree.js b/workspaces/arborist/lib/arborist/build-ideal-tree.js
index 899d92ca9..dffcd546b 100644
--- a/workspaces/arborist/lib/arborist/build-ideal-tree.js
+++ b/workspaces/arborist/lib/arborist/build-ideal-tree.js
@@ -176,7 +176,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
// public method
async buildIdealTree (options = {}) {
if (this.idealTree) {
- return Promise.resolve(this.idealTree)
+ return this.idealTree
}
// allow the user to set reify options on the ctor as well.
@@ -194,8 +194,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
process.emit('time', 'idealTree')
if (!options.add && !options.rm && !options.update && this[_global]) {
- const er = new Error('global requires add, rm, or update option')
- return Promise.reject(er)
+ throw new Error('global requires add, rm, or update option')
}
// first get the virtual tree, if possible. If there's a lockfile, then
@@ -334,6 +333,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
root.meta.lockfileVersion = defaultLockfileVersion
}
}
+ root.meta.inferFormattingOptions(root.package)
return root
})
diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js
index a7a68c98c..b45fea0ac 100644
--- a/workspaces/arborist/lib/shrinkwrap.js
+++ b/workspaces/arborist/lib/shrinkwrap.js
@@ -424,6 +424,18 @@ class Shrinkwrap {
.map(fn => fn && maybeStatFile(fn)))
}
+ inferFormattingOptions (packageJSONData) {
+ // don't use detect-indent, just pick the first line.
+ // if the file starts with {" then we have an indent of '', ie, none
+ // which will default to 2 at save time.
+ const {
+ [Symbol.for('indent')]: indent,
+ [Symbol.for('newline')]: newline,
+ } = packageJSONData
+ this.indent = indent !== undefined ? indent : this.indent
+ this.newline = newline !== undefined ? newline : this.newline
+ }
+
load () {
// we don't need to load package-lock.json except for top of tree nodes,
// only npm-shrinkwrap.json.
@@ -451,15 +463,7 @@ class Shrinkwrap {
return data ? parseJSON(data) : {}
}).then(async data => {
- // don't use detect-indent, just pick the first line.
- // if the file starts with {" then we have an indent of '', ie, none
- // which will default to 2 at save time.
- const {
- [Symbol.for('indent')]: indent,
- [Symbol.for('newline')]: newline,
- } = data
- this.indent = indent !== undefined ? indent : this.indent
- this.newline = newline !== undefined ? newline : this.newline
+ this.inferFormattingOptions(data)
if (!this.hiddenLockfile || !data.packages) {
return data