feat: any unknown command is assumed to be a script

close #2169
PR #2668
This commit is contained in:
Zoltan Kochan
2020-07-06 00:23:08 +03:00
committed by GitHub
parent 944a1eae8e
commit 09b777f8d1
5 changed files with 32 additions and 6 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/parse-cli-args": major
---
New required option added: `knownCommands`.
Any unknown command is assumed to be a script. So `pnpm foo` becomes `pnpm run foo`.

View File

@@ -22,6 +22,7 @@ export default async function parseCliArgs (
opts: {
getCommandLongName: (commandName: string) => string | null,
getTypesByCommandName: (commandName: string) => object,
knownCommands: Set<string>,
renamedOptions?: Record<string, string>,
shorthandsByCommandName: Record<string, Record<string, string | string[]>>,
universalOptionsTypes: Record<string, unknown>,
@@ -56,8 +57,13 @@ export default async function parseCliArgs (
}
const recursiveCommandUsed = RECURSIVE_CMDS.has(noptExploratoryResults.argv.remain[0])
const commandName = getCommandName(noptExploratoryResults.argv.remain)
let commandName = getCommandName(noptExploratoryResults.argv.remain)
let cmd = commandName ? opts.getCommandLongName(commandName) : null
if (cmd && !opts.knownCommands.has(cmd)) {
cmd = 'run'
commandName = 'run'
inputArgv.unshift('run')
}
const types = {
...opts.universalOptionsTypes,
...opts.getTypesByCommandName(commandName),

View File

@@ -6,6 +6,7 @@ import test = require('tape')
const DEFAULT_OPTS = {
getCommandLongName: (commandName: string) => commandName,
getTypesByCommandName: (commandName: string) => ({}),
knownCommands: new Set(['update', 'add', 'install', 'outdated']),
renamedOptions: { 'prefix': 'dir' },
shorthandsByCommandName: {},
universalOptionsTypes: {},
@@ -89,8 +90,8 @@ test('command is used recursively', async (t) => {
const { cmd, options } = await parseCliArgs({
...DEFAULT_OPTS,
universalOptionsTypes: {},
}, ['recursive', 'foo'])
t.equal(cmd, 'foo')
}, ['recursive', 'outdated'])
t.equal(cmd, 'outdated')
t.equal(options.recursive, true)
t.end()
})
@@ -191,3 +192,14 @@ test('use command-specific shorthands', async (t) => {
t.ok(options['dev'])
t.end()
})
test('any unknown command is treated as a script', async (t) => {
const { options, cmd, params } = await parseCliArgs({
...DEFAULT_OPTS,
universalOptionsTypes: { filter: [String, Array] },
}, ['foo', '--recursive'])
t.equal(cmd, 'run')
t.deepEqual(params, ['foo'])
t.ok(options['recursive'])
t.end()
})

View File

@@ -1,5 +1,5 @@
import parseCliArgsLib from '@pnpm/parse-cli-args'
import {
import pnpmCmds, {
getCliOptionsTypes,
getCommandFullName,
GLOBAL_OPTIONS,
@@ -18,6 +18,7 @@ export default function parseCliArgs (inputArgv: string[]) {
return parseCliArgsLib({
getCommandLongName: getCommandFullName,
getTypesByCommandName: getCliOptionsTypes,
knownCommands: new Set(Object.keys(pnpmCmds)),
renamedOptions: RENAMED_OPTIONS,
shorthandsByCommandName,
universalOptionsTypes: GLOBAL_OPTIONS,

View File

@@ -70,10 +70,10 @@ test('pass through to npm with all the args', async t => {
test('pnpm fails when an unsupported command is used', async (t) => {
const project = prepare(t)
const { status, stderr } = execPnpmSync(['unsupported-command'])
const { status, stdout } = execPnpmSync(['unsupported-command'])
t.equal(status, 1, 'command failed')
t.ok(stderr.toString().includes("Unknown command 'unsupported-command'"))
t.ok(stdout.toString().includes('Missing script: unsupported-command'))
})
test('pnpm fails when no command is specified', async (t) => {