@npmcli/config 10.6.0 → 10.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,8 @@ const allowed = [
22
22
  'typeDescription',
23
23
  'usage',
24
24
  'envExport',
25
+ 'alias',
26
+ 'required',
25
27
  ]
26
28
 
27
29
  const {
@@ -246,6 +246,7 @@ const definitions = {
246
246
  default: null,
247
247
  hint: '<date>',
248
248
  type: [null, Date],
249
+ exclusive: ['min-release-age'],
249
250
  description: `
250
251
  If passed to \`npm install\`, will rebuild the npm tree such that only
251
252
  versions that were available **on or before** the given date are
@@ -1347,6 +1348,28 @@ const definitions = {
1347
1348
  `,
1348
1349
  flatten,
1349
1350
  }),
1351
+ 'min-release-age': new Definition('min-release-age', {
1352
+ default: null,
1353
+ hint: '<days>',
1354
+ type: [null, Number],
1355
+ exclusive: ['before'],
1356
+ description: `
1357
+ If set, npm will build the npm tree such that only versions that were
1358
+ available more than the given number of days ago will be installed. If
1359
+ there are no versions available for the current set of dependencies, the
1360
+ command will error.
1361
+
1362
+ This flag is a complement to \`before\`, which accepts an exact date
1363
+ instead of a relative number of days.
1364
+ `,
1365
+ flatten: (key, obj, flatOptions) => {
1366
+ if (obj['min-release-age'] !== null) {
1367
+ flatOptions.before = new Date(Date.now() - (86400000 * obj['min-release-age']))
1368
+ obj.before = flatOptions.before
1369
+ delete obj['min-release-age']
1370
+ }
1371
+ },
1372
+ }),
1350
1373
  'node-gyp': new Definition('node-gyp', {
1351
1374
  default: (() => {
1352
1375
  try {
package/lib/index.js CHANGED
@@ -59,6 +59,7 @@ class Config {
59
59
  #flatten
60
60
  // populated the first time we flatten the object
61
61
  #flatOptions = null
62
+ #warnings = []
62
63
 
63
64
  static get typeDefs () {
64
65
  return typeDefs
@@ -78,20 +79,13 @@ class Config {
78
79
  execPath = process.execPath,
79
80
  cwd = process.cwd(),
80
81
  excludeNpmCwd = false,
82
+ warn = true,
81
83
  }) {
82
84
  this.nerfDarts = nerfDarts
83
85
  this.definitions = definitions
84
86
  // turn the definitions into nopt's weirdo syntax
85
- const types = {}
86
- const defaults = {}
87
- this.deprecated = {}
88
- for (const [key, def] of Object.entries(definitions)) {
89
- defaults[key] = def.default
90
- types[key] = def.type
91
- if (def.deprecated) {
92
- this.deprecated[key] = def.deprecated.trim().replace(/\n +/, '\n')
93
- }
94
- }
87
+ const { types, defaults, deprecated } = getTypesFromDefinitions(definitions)
88
+ this.deprecated = deprecated
95
89
 
96
90
  this.#flatten = flatten
97
91
  this.types = types
@@ -137,6 +131,7 @@ class Config {
137
131
  }
138
132
 
139
133
  this.#loaded = false
134
+ this.warn = warn
140
135
  }
141
136
 
142
137
  get list () {
@@ -369,7 +364,7 @@ class Config {
369
364
  }
370
365
  nopt.invalidHandler = (k, val, type) =>
371
366
  this.invalidHandler(k, val, type, 'command line options', 'cli')
372
- nopt.unknownHandler = this.unknownHandler
367
+ nopt.unknownHandler = (k, next) => this.unknownHandler(k, next)
373
368
  nopt.abbrevHandler = this.abbrevHandler
374
369
  const conf = nopt(this.types, this.shorthands, this.argv)
375
370
  nopt.invalidHandler = null
@@ -545,7 +540,7 @@ class Config {
545
540
 
546
541
  unknownHandler (key, next) {
547
542
  if (next) {
548
- log.warn(`"${next}" is being parsed as a normal command line argument.`)
543
+ this.queueWarning(`unknown:${next}`, `"${next}" is being parsed as a normal command line argument.`)
549
544
  }
550
545
  }
551
546
 
@@ -614,12 +609,12 @@ class Config {
614
609
  return
615
610
  }
616
611
  if (!key.includes(':')) {
617
- log.warn(`Unknown ${where} config "${where === 'cli' ? '--' : ''}${key}". This will stop working in the next major version of npm.`)
612
+ this.queueWarning(key, `Unknown ${where} config "${where === 'cli' ? '--' : ''}${key}". This will stop working in the next major version of npm.`)
618
613
  return
619
614
  }
620
615
  const baseKey = key.split(':').pop()
621
616
  if (!this.definitions[baseKey] && !this.nerfDarts.includes(baseKey)) {
622
- log.warn(`Unknown ${where} config "${baseKey}" (${key}). This will stop working in the next major version of npm.`)
617
+ this.queueWarning(baseKey, `Unknown ${where} config "${baseKey}" (${key}). This will stop working in the next major version of npm.`)
623
618
  }
624
619
  }
625
620
  }
@@ -923,6 +918,35 @@ class Config {
923
918
  setEnvs () {
924
919
  setEnvs(this)
925
920
  }
921
+
922
+ removeWarning (key) {
923
+ this.#warnings = this.#warnings.filter(w => w.type !== key)
924
+ }
925
+
926
+ getUnknownPositionals () {
927
+ return this.#warnings
928
+ .filter(w => w.type.startsWith('unknown:'))
929
+ .map(w => w.type.slice('unknown:'.length))
930
+ }
931
+
932
+ removeUnknownPositional (value) {
933
+ this.removeWarning(`unknown:${value}`)
934
+ }
935
+
936
+ queueWarning (type, ...args) {
937
+ if (!this.warn) {
938
+ this.#warnings.push({ type, args })
939
+ } else {
940
+ log.warn(...args)
941
+ }
942
+ }
943
+
944
+ logWarnings () {
945
+ for (const warning of this.#warnings) {
946
+ log.warn(...warning.args)
947
+ }
948
+ this.#warnings = []
949
+ }
926
950
  }
927
951
 
928
952
  const _loadError = Symbol('loadError')
@@ -980,4 +1004,21 @@ class ConfigData {
980
1004
  }
981
1005
  }
982
1006
 
1007
+ const getTypesFromDefinitions = (definitions) => {
1008
+ const types = {}
1009
+ const defaults = {}
1010
+ const deprecated = {}
1011
+
1012
+ for (const [key, def] of Object.entries(definitions)) {
1013
+ defaults[key] = def.default
1014
+ types[key] = def.type
1015
+ if (def.deprecated) {
1016
+ deprecated[key] = def.deprecated.trim().replace(/\n +/, '\n')
1017
+ }
1018
+ }
1019
+
1020
+ return { types, defaults, deprecated }
1021
+ }
1022
+
983
1023
  module.exports = Config
1024
+ module.exports.getTypesFromDefinitions = getTypesFromDefinitions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/config",
3
- "version": "10.6.0",
3
+ "version": "10.7.0",
4
4
  "files": [
5
5
  "bin/",
6
6
  "lib/"