Skip to content

Commit

Permalink
feat: update custom flag definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Aug 23, 2022
1 parent 1e90779 commit 828daac
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 39 deletions.
26 changes: 9 additions & 17 deletions src/flags/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Flags, Interfaces } from '@oclif/core';
import { Flags } from '@oclif/core';
import { Messages } from '@salesforce/core';
import { Duration } from '@salesforce/kit';

Expand All @@ -13,12 +13,12 @@ const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages'

type DurationUnit = Lowercase<keyof typeof Duration.Unit>;

export interface DurationFlagConfig extends Partial<Interfaces.OptionFlag<Duration>> {
export type DurationFlagConfig = {
unit: Required<DurationUnit>;
defaultValue?: number;
min?: number;
max?: number;
}
};

/**
* Duration flag with built-in default and min/max validation
Expand All @@ -37,20 +37,12 @@ export interface DurationFlagConfig extends Partial<Interfaces.OptionFlag<Durati
* }),
* }
*/
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({
...baseProps,
parse: async (input: string) => validate(input, { min, max, unit }),
default: defaultValue ? async () => toDuration(defaultValue, unit) : undefined,
})();
}
export const durationFlag = Flags.custom<Duration, DurationFlagConfig>({
parse: async (input, _, opts) => validate(input, opts),
default: async (context) => {
return context.options.defaultValue ? toDuration(context.options.defaultValue, context.options.unit) : undefined;
},
});

const validate = (input: string, config: DurationFlagConfig): Duration => {
const { min, max, unit } = config || {};
Expand Down
4 changes: 2 additions & 2 deletions src/flags/orgApiVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ 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({
parse: async (input: string) => validate(input),
export const orgApiVersionFlag = Flags.custom({
parse: async (input) => 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({
export const optionalOrgFlag = Flags.custom({
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({
* }),
* }
*/
export const requiredOrgFlag = Flags.build({
export const requiredOrgFlag = Flags.custom({
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({
* }),
* }
*/
export const requiredHubFlag = Flags.build({
export const requiredHubFlag = Flags.custom({
char: 'v',
parse: async (input: string | undefined) => await getHubOrThrow(input),
default: async () => await getHubOrThrow(),
Expand Down
24 changes: 7 additions & 17 deletions src/flags/salesforceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Flags, Interfaces } from '@oclif/core';
import { Flags } from '@oclif/core';
import { Messages, sfdc } from '@salesforce/core';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
export type IdFlagConfig = {
/**
* Can specify if the version must be 15 or 18 characters long. Leave blank to allow either 15 or 18.
*/
Expand All @@ -19,7 +19,7 @@ export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
* If the ID belongs to a certain sobject type, specify the 3 character prefix.
*/
startsWith?: string;
}
};

/**
* Id flag with built-in validation. Short character is `i`
Expand All @@ -40,20 +40,10 @@ export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
* }),
* }
*/
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({
char: 'i',
...baseProps,
parse: async (input: string) => validate(input, { length, startsWith }),
})();
}
export const salesforceIdFlag = Flags.custom<string, IdFlagConfig>({
parse: async (input, _ctx, opts) => validate(input, opts),
char: 'i',
});

const validate = (input: string, config?: IdFlagConfig): string => {
const { length, startsWith } = config || {};
Expand Down

0 comments on commit 828daac

Please sign in to comment.