Skip to content

Commit

Permalink
fix: shrinkwrap setting incorrect lockfileVersion
Browse files Browse the repository at this point in the history
Fix: #3962

When created from a hidden lockfile, shrinkwrap was always setting the
lockfileVersion to 3. The shrinkwrap command also needed to be updated
to log the correct message based on the lockfileVersion config instead
of the static lockfileVersion.

With these fixes, it was possible to log a better message whenever we
are changing the lockfileVersion, either from a config or any existing
lockfile.

Lastly, the tests for shrinkwrap were completely refactored to use the
real npm and test all conceivable scenarios, as well as removing usage
of `util.promisify` in favor of `fs.promises` now that we've dropped
support for Node 10.

PR-URL: #3978
Credit: @lukekarrys
Close: #3978
Reviewed-by: @wraithgar
  • Loading branch information
lukekarrys committed Nov 3, 2021
1 parent 8ffeb71 commit e5bfdac
Show file tree
Hide file tree
Showing 3 changed files with 613 additions and 323 deletions.
37 changes: 27 additions & 10 deletions lib/commands/shrinkwrap.js
@@ -1,7 +1,5 @@
const { resolve, basename } = require('path')
const util = require('util')
const fs = require('fs')
const { unlink } = fs.promises || { unlink: util.promisify(fs.unlink) }
const { unlink } = require('fs').promises
const Arborist = require('@npmcli/arborist')
const log = require('npmlog')

Expand All @@ -21,7 +19,6 @@ class Shrinkwrap extends BaseCommand {
// if has a npm-shrinkwrap.json, nothing to do
// if has a package-lock.json, rename to npm-shrinkwrap.json
// if has neither, load the actual tree and save that as npm-shrinkwrap.json
// in all cases, re-cast to current lockfile version
//
// loadVirtual, fall back to loadActual
// rename shrinkwrap file type, and tree.meta.save()
Expand All @@ -40,17 +37,37 @@ class Shrinkwrap extends BaseCommand {
const oldFilename = meta.filename
const notSW = !newFile && basename(oldFilename) !== 'npm-shrinkwrap.json'

// The computed lockfile version of a hidden lockfile is always 3
// even if the actual value of the property is a different.
// When shrinkwrap is run with only a hidden lockfile we want to
// set the shrinkwrap lockfile version as whatever was explicitly
// requested with a fallback to the actual value from the hidden
// lockfile.
if (meta.hiddenLockfile) {
meta.lockfileVersion = arb.options.lockfileVersion ||
meta.originalLockfileVersion
}
meta.hiddenLockfile = false
meta.filename = sw
await meta.save()

if (newFile)
log.notice('', 'created a lockfile as npm-shrinkwrap.json')
else if (notSW) {
const updatedVersion = meta.originalLockfileVersion !== meta.lockfileVersion
? meta.lockfileVersion
: null

if (newFile) {
let message = 'created a lockfile as npm-shrinkwrap.json'
if (updatedVersion)
message += ` with version ${updatedVersion}`
log.notice('', message)
} else if (notSW) {
await unlink(oldFilename)
log.notice('', 'package-lock.json has been renamed to npm-shrinkwrap.json')
} else if (meta.originalLockfileVersion !== this.npm.lockfileVersion)
log.notice('', `npm-shrinkwrap.json updated to version ${this.npm.lockfileVersion}`)
let message = 'package-lock.json has been renamed to npm-shrinkwrap.json'
if (updatedVersion)
message += ` and updated to version ${updatedVersion}`
log.notice('', message)
} else if (updatedVersion)
log.notice('', `npm-shrinkwrap.json updated to version ${updatedVersion}`)
else
log.notice('', 'npm-shrinkwrap.json up to date')
}
Expand Down

0 comments on commit e5bfdac

Please sign in to comment.