From 12c117837a08bc8cf8eff41832f95a19d3386b18 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 8 Aug 2022 15:37:56 -0600 Subject: [PATCH] fix: add function overloads to custom flags --- src/flags/duration.ts | 12 +++++++++--- src/flags/orgApiVersion.ts | 2 +- src/flags/orgFlags.ts | 6 +++--- src/flags/salesforceId.ts | 12 +++++++++--- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/flags/duration.ts b/src/flags/duration.ts index f7f1d36f3..5f452a17c 100644 --- a/src/flags/duration.ts +++ b/src/flags/duration.ts @@ -37,14 +37,20 @@ export interface DurationFlagConfig extends Partial => { +export function durationFlag( + durationConfig: DurationFlagConfig & ({ required: true } | { default: Interfaces.Default }) +): Interfaces.OptionFlag; +export function durationFlag(durationConfig: DurationFlagConfig): Interfaces.OptionFlag; +export function durationFlag( + durationConfig: DurationFlagConfig +): Interfaces.OptionFlag | Interfaces.OptionFlag { const { defaultValue, min, max, unit, ...baseProps } = durationConfig; - return Flags.build({ + return Flags.build({ ...baseProps, parse: async (input: string) => validate(input, { min, max, unit }), default: defaultValue ? async () => toDuration(defaultValue, unit) : undefined, })(); -}; +} const validate = (input: string, config: DurationFlagConfig): Duration => { const { min, max, unit } = config || {}; diff --git a/src/flags/orgApiVersion.ts b/src/flags/orgApiVersion.ts index 1d4188372..b8b63a898 100644 --- a/src/flags/orgApiVersion.ts +++ b/src/flags/orgApiVersion.ts @@ -24,7 +24,7 @@ export const maxDeprecatedUrl = 'https://help.salesforce.com/s/articleView?id=00 * CAVEAT: unlike the apiversion flag on sfdxCommand, this does not set the version on the org/connection * We leave this up to the plugins to implement */ -export const orgApiVersionFlag = Flags.build({ +export const orgApiVersionFlag = Flags.build({ parse: async (input: string) => validate(input), default: async () => await getDefaultFromConfig(), description: messages.getMessage('flags.apiVersion.description'), diff --git a/src/flags/orgFlags.ts b/src/flags/orgFlags.ts index 02817310f..ed0ba928f 100644 --- a/src/flags/orgFlags.ts +++ b/src/flags/orgFlags.ts @@ -63,7 +63,7 @@ const getHubOrThrow = async (aliasOrUsername?: string): Promise => { * }), * } */ -export const optionalOrgFlag = Flags.build({ +export const optionalOrgFlag = Flags.build({ char: 'e', parse: async (input: string | undefined) => await maybeGetOrg(input), default: async () => await maybeGetOrg(), @@ -89,7 +89,7 @@ export const optionalOrgFlag = Flags.build({ * }), * } */ -export const requiredOrgFlag = Flags.build({ +export const requiredOrgFlag = Flags.build({ char: 'e', parse: async (input: string | undefined) => await getOrgOrThrow(input), default: async () => await getOrgOrThrow(), @@ -115,7 +115,7 @@ export const requiredOrgFlag = Flags.build({ * }), * } */ -export const requiredHubFlag = Flags.build({ +export const requiredHubFlag = Flags.build({ char: 'v', parse: async (input: string | undefined) => await getHubOrThrow(input), default: async () => await getHubOrThrow(), diff --git a/src/flags/salesforceId.ts b/src/flags/salesforceId.ts index f7f5bfee9..eadadf43e 100644 --- a/src/flags/salesforceId.ts +++ b/src/flags/salesforceId.ts @@ -40,14 +40,20 @@ export interface IdFlagConfig extends Partial> { * }), * } */ -export const salesforceIdFlag = (inputs: IdFlagConfig = {}): Interfaces.OptionFlag => { +export function salesforceIdFlag( + inputs: IdFlagConfig & ({ required: true } | { default: Interfaces.Default }) +): Interfaces.OptionFlag; +export function salesforceIdFlag(inputs: IdFlagConfig): Interfaces.OptionFlag; +export function salesforceIdFlag( + inputs: IdFlagConfig = {} +): Interfaces.OptionFlag | Interfaces.OptionFlag { const { length, startsWith, ...baseProps } = inputs; - return Flags.build({ + return Flags.build({ char: 'i', ...baseProps, parse: async (input: string) => validate(input, { length, startsWith }), })(); -}; +} const validate = (input: string, config?: IdFlagConfig): string => { const { length, startsWith } = config || {};