Skip to content

Commit 1814b45

Browse files
authored
fix: re-add positional arg and abbrev warnings (#8145)
PR #8071 originally had this but that appears to have gotten lost along the way.
1 parent 26803bc commit 1814b45

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

workspaces/config/lib/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,11 @@ class Config {
364364
}
365365
nopt.invalidHandler = (k, val, type) =>
366366
this.invalidHandler(k, val, type, 'command line options', 'cli')
367+
nopt.unknownHandler = this.unknownHandler
368+
nopt.abbrevHandler = this.abbrevHandler
367369
const conf = nopt(this.types, this.shorthands, this.argv)
368370
nopt.invalidHandler = null
371+
nopt.unknownHandler = null
369372
this.parsedArgv = conf.argv
370373
delete conf.argv
371374
this.#loadObject(conf, 'cli', 'command line options')
@@ -531,6 +534,16 @@ class Config {
531534
log.warn('invalid config', msg, desc)
532535
}
533536

537+
abbrevHandler (short, long) {
538+
log.warn(`Expanding --${short} to --${long}. This will stop working in the next major version of npm.`)
539+
}
540+
541+
unknownHandler (key, next) {
542+
if (next) {
543+
log.warn(`"${next}" is being parsed as a normal command line argument.`)
544+
}
545+
}
546+
534547
#getOneOfKeywords (mustBe, typeDesc) {
535548
let keyword
536549
if (mustBe.length === 1 && typeDesc.includes(Array)) {

workspaces/config/test/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -1542,3 +1542,48 @@ t.test('invalid single hyphen warnings', async t => {
15421542
['warn', '-ws is not a valid single-hyphen cli flag and will be removed in the future'],
15431543
], 'Warns about single hyphen configs')
15441544
})
1545+
1546+
t.test('positional arg warnings', async t => {
1547+
const path = t.testdir()
1548+
const logs = []
1549+
const logHandler = (...args) => logs.push(args)
1550+
process.on('log', logHandler)
1551+
t.teardown(() => process.off('log', logHandler))
1552+
const config = new Config({
1553+
npmPath: `${path}/npm`,
1554+
env: {},
1555+
argv: [process.execPath, __filename, '--something', 'extra'],
1556+
cwd: path,
1557+
shorthands,
1558+
definitions,
1559+
nerfDarts,
1560+
})
1561+
await config.load()
1562+
const filtered = logs.filter(l => l[0] === 'warn')
1563+
t.match(filtered, [
1564+
['warn', '"extra" is being parsed as a normal command line argument.'],
1565+
['warn', 'Unknown cli config "--something". This will stop working in the next major version of npm.'],
1566+
], 'Warns about positional cli arg')
1567+
})
1568+
1569+
t.test('abbreviation expansion warnings', async t => {
1570+
const path = t.testdir()
1571+
const logs = []
1572+
const logHandler = (...args) => logs.push(args)
1573+
process.on('log', logHandler)
1574+
t.teardown(() => process.off('log', logHandler))
1575+
const config = new Config({
1576+
npmPath: `${path}/npm`,
1577+
env: {},
1578+
argv: [process.execPath, __filename, '--bef', '2020-01-01'],
1579+
cwd: path,
1580+
shorthands,
1581+
definitions,
1582+
nerfDarts,
1583+
})
1584+
await config.load()
1585+
const filtered = logs.filter(l => l[0] === 'warn')
1586+
t.match(filtered, [
1587+
['warn', 'Expanding --bef to --before. This will stop working in the next major version of npm'],
1588+
], 'Warns about expanded abbreviations')
1589+
})

0 commit comments

Comments
 (0)