Skip to content

Commit

Permalink
feat: add forwards compatibility for v2 args (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jan 11, 2023
1 parent 6698b2b commit 9bc4a92
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {format} from 'util'

import {Options, Plugin as IPlugin} from '../interfaces/plugin'
import {Config as IConfig, ArchTypes, PlatformTypes, LoadOptions} from '../interfaces/config'
import {Command, CompletableOptionFlag, Hook, Hooks, PJSON, Topic} from '../interfaces'
import {ArgInput, Command, CompletableOptionFlag, Hook, Hooks, PJSON, Topic} from '../interfaces'
import * as Plugin from './plugin'
import {Debug, compact, loadJSON, collectUsableIds, getCommandIdPermutations} from './util'
import {isProd} from '../util'
Expand Down Expand Up @@ -786,7 +786,9 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
}
}

const argsPromise = (c.args || []).map(async a => ({
// v2 commands have args as an object, so we need to normalize it to an array for forwards compatibility
const normalized = (Array.isArray(c.args) ? c.args ?? [] : Object.values(c.args ?? {})) as ArgInput
const argsPromise = normalized.map(async a => ({
name: a.name,
description: a.description,
required: a.required,
Expand Down

2 comments on commit 9bc4a92

@rpastro
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdonnalley I'm a little confused how this will work since with v2 the args object doesn't have a name property. For instance, according to #539 we would have:

v1:

static args = [{name: arg1, description: 'an argument', required: true}]

v2:

  static args = {
    arg1: Args.string({description: 'an argument', required: true})
  }

Object.values for the v2 args would return the following array:

[{description: 'an argument', required: true}]

Am I missing something?

@rpastro
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead I would expect something like the following:

const normalized = (Array.isArray(c.args) ? c.args : Object.entries(c.args ?? {}).map(([key, value]) => ({ ...value, name: key ))) as ArgInput

Please sign in to comment.