From 2442072b465bc2a4a8f0bb2cc38390113745a005 Mon Sep 17 00:00:00 2001 From: azlam Date: Mon, 7 Aug 2023 16:20:38 +1000 Subject: [PATCH] fix(flags): remove unused orgFlags sfpowerscripts uses old style sfdx flags for now, and the this file is not required --- .../src/commands/orchestrator/publish.ts | 2 +- .../sfpowerscripts-cli/src/flags/orgFlags.ts | 236 ------------------ .../sfpowerscripts-cli/src/flags/sfdxflags.ts | 30 +-- 3 files changed, 12 insertions(+), 256 deletions(-) delete mode 100644 packages/sfpowerscripts-cli/src/flags/orgFlags.ts diff --git a/packages/sfpowerscripts-cli/src/commands/orchestrator/publish.ts b/packages/sfpowerscripts-cli/src/commands/orchestrator/publish.ts index 6e8bcbe6e..abf1db515 100644 --- a/packages/sfpowerscripts-cli/src/commands/orchestrator/publish.ts +++ b/packages/sfpowerscripts-cli/src/commands/orchestrator/publish.ts @@ -54,7 +54,7 @@ export default class Promote extends SfpowerscriptsCommand { description: messages.getMessage('publishPromotedOnlyFlagDescription'), dependsOn: ['devhubalias'], }), - 'devhubalias':optionalDevHubFlag, + 'devhubalias':optionalDevHubFlag, scriptpath: Flags.file({ char: 'f', description: messages.getMessage('scriptPathFlagDescription'), diff --git a/packages/sfpowerscripts-cli/src/flags/orgFlags.ts b/packages/sfpowerscripts-cli/src/flags/orgFlags.ts deleted file mode 100644 index 8d095d48f..000000000 --- a/packages/sfpowerscripts-cli/src/flags/orgFlags.ts +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright (c) 2020, salesforce.com, inc. - * All rights reserved. - * 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 } from '@oclif/core'; -import { ConfigAggregator, Messages, Org, OrgConfigProperties } from '@salesforce/core'; -import { AliasAccessor } from '@salesforce/core/lib/stateAggregator'; - -Messages.importMessagesDirectory(__dirname); -const messages = Messages.loadMessages('@dxatscale/sfpowerscripts', 'core-messages'); - -export async function maybeGetOrg(input: string): Promise; -export async function maybeGetOrg(input: undefined): Promise; -export async function maybeGetOrg(input?: string | undefined): Promise; -export async function maybeGetOrg(input?: string | undefined): Promise { - try { - return await Org.create({ aliasOrUsername: input }); - } catch (e) { - if (!input) { - return undefined; - } else { - throw e; - } - } -} - -export const maybeGetHub = async (input?: string): Promise => { - let org: Org | undefined; - // user provided input, verify the org exits - if (input) { - org = await getOrgOrThrow(input); - } else { - // no input, check config for a default - const aliasOrUsername = await getDefaultHub(false); - // if there is a default, verify the org exists - if (aliasOrUsername) { - org = await getOrgOrThrow(aliasOrUsername); - } - } - if (org) { - return ensureDevHub(org, org.getUsername()); - } else { - return undefined; - } -}; - -export const getOrgOrThrow = async (input?: string): Promise => { - const org = await maybeGetOrg(input); - if (!org) { - throw messages.createError('errors.NoDefaultEnv'); - } - - return org; -}; - -const ensureDevHub = async (org: Org, aliasOrUsername?: string): Promise => { - if (await org.determineIfDevHubOrg()) { - return org; - } - throw messages.createError('errors.NotADevHub', [aliasOrUsername ?? org.getUsername()]); -}; - -async function getDefaultHub(throwIfNotFound: false): Promise; -async function getDefaultHub(throwIfNotFound: true): Promise; -async function getDefaultHub(throwIfNotFound: boolean): Promise { - // 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; -} - -export const getHubOrThrow = async (aliasOrUsername?: string): Promise => { - const resolved = aliasOrUsername ?? (await getDefaultHub(true)); - const org = await Org.create({ aliasOrUsername: resolved, isDevHub: true }); - return ensureDevHub(org, resolved); -}; - -/** - * An optional org specified by username or alias - * Will default to the default org if one is not specified. - * Will not throw if the specified org and default do not exist - * - * @example - * - * ``` - * import { Flags } from '@salesforce/sf-plugins-core'; - * public static flags = { - * // setting length or prefix - * 'target-org': Flags.optionalOrg(), - * // adding properties - * 'flag2': Flags.optionalOrg({ - * required: true, - * description: 'flag2 description', - * }), - * } - * ``` - */ -export const optionalOrgFlag = Flags.custom({ - char: 'u', - parse: async (input: string | undefined) => maybeGetOrg(input), - default: async () => maybeGetOrg(), - defaultHelp: async (context, isWritingManifest) => { - if (isWritingManifest) { - return undefined; - } - if (context.options instanceof Org) { - const org = context.options as Org; - return org.getUsername(); - } - return (await maybeGetOrg())?.getUsername(); - }, -}); - -/** - * A required org, specified by username or alias - * Will throw if the specified org default do not exist - * Will default to the default org if one is not specified. - * Will throw if no default org exists and none is specified - * - * @example - * - * ``` - * import { Flags } from '@salesforce/sf-plugins-core'; - * public static flags = { - * // setting length or prefix - * 'target-org': Flags.requiredOrg(), - * // adding properties - * 'flag2': Flags.requiredOrg({ - * required: true, - * description: 'flag2 description', - * char: 'o' - * }), - * } - * ``` - */ -export const requiredOrgFlag = Flags.custom({ - char: 'u', - summary: messages.getMessage('flags.targetOrg.summary'), - parse: async (input: string | undefined) => getOrgOrThrow(input), - default: async () => getOrgOrThrow(), - defaultHelp: async (context, isWritingManifest) => { - if (isWritingManifest) { - return undefined; - } - if (context.options instanceof Org) { - const org = context.options as Org; - return org.getUsername(); - } - return (await maybeGetOrg())?.getUsername(); - }, - required: true, -}); - -/** - * A required org that is a devHub - * Will throw if the specified org does not exist - * Will default to the default dev hub if one is not specified - * Will 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': requiredHub(), - * // adding properties - * 'flag2': requiredHub({ - * required: true, - * description: 'flag2 description', - * char: 'h' - * }), - * } - * ``` - */ -export const requiredHubFlag = Flags.custom({ - char: 'v', - summary: messages.getMessage('flags.targetDevHubOrg.summary'), - parse: async (input: string | undefined) => getHubOrThrow(input), - default: async () => getHubOrThrow(), - defaultHelp: async (context, isWritingManifest) => { - if (isWritingManifest) { - return undefined; - } - if (context.options instanceof Org) { - const org = context.options as Org; - return org.getUsername(); - } - return (await maybeGetHub())?.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 (context, isWritingManifest) => { - if (isWritingManifest) { - return undefined; - } - if (context.options instanceof Org) { - const org = context.options as Org; - return org.getUsername(); - } - return (await maybeGetHub())?.getUsername(); - }, - required: false, -}); diff --git a/packages/sfpowerscripts-cli/src/flags/sfdxflags.ts b/packages/sfpowerscripts-cli/src/flags/sfdxflags.ts index 0748f9487..54694ac7f 100644 --- a/packages/sfpowerscripts-cli/src/flags/sfdxflags.ts +++ b/packages/sfpowerscripts-cli/src/flags/sfdxflags.ts @@ -14,7 +14,6 @@ import { Flags } from '@oclif/core'; import { Lifecycle, Messages, Org, OrgConfigProperties } from '@salesforce/core'; import { orgApiVersionFlag } from './orgApiVersion'; -import { getHubOrThrow, getOrgOrThrow, maybeGetHub, maybeGetOrg, optionalHubFlag, optionalOrgFlag, requiredHubFlag, requiredOrgFlag } from './orgFlags'; import { AliasAccessor } from '@salesforce/core/lib/stateAggregator'; /** @@ -27,13 +26,7 @@ export const orgApiVersionFlagSfdxStyle = orgApiVersionFlag({ Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@dxatscale/sfpowerscripts', 'core-messages'); -/** - * Use only for commands that maintain sfdx compatibility. - * Flag will be hidden and will show a warning if used. - * Flag does *not* set the loglevel - * - * - */ + export const loglevel = Flags.string({ description: 'logging level for this command invocation', default: 'info', @@ -89,17 +82,16 @@ export const requiredUserNameFlag = userNameFlag({ const devhubFlag = Flags.custom({ char: 'v', summary: messages.getMessage('flags.targetDevHubOrg.summary'), - parse: async (input: string | undefined) => (await getHubOrThrow(input)).getUsername(), - default: async () => (await getHubOrThrow()).getUsername(), - defaultHelp: async (context, isWritingManifest) => { - if (isWritingManifest) { - return undefined; - } - if (context.options instanceof Org) { - const org = context.options as Org; - return org.getUsername(); - } - return (await maybeGetHub())?.getUsername(); + parse: async (input: string | undefined) => { + let aliasAccessor = (await AliasAccessor.create()); + let resolvedAliasOrUserName; + if(aliasAccessor.resolveAlias(input)) + resolvedAliasOrUserName=aliasAccessor.resolveAlias(input); + else + resolvedAliasOrUserName=aliasAccessor.resolveUsername(input); + //Check if its devhub + const org = await Org.create({ aliasOrUsername: resolvedAliasOrUserName, isDevHub: true }); + return resolvedAliasOrUserName; }, });