Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: shrinkwrap setting incorrect lockfileVersion #3978

Merged
merged 1 commit into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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