Skip to content

Commit 9dd0eb5

Browse files
committed
feat: add syncNormalize
This is an affordance so that arborist can stop using read-package-json-fast. It currently calls read-package-json.normalize which is a synchronous function, different than read-package-json-fast() itself. We need this package to have an equivalent that skips the directories.bin expansion (which is asynchronous) and the _attribute stripping (which would break arborist)
1 parent a179a87 commit 9dd0eb5

6 files changed

Lines changed: 246 additions & 183 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Like `normalize` but intended for preparing package.json files for publish.
144144

145145
---
146146

147+
### `PackageJson.syncNormalize()`
148+
149+
This calls normalize synchronously. Most consumers of this package should avoid using this. It was added because some parts of npm were normalizing package content in class constructors and needed this affordance. It will silently ignore any asynchronous steps asked for. Again, this is a compatiblity affordance for some code in npm that is currently impossible to change without a significant semver major change, and is best not used.
150+
147151
### **static** `async PackageJson.prepare(path, opts = {})`
148152

149153
Convenience static that calls `load` before calling `prepare`

lib/index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const parseJSON = require('json-parse-even-better-errors')
55
const updateDeps = require('./update-dependencies.js')
66
const updateScripts = require('./update-scripts.js')
77
const updateWorkspaces = require('./update-workspaces.js')
8-
const normalize = require('./normalize.js')
8+
const { normalize, syncNormalize } = require('./normalize.js')
99
const { read, parse } = require('./read-package.js')
1010
const { packageSort } = require('./sort.js')
1111

@@ -25,18 +25,6 @@ const knownKeys = new Set([
2525
])
2626

2727
class PackageJson {
28-
static normalizeSteps = Object.freeze([
29-
'_id',
30-
'_attributes',
31-
'bundledDependencies',
32-
'bundleDependencies',
33-
'optionalDedupe',
34-
'scripts',
35-
'funding',
36-
'bin',
37-
'binDir',
38-
])
39-
4028
// npm pkg fix
4129
static fixSteps = Object.freeze([
4230
'binRefs',
@@ -49,6 +37,18 @@ class PackageJson {
4937
'scriptpath',
5038
])
5139

40+
static normalizeSteps = Object.freeze([
41+
'_id',
42+
'_attributes',
43+
'bundledDependencies',
44+
'bundleDependencies',
45+
'optionalDedupe',
46+
'scripts',
47+
'funding',
48+
'bin',
49+
'binDir',
50+
])
51+
5252
static prepareSteps = Object.freeze([
5353
'_id',
5454
'_attributes',
@@ -258,6 +258,13 @@ class PackageJson {
258258
}
259259
}
260260

261+
// steps is NOT overrideable here because this is a legacy function that's not being used in new places
262+
syncNormalize (opts = {}) {
263+
opts.steps = this.constructor.normalizeSteps.filter(s => s !== '_attributes')
264+
syncNormalize(this, opts)
265+
return this
266+
}
267+
261268
async normalize (opts = {}) {
262269
if (!opts.steps) {
263270
opts.steps = this.constructor.normalizeSteps

0 commit comments

Comments
 (0)