From c8b6df0ff1d6356fa37e2693f5fa25d8af448d00 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 7 Jan 2025 12:15:57 -0800 Subject: [PATCH 1/2] deps: add proc-log@5.0.0 --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 335fa5b..636aaa8 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ }, "license": "ISC", "dependencies": { - "abbrev": "^3.0.0" + "abbrev": "^3.0.0", + "proc-log": "^5.0.0" }, "devDependencies": { "@npmcli/eslint-config": "^5.0.0", From 834497a65290a6545b2148e362974755c0eeb5e2 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 7 Jan 2025 12:48:44 -0800 Subject: [PATCH 2/2] fix: log warnings about unknown config situations --- lib/nopt-lib.js | 13 ++++++++++- test/lib.js | 62 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/lib/nopt-lib.js b/lib/nopt-lib.js index d3d1de0..fc34ee5 100644 --- a/lib/nopt-lib.js +++ b/lib/nopt-lib.js @@ -1,6 +1,7 @@ const abbrev = require('abbrev') const debug = require('./debug') const defaultTypeDefs = require('./type-defs') +const { log } = require('proc-log') const hasOwn = (o, k) => Object.prototype.hasOwnProperty.call(o, k) @@ -298,7 +299,10 @@ function parse (args, data, remain, { arg = arg.slice(3) } - if (abbrevs[arg]) { + // abbrev includes the original full string in its abbrev list + if (abbrevs[arg] && abbrevs[arg] !== arg) { + /* eslint-disable-next-line max-len */ + log.warn(`Expanding "--${arg}" to "--${abbrevs[arg]}". This will stop working in the next major version of npm.`) arg = abbrevs[arg] } @@ -331,6 +335,11 @@ function parse (args, data, remain, { (argType === null || isTypeArray && ~argType.indexOf(null))) + if (typeof argType === 'undefined' && !hadEq && la && !la?.startsWith('-')) { + // npm itself will log the warning about the undefined argType + log.warn(`"${la}" is being parsed as a normal command line argument.`) + } + if (isBool) { // just set and move along val = !no @@ -458,6 +467,8 @@ function resolveShort (arg, ...rest) { // if it's an abbr for a shorthand, then use that if (shortAbbr[arg]) { + /* eslint-disable-next-line max-len */ + log.warn(`Expanding "--${arg}" to "--${shortAbbr[arg]}". This will stop working in the next major version of npm.`) arg = shortAbbr[arg] } diff --git a/test/lib.js b/test/lib.js index 7027f5d..21f9a23 100644 --- a/test/lib.js +++ b/test/lib.js @@ -2,13 +2,24 @@ const t = require('tap') const noptLib = require('../lib/nopt-lib.js') const Stream = require('stream') -const nopt = (t, argv, opts, expected) => { +const logs = [] +t.afterEach(() => { + logs.length = 0 +}) +process.on('log', (...msg) => { + logs.push(msg) +}) + +const nopt = (t, argv, opts, expected, expectedLogs) => { if (Array.isArray(argv)) { t.strictSame(noptLib.nopt(argv, { typeDefs: noptLib.typeDefs, ...opts }), expected) } else { noptLib.clean(argv, { typeDefs: noptLib.typeDefs, ...opts }) t.match(argv, expected) } + if (expectedLogs) { + t.match(expectedLogs, logs) + } t.end() } @@ -125,6 +136,43 @@ t.test('false invalid handler', (t) => { }) }) +t.test('longhand abbreviation', (t) => { + nopt(t, ['--lon', 'text'], { + types: { + long: String, + }, + }, { + long: 'text', + argv: { + remain: [], + cooked: ['--lon', 'text'], + original: ['--lon', 'text'], + }, + }, [ + /* eslint-disable-next-line max-len */ + ['warn', 'Expanding "--lon" to "--long". This will stop working in the next major version of npm.'], + ]) +}) + +t.test('shorthand abbreviation', (t) => { + nopt(t, ['--shor'], { + types: {}, + shorthands: { + short: '--shorthand', + }, + }, { + shorthand: true, + argv: { + remain: [], + cooked: ['--shorthand'], + original: ['--shor'], + }, + }, [ + /* eslint-disable-next-line max-len */ + ['warn', 'Expanding "--shor" to "--short". This will stop working in the next major version of npm.'], + ]) +}) + t.test('shorthands that is the same', (t) => { nopt(t, ['--sh'], { types: {}, @@ -142,14 +190,16 @@ t.test('shorthands that is the same', (t) => { }) t.test('unknown multiple', (t) => { - nopt(t, ['--mult', '--mult', '--mult'], { + nopt(t, ['--mult', '--mult', '--mult', 'extra'], { types: {}, }, { mult: [true, true, true], argv: { - remain: [], - cooked: ['--mult', '--mult', '--mult'], - original: ['--mult', '--mult', '--mult'], + remain: ['extra'], + cooked: ['--mult', '--mult', '--mult', 'extra'], + original: ['--mult', '--mult', '--mult', 'extra'], }, - }) + }, [ + ['warn', '"extra" is being parsed as a normal command line argument.'], + ]) })