Skip to content

Commit

Permalink
chore: @npmcli/package-json refactor
Browse files Browse the repository at this point in the history
Removes lib/update-root-package-json.js in favor of using
@npmcli/package-json for reading and modifying package.json during
reify.

Relates to: npm/statusboard#368
  • Loading branch information
ruyadorno committed Jun 23, 2021
1 parent 1c295c0 commit 15c4903
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 395 deletions.
23 changes: 21 additions & 2 deletions lib/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const mkdirp = require('mkdirp-infer-owner')
const justMkdirp = require('mkdirp')
const moveFile = require('@npmcli/move-file')
const rimraf = promisify(require('rimraf'))
const PackageJson = require('@npmcli/package-json')
const packageContents = require('@npmcli/installed-package-contents')
const { checkEngine, checkPlatform } = require('npm-install-checks')

Expand All @@ -24,7 +25,6 @@ const Diff = require('../diff.js')
const retirePath = require('../retire-path.js')
const promiseAllRejectLate = require('promise-all-reject-late')
const optionalSet = require('../optional-set.js')
const updateRootPackageJson = require('../update-root-package-json.js')
const calcDepFlags = require('../calc-dep-flags.js')
const { saveTypeMap, hasSubKey } = require('../add-rm-pkg-deps.js')

Expand Down Expand Up @@ -1029,14 +1029,33 @@ module.exports = cls => class Reifier extends cls {

const promises = [this[_saveLockFile](saveOpt)]

const updatePackageJson = async (tree) => {
const pkgJson = await PackageJson.load(tree.path)
.catch(() => new PackageJson(tree.path))
const {
dependencies = {},
devDependencies = {},
optionalDependencies = {},
peerDependencies = {},
} = tree.package

pkgJson.update({
dependencies,
devDependencies,
optionalDependencies,
peerDependencies,
})
await pkgJson.save()
}

// grab any from explicitRequests that had deps removed
for (const { from: tree } of this.explicitRequests)
updatedTrees.add(tree)

for (const tree of updatedTrees) {
// refresh the edges so they have the correct specs
tree.package = tree.package
promises.push(updateRootPackageJson(tree))
promises.push(updatePackageJson(tree))
}

await Promise.all(promises)
Expand Down
95 changes: 0 additions & 95 deletions lib/update-root-package-json.js

This file was deleted.

17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@npmcli/move-file": "^1.1.0",
"@npmcli/name-from-folder": "^1.0.1",
"@npmcli/node-gyp": "^1.0.1",
"@npmcli/package-json": "^1.0.1",
"@npmcli/run-script": "^1.8.2",
"bin-links": "^2.2.1",
"cacache": "^15.0.3",
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/arborist/reify.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31952,7 +31952,7 @@ exports[`test/arborist/reify.js TAP save proper lockfile with bins when upgradin
`

exports[`test/arborist/reify.js TAP save-prod, with optional > must match snapshot 1`] = `
{"optionalDependencies":{},"dependencies":{"abbrev":"^1.1.1"}}
{"dependencies":{"abbrev":"^1.1.1"}}
`

exports[`test/arborist/reify.js TAP saving the ideal tree save some stuff > lock after save 1`] = `
Expand Down
Loading

0 comments on commit 15c4903

Please sign in to comment.