refactor(parse-cli-args): add opts.fallbackCommand

ref #2668
ref #2169
PR #2669
This commit is contained in:
Zoltan Kochan authored and GitHub committed 2020-07-06 18:54:17 +03:00
1 parent 09b777f8d1
commit 69fd42440c
6 files changed
+30 -13

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"pnpm": minor
---
Any unknown command is assumed to be a script. So `pnpm foo` becomes `pnpm run foo`.
+2 -4
View File
@@ -1,7 +1,5 @@
---
"@pnpm/parse-cli-args": major
"@pnpm/parse-cli-args": minor
---
New required option added: `knownCommands`.
Any unknown command is assumed to be a script. So `pnpm foo` becomes `pnpm run foo`.
New option added: `fallbackCommand`. If set, this command is added to the beginning of any unknown query.
+5 -5
View File
@@ -20,9 +20,9 @@ export interface ParsedCliArgs {
export default async function parseCliArgs (
opts: {
fallbackCommand?: string,
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>,
@@ -59,10 +59,10 @@ export default async function parseCliArgs (
const recursiveCommandUsed = RECURSIVE_CMDS.has(noptExploratoryResults.argv.remain[0])
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')
if (commandName && !cmd && opts.fallbackCommand) {
cmd = opts.fallbackCommand
commandName = opts.fallbackCommand
inputArgv.unshift(opts.fallbackCommand)
}
const types = {
...opts.universalOptionsTypes,
+14 -1
View File
@@ -6,7 +6,6 @@ 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: {},
@@ -196,6 +195,8 @@ test('use command-specific shorthands', async (t) => {
test('any unknown command is treated as a script', async (t) => {
const { options, cmd, params } = await parseCliArgs({
...DEFAULT_OPTS,
fallbackCommand: 'run',
getCommandLongName: () => null,
universalOptionsTypes: { filter: [String, Array] },
}, ['foo', '--recursive'])
t.equal(cmd, 'run')
@@ -203,3 +204,15 @@ test('any unknown command is treated as a script', async (t) => {
t.ok(options['recursive'])
t.end()
})
test("don't use the fallback command if no command is present", async (t) => {
const { cmd, params } = await parseCliArgs({
...DEFAULT_OPTS,
fallbackCommand: 'run',
getCommandLongName: () => null,
universalOptionsTypes: { filter: [String, Array] },
}, [])
t.equal(cmd, null)
t.deepEqual(params, [])
t.end()
})
+2 -1
View File
@@ -145,7 +145,8 @@ export function getRCOptionsTypes (commandName: string | null) {
}
export function getCommandFullName (commandName: string) {
return aliasToFullName.get(commandName) || commandName
return aliasToFullName.get(commandName) ||
(handlerByCommandName[commandName] ? commandName : null)
}
export { shorthandsByCommandName }
+2 -2
View File
@@ -1,5 +1,5 @@
import parseCliArgsLib from '@pnpm/parse-cli-args'
import pnpmCmds, {
import {
getCliOptionsTypes,
getCommandFullName,
GLOBAL_OPTIONS,
@@ -16,9 +16,9 @@ const RENAMED_OPTIONS = {
export default function parseCliArgs (inputArgv: string[]) {
return parseCliArgsLib({
fallbackCommand: 'run',
getCommandLongName: getCommandFullName,
getTypesByCommandName: getCliOptionsTypes,
knownCommands: new Set(Object.keys(pnpmCmds)),
renamedOptions: RENAMED_OPTIONS,
shorthandsByCommandName,
universalOptionsTypes: GLOBAL_OPTIONS,