Skip to content

Commit

Permalink
fix: add function overloads to custom flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Aug 8, 2022
1 parent 043f664 commit 12c1178
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/flags/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ export interface DurationFlagConfig extends Partial<Interfaces.OptionFlag<Durati
* }),
* }
*/
export const durationFlag = (durationConfig: DurationFlagConfig): Interfaces.OptionFlag<Duration | undefined> => {
export function durationFlag(
durationConfig: DurationFlagConfig & ({ required: true } | { default: Interfaces.Default<Duration> })
): Interfaces.OptionFlag<Duration>;
export function durationFlag(durationConfig: DurationFlagConfig): Interfaces.OptionFlag<Duration | undefined>;
export function durationFlag(
durationConfig: DurationFlagConfig
): Interfaces.OptionFlag<Duration> | Interfaces.OptionFlag<Duration | undefined> {
const { defaultValue, min, max, unit, ...baseProps } = durationConfig;
return Flags.build<Duration>({
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 || {};
Expand Down
2 changes: 1 addition & 1 deletion src/flags/orgApiVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>({
export const orgApiVersionFlag = Flags.build({
parse: async (input: string) => validate(input),
default: async () => await getDefaultFromConfig(),
description: messages.getMessage('flags.apiVersion.description'),
Expand Down
6 changes: 3 additions & 3 deletions src/flags/orgFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const getHubOrThrow = async (aliasOrUsername?: string): Promise<Org> => {
* }),
* }
*/
export const optionalOrgFlag = Flags.build<Org | undefined>({
export const optionalOrgFlag = Flags.build({
char: 'e',
parse: async (input: string | undefined) => await maybeGetOrg(input),
default: async () => await maybeGetOrg(),
Expand All @@ -89,7 +89,7 @@ export const optionalOrgFlag = Flags.build<Org | undefined>({
* }),
* }
*/
export const requiredOrgFlag = Flags.build<Org>({
export const requiredOrgFlag = Flags.build({
char: 'e',
parse: async (input: string | undefined) => await getOrgOrThrow(input),
default: async () => await getOrgOrThrow(),
Expand All @@ -115,7 +115,7 @@ export const requiredOrgFlag = Flags.build<Org>({
* }),
* }
*/
export const requiredHubFlag = Flags.build<Org>({
export const requiredHubFlag = Flags.build({
char: 'v',
parse: async (input: string | undefined) => await getHubOrThrow(input),
default: async () => await getHubOrThrow(),
Expand Down
12 changes: 9 additions & 3 deletions src/flags/salesforceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
* }),
* }
*/
export const salesforceIdFlag = (inputs: IdFlagConfig = {}): Interfaces.OptionFlag<string | undefined> => {
export function salesforceIdFlag(
inputs: IdFlagConfig & ({ required: true } | { default: Interfaces.Default<string> })
): Interfaces.OptionFlag<string>;
export function salesforceIdFlag(inputs: IdFlagConfig): Interfaces.OptionFlag<string | undefined>;
export function salesforceIdFlag(
inputs: IdFlagConfig = {}
): Interfaces.OptionFlag<string> | Interfaces.OptionFlag<string | undefined> {
const { length, startsWith, ...baseProps } = inputs;
return Flags.build<string>({
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 || {};
Expand Down

0 comments on commit 12c1178

Please sign in to comment.