From 69fd42440cd1c11d1dd4bbf41769e4725e128a87 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Mon, 6 Jul 2020 18:54:17 +0300 Subject: [PATCH] refactor(parse-cli-args): add opts.fallbackCommand ref #2668 ref #2169 PR #2669 --- .changeset/lemon-otters-talk.md | 5 +++++ .changeset/nasty-walls-destroy.md | 6 ++---- packages/parse-cli-args/src/index.ts | 10 +++++----- packages/parse-cli-args/test/index.ts | 15 ++++++++++++++- packages/pnpm/src/cmd/index.ts | 3 ++- packages/pnpm/src/parseCliArgs.ts | 4 ++-- 6 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 .changeset/lemon-otters-talk.md diff --git a/.changeset/lemon-otters-talk.md b/.changeset/lemon-otters-talk.md new file mode 100644 index 0000000000..966cd862c4 --- /dev/null +++ b/.changeset/lemon-otters-talk.md @@ -0,0 +1,5 @@ +--- +"pnpm": minor +--- + +Any unknown command is assumed to be a script. So `pnpm foo` becomes `pnpm run foo`. diff --git a/.changeset/nasty-walls-destroy.md b/.changeset/nasty-walls-destroy.md index ecaeb1b1bc..bbb71e245e 100644 --- a/.changeset/nasty-walls-destroy.md +++ b/.changeset/nasty-walls-destroy.md @@ -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. diff --git a/packages/parse-cli-args/src/index.ts b/packages/parse-cli-args/src/index.ts index 1094f84289..b3fb568d56 100644 --- a/packages/parse-cli-args/src/index.ts +++ b/packages/parse-cli-args/src/index.ts @@ -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, renamedOptions?: Record, shorthandsByCommandName: Record>, universalOptionsTypes: Record, @@ -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, diff --git a/packages/parse-cli-args/test/index.ts b/packages/parse-cli-args/test/index.ts index d289c8cf78..271d9ebc73 100644 --- a/packages/parse-cli-args/test/index.ts +++ b/packages/parse-cli-args/test/index.ts @@ -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() +}) \ No newline at end of file diff --git a/packages/pnpm/src/cmd/index.ts b/packages/pnpm/src/cmd/index.ts index 6b47a43488..ae3e82ae6c 100644 --- a/packages/pnpm/src/cmd/index.ts +++ b/packages/pnpm/src/cmd/index.ts @@ -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 } diff --git a/packages/pnpm/src/parseCliArgs.ts b/packages/pnpm/src/parseCliArgs.ts index e89ebacd61..4353b44097 100644 --- a/packages/pnpm/src/parseCliArgs.ts +++ b/packages/pnpm/src/parseCliArgs.ts @@ -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,