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: allow --lockfile-version config to be string and coerce to number #3949

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: allow --lockfile-version config to be string and coerce to number
As all CLI input is considered to be string, eg. a
"npm install --lockfile-version 3" would fail with the error messages:

```
npm WARN invalid config lockfile-version="3" set in command line options
npm WARN invalid config Must be one of: null, 1, 2, 3
```

Until we have a config system that supports setting type and possible values
of configs, we have to specify all string and number values for the
`lockfile-version`, but we coerce all values to numbers in the flattener.

Co-authored-by: @voxpelli
Co-authored-by: @isaacs

PR-URL: #3949
Credit: @lukekarrys
Close: #3949
Reviewed-by: @isaacs
  • Loading branch information
voxpelli authored and lukekarrys committed Oct 27, 2021
commit cb9f43551f46bf27095cd7bd6c1885a441004cd2
2 changes: 1 addition & 1 deletion docs/content/using-npm/config.md
Original file line number Diff line number Diff line change
@@ -987,7 +987,7 @@ When passed to `npm config` this refers to which config file to use.

* Default: Version 2 if no lockfile or current lockfile version less than or
equal to 2, otherwise maintain current lockfile version
* Type: null, 1, 2, or 3
* Type: null, 1, 2, 3, "1", "2", or "3"

Set the lockfile format version to be used in package-lock.json and
npm-shrinkwrap-json files. Possible options are:
6 changes: 4 additions & 2 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
@@ -1157,7 +1157,7 @@ define('location', {

define('lockfile-version', {
default: null,
type: [null, 1, 2, 3],
type: [null, 1, 2, 3, '1', '2', '3'],
defaultDescription: `
Version 2 if no lockfile or current lockfile version less than or equal to
2, otherwise maintain current lockfile version
@@ -1179,7 +1179,9 @@ define('lockfile-version', {
on disk than lockfile version 2, but not interoperable with older npm
versions. Ideal if all users are on npm version 7 and higher.
`,
flatten,
flatten: (key, obj, flatOptions) => {
flatOptions.lockfileVersion = obj[key] && parseInt(obj[key], 10)
},
})

define('loglevel', {
Original file line number Diff line number Diff line change
@@ -1061,7 +1061,7 @@ exports[`test/lib/utils/config/definitions.js TAP > config description for lockf
* Default: Version 2 if no lockfile or current lockfile version less than or
equal to 2, otherwise maintain current lockfile version
* Type: null, 1, 2, or 3
* Type: null, 1, 2, 3, "1", "2", or "3"
Set the lockfile format version to be used in package-lock.json and
npm-shrinkwrap-json files. Possible options are:
Original file line number Diff line number Diff line change
@@ -861,7 +861,7 @@ When passed to \`npm config\` this refers to which config file to use.
* Default: Version 2 if no lockfile or current lockfile version less than or
equal to 2, otherwise maintain current lockfile version
* Type: null, 1, 2, or 3
* Type: null, 1, 2, 3, "1", "2", or "3"
Set the lockfile format version to be used in package-lock.json and
npm-shrinkwrap-json files. Possible options are:
9 changes: 9 additions & 0 deletions test/lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
@@ -892,3 +892,12 @@ t.test('workspaces derived', t => {
t.equal(flat.workspacesEnabled, false)
t.end()
})

t.test('lockfile version', t => {
const flat = {}
definitions['lockfile-version'].flatten('lockfile-version', {
'lockfile-version': '3',
}, flat)
t.match(flat.lockfileVersion, 3, 'flattens to a number')
t.end()
})