Skip to content

Commit cb9f435

Browse files
voxpellilukekarrys
authored andcommitted
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
1 parent 62c7315 commit cb9f435

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

docs/content/using-npm/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ When passed to `npm config` this refers to which config file to use.
987987

988988
* Default: Version 2 if no lockfile or current lockfile version less than or
989989
equal to 2, otherwise maintain current lockfile version
990-
* Type: null, 1, 2, or 3
990+
* Type: null, 1, 2, 3, "1", "2", or "3"
991991

992992
Set the lockfile format version to be used in package-lock.json and
993993
npm-shrinkwrap-json files. Possible options are:

lib/utils/config/definitions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ define('location', {
11571157

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

11851187
define('loglevel', {

tap-snapshots/test/lib/utils/config/definitions.js.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ exports[`test/lib/utils/config/definitions.js TAP > config description for lockf
10611061
10621062
* Default: Version 2 if no lockfile or current lockfile version less than or
10631063
equal to 2, otherwise maintain current lockfile version
1064-
* Type: null, 1, 2, or 3
1064+
* Type: null, 1, 2, 3, "1", "2", or "3"
10651065
10661066
Set the lockfile format version to be used in package-lock.json and
10671067
npm-shrinkwrap-json files. Possible options are:

tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ When passed to \`npm config\` this refers to which config file to use.
861861
862862
* Default: Version 2 if no lockfile or current lockfile version less than or
863863
equal to 2, otherwise maintain current lockfile version
864-
* Type: null, 1, 2, or 3
864+
* Type: null, 1, 2, 3, "1", "2", or "3"
865865
866866
Set the lockfile format version to be used in package-lock.json and
867867
npm-shrinkwrap-json files. Possible options are:

test/lib/utils/config/definitions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,12 @@ t.test('workspaces derived', t => {
892892
t.equal(flat.workspacesEnabled, false)
893893
t.end()
894894
})
895+
896+
t.test('lockfile version', t => {
897+
const flat = {}
898+
definitions['lockfile-version'].flatten('lockfile-version', {
899+
'lockfile-version': '3',
900+
}, flat)
901+
t.match(flat.lockfileVersion, 3, 'flattens to a number')
902+
t.end()
903+
})

0 commit comments

Comments
 (0)