Content-Length: 437953 | pFad | https://github.com/npm/cli/commit/593c84921b0df963cef2ca7b13e44acc20cbd558

E9 fix: warn on invalid single-hyphen cli flags (#8076) · npm/cli@593c849 · GitHub
Skip to content

Commit 593c849

Browse files
authored
fix: warn on invalid single-hyphen cli flags (#8076)
Single hyphen cli flags traditionally are single-character only, so they can be combined. npm already supports combining single-hyphen flags together, so it eventually needs stop supporting multi-character ones. Also re-added -ws to undocumented shorthands, it was accidentally removed from the main config and not re-added to the internal one. Finally, warnings on a few env configs that npm tosses around were suppressed for now.
1 parent ed85b01 commit 593c849

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

tap-snapshots/test/lib/docs.js.test.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,7 @@ exports[`test/lib/docs.js TAP shorthands > docs 1`] = `
25472547
* \`--help\`: \`--usage\`
25482548
* \`-v\`: \`--version\`
25492549
* \`-w\`: \`--workspace\`
2550+
* \`--ws\`: \`--workspaces\`
25502551
* \`-y\`: \`--yes\`
25512552
`
25522553

workspaces/config/lib/definitions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const shorthands = {
5555
readonly: ['--read-only'],
5656
reg: ['--registry'],
5757
iwr: ['--include-workspace-root'],
58+
ws: ['--workspaces'],
5859
...definitionProps.shorthands,
5960
}
6061

workspaces/config/lib/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const {
1515
mkdir,
1616
} = require('node:fs/promises')
1717

18+
// TODO these need to be either be ignored when parsing env, formalized as config, or not exported to the env in the first place. For now this list is just to suppress warnings till we can pay off this tech debt.
19+
const internalEnv = [
20+
'global-prefix',
21+
'local-prefix',
22+
'npm-version',
23+
'node-gyp',
24+
]
25+
1826
const fileExists = (...p) => stat(resolve(...p))
1927
.then((st) => st.isFile())
2028
.catch(() => false)
@@ -349,6 +357,11 @@ class Config {
349357
}
350358

351359
loadCLI () {
360+
for (const s of Object.keys(this.shorthands)) {
361+
if (s.length > 1 && this.argv.includes(`-${s}`)) {
362+
log.warn(`-${s} is not a valid single-hyphen cli flag and will be removed in the future`)
363+
}
364+
}
352365
nopt.invalidHandler = (k, val, type) =>
353366
this.invalidHandler(k, val, type, 'command line options', 'cli')
354367
const conf = nopt(this.types, this.shorthands, this.argv)
@@ -580,6 +593,9 @@ class Config {
580593

581594
#checkUnknown (where, key) {
582595
if (!this.definitions[key]) {
596+
if (internalEnv.includes(key)) {
597+
return
598+
}
583599
if (!key.includes(':')) {
584600
log.warn(`Unknown ${where} config "${where === 'cli' ? '--' : ''}${key}". This will stop working in the next major version of npm.`)
585601
return

workspaces/config/test/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,3 +1519,26 @@ t.test('catch project config prefix error', async t => {
15191519
'error', 'config', `prefix cannot be changed from project config: ${path}`,
15201520
]], 'Expected error logged')
15211521
})
1522+
1523+
t.test('invalid single hyphen warnings', async t => {
1524+
const path = t.testdir()
1525+
const logs = []
1526+
const logHandler = (...args) => logs.push(args)
1527+
process.on('log', logHandler)
1528+
t.teardown(() => process.off('log', logHandler))
1529+
const config = new Config({
1530+
npmPath: `${path}/npm`,
1531+
env: {},
1532+
argv: [process.execPath, __filename, '-ws', '-iwr'],
1533+
cwd: path,
1534+
shorthands,
1535+
definitions,
1536+
nerfDarts,
1537+
})
1538+
await config.load()
1539+
const filtered = logs.filter(l => l[0] === 'warn')
1540+
t.match(filtered, [
1541+
['warn', '-iwr is not a valid single-hyphen cli flag and will be removed in the future'],
1542+
['warn', '-ws is not a valid single-hyphen cli flag and will be removed in the future'],
1543+
], 'Warns about single hyphen configs')
1544+
})

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/npm/cli/commit/593c84921b0df963cef2ca7b13e44acc20cbd558

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy