Skip to content

Commit

Permalink
feat: optional devhub flag (and deprecated equivalent)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jan 8, 2023
1 parent 73d76f9 commit 8458fc3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { Flags } from '@oclif/core';
import { Lifecycle, Messages } from '@salesforce/core';
import { orgApiVersionFlag } from './flags/orgApiVersion';
import { optionalOrgFlag, requiredHubFlag, requiredOrgFlag } from './flags/orgFlags';
import { optionalHubFlag, optionalOrgFlag, requiredHubFlag, requiredOrgFlag } from './flags/orgFlags';

/**
* Adds an alias for the deprecated sfdx-style "apiversion" and provides a warning if it is used
Expand Down Expand Up @@ -77,6 +77,15 @@ export const requiredHubFlagWithDeprecations = requiredHubFlag({
required: true,
});

/**
* @deprecated
*/
export const optionalHubFlagWithDeprecations = optionalHubFlag({
aliases: ['targetdevhubusername'],
deprecateAliases: true,
required: false,
});

export type ArrayWithDeprecationOptions = {
// prevent invalid options from being passed
multiple?: true;
Expand Down
75 changes: 63 additions & 12 deletions src/flags/orgFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { Messages, Org, ConfigAggregator, OrgConfigProperties } from '@salesforc
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

const maybeGetOrg = async (input?: string): Promise<Org | undefined> => {
async function maybeGetOrg(input: string): Promise<Org>;
async function maybeGetOrg(input: undefined): Promise<undefined>;
async function maybeGetOrg(input?: string | undefined): Promise<Org | undefined>;
async function maybeGetOrg(input?: string | undefined): Promise<Org | undefined> {
try {
return await Org.create({ aliasOrUsername: input });
} catch (e) {
Expand All @@ -20,6 +23,15 @@ const maybeGetOrg = async (input?: string): Promise<Org | undefined> => {
throw e;
}
}
}

const maybeGetHub = async (input?: string): Promise<Org | undefined> => {
const org = await maybeGetOrg(input ?? (await getDefaultHub(false)));
if (org) {
return ensureDevHub(org, input ?? org.getUsername());
} else {
return undefined;
}
};

const getOrgOrThrow = async (input?: string): Promise<Org> => {
Expand All @@ -30,20 +42,29 @@ const getOrgOrThrow = async (input?: string): Promise<Org> => {
return org;
};

const getHubOrThrow = async (aliasOrUsername?: string): Promise<Org> => {
if (!aliasOrUsername) {
// check config for a default
const config = await ConfigAggregator.create();
aliasOrUsername = config.getInfo(OrgConfigProperties.TARGET_DEV_HUB)?.value as string;
if (!aliasOrUsername) {
throw messages.createError('errors.NoDefaultDevHub');
}
}
const org = await Org.create({ aliasOrUsername });
const ensureDevHub = async (org: Org, aliasOrUsername?: string): Promise<Org> => {
if (await org.determineIfDevHubOrg()) {
return org;
}
throw messages.createError('errors.NotADevHub', [aliasOrUsername]);
throw messages.createError('errors.NotADevHub', [aliasOrUsername ?? org.getUsername()]);
};

async function getDefaultHub(throwIfNotFound: false): Promise<string | undefined>;
async function getDefaultHub(throwIfNotFound: true): Promise<string>;
async function getDefaultHub(throwIfNotFound: boolean): Promise<string | undefined> {
// check config for a default
const config = await ConfigAggregator.create();
const aliasOrUsername = config.getInfo(OrgConfigProperties.TARGET_DEV_HUB)?.value as string;
if (throwIfNotFound && !aliasOrUsername) {
throw messages.createError('errors.NoDefaultDevHub');
}
return aliasOrUsername;
}

const getHubOrThrow = async (aliasOrUsername?: string): Promise<Org> => {
const resolved = aliasOrUsername ?? (await getDefaultHub(true));
const org = await Org.create({ aliasOrUsername: resolved });
return ensureDevHub(org, resolved);
};

/**
Expand Down Expand Up @@ -134,3 +155,33 @@ export const requiredHubFlag = Flags.custom({
defaultHelp: async () => (await getHubOrThrow())?.getUsername(),
required: true,
});

/**
* An optional org that, if present, must be a devHub
* Will throw if the specified org does not exist
* Will default to the default dev hub if one is not specified
* Will NOT throw if no default deb hub exists and none is specified
*
* @example
*
* ```
* import { Flags } from '@salesforce/sf-plugins-core';
* public static flags = {
* // setting length or prefix
* 'target-org': optionalHubFlag(),
* // adding properties
* 'flag2': optionalHubFlag({
* description: 'flag2 description',
* char: 'h'
* }),
* }
* ```
*/
export const optionalHubFlag = Flags.custom({
char: 'v',
summary: messages.getMessage('flags.targetDevHubOrg.summary'),
parse: async (input: string | undefined) => maybeGetHub(input),
default: async () => maybeGetHub(),
defaultHelp: async () => (await maybeGetHub())?.getUsername(),
required: false,
});

0 comments on commit 8458fc3

Please sign in to comment.