Skip to content

Commit

Permalink
fix: support v2 style argument definitions (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley authored Jan 19, 2023
1 parent 0844bfc commit bba8f65
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Config as IConfig, ArchTypes, PlatformTypes, LoadOptions} from '../inter
import {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'
import {ensureArgArray, isProd} from '../util'
import ModuleLoader from '../module-loader'
import {getHelpFlagAdditions} from '../help/util'

Expand Down Expand Up @@ -786,10 +786,7 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
}
}

// v2 commands have args as an object, so we need to normalize it to an array for forwards compatibility
// @ts-ignore
const normalized = Array.isArray(c.args) ? c.args ?? [] : Object.entries(c.args ?? {}).map(([name, arg]) => ({...arg, name}))
const argsPromise = normalized.map(async a => ({
const argsPromise = ensureArgArray(c.args).map(async a => ({
name: a.name,
description: a.description,
required: a.required,
Expand Down
4 changes: 2 additions & 2 deletions src/help/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Chalk from 'chalk'
import stripAnsi = require('strip-ansi')

import {castArray, compact, sortBy} from '../util'
import {castArray, compact, ensureArgArray, sortBy} from '../util'
import * as Interfaces from '../interfaces'
import {Example} from '../interfaces/command'
import {HelpFormatter, HelpSection, HelpSectionRenderer} from './formatter'
Expand Down Expand Up @@ -41,7 +41,7 @@ export class CommandHelp extends HelpFormatter {
return v
}), f => [!f.char, f.char, f.name])

const args = (cmd.args || []).filter(a => !a.hidden)
const args = ensureArgArray(cmd.args).filter(a => !a.hidden)
const output = compact(this.sections().map(({header, generate}) => {
const body = generate({cmd, flags, args}, header)
// Generate can return a list of sections
Expand Down
3 changes: 2 additions & 1 deletion src/help/docopts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Interfaces} from '..'
import {ensureArgArray} from '../util'

type Flag = Interfaces.Command.Flag
type Flags = Flag[]
Expand Down Expand Up @@ -82,7 +83,7 @@ export class DocOpts {
public toString(): string {
const opts = this.cmd.id === '.' || this.cmd.id === '' ? [] : ['<%= command.id %>']
if (this.cmd.args) {
const a = this.cmd.args?.map(arg => `[${arg.name.toUpperCase()}]`) || []
const a = ensureArgArray(this.cmd.args).map(arg => `[${arg.name.toUpperCase()}]`) || []
opts.push(...a)
}

Expand Down
13 changes: 13 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {ArgInput} from './interfaces'

export function compact<T>(a: (T | undefined)[]): T[] {
return a.filter((a): a is T => Boolean(a))
}
Expand Down Expand Up @@ -59,3 +61,14 @@ export function sumBy<T>(arr: T[], fn: (i: T) => number): number {
export function capitalize(s: string): string {
return s ? s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() : ''
}

/**
* Ensure that the args are in an array instead of an object. This is required to ensure
* forwards compatibility with the new arg format in v2.
*
* @param args The args to ensure are in an array
* @returns ArgInput
*/
export function ensureArgArray(args?: ArgInput | {[name: string]: any}): ArgInput {
return Array.isArray(args) ? args ?? [] : Object.entries(args ?? {}).map(([name, arg]) => ({...arg, name}))
}
17 changes: 16 additions & 1 deletion test/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai'
import {maxBy, sumBy, capitalize} from '../src/util'
import {maxBy, sumBy, capitalize, ensureArgArray} from '../src/util'

describe('capitalize', () => {
it('capitalizes the string', () => {
Expand Down Expand Up @@ -33,3 +33,18 @@ describe('maxBy', () => {
expect(maxBy(arr, i => i.x)).to.equal(arr[1])
})
})

describe('ensureArgArray', () => {
it('should convert arg object to array', () => {
const args = {
foo: {name: 'foo', description: 'foo desc', required: true},
bar: {name: 'bar', description: 'bar desc'},
}
const expected = [
{name: 'foo', description: 'foo desc', required: true},
{name: 'bar', description: 'bar desc'},
]

expect(ensureArgArray(args)).to.deep.equal(expected)
})
})

0 comments on commit bba8f65

Please sign in to comment.