diff --git a/src/common.ts b/src/common.ts index 89b77619..3d8e524a 100644 --- a/src/common.ts +++ b/src/common.ts @@ -15,28 +15,27 @@ const messages = Messages.loadMessages('@salesforce/plugin-auth', 'messages'); const resolveLoginUrl = async (instanceUrl?: string): Promise => { const logger = await Logger.child('Common', { tag: 'resolveLoginUrl' }); - if (instanceUrl) { - throwIfLightning(instanceUrl); - return instanceUrl; - } - let loginUrl: string; + const loginUrl = instanceUrl ?? (await getLoginUrl(logger)); + throwIfLightning(loginUrl); + logger.debug(`loginUrl: ${loginUrl}`); + return loginUrl; +}; + +/** try to get url from project if there is one, otherwise use the default production URL */ +const getLoginUrl = async (logger: Logger): Promise => { try { const project = await SfProject.resolve(); const projectJson = await project.resolveProjectConfig(); - loginUrl = getString(projectJson, 'sfdcLoginUrl', SfdcUrl.PRODUCTION); + return getString(projectJson, 'sfdcLoginUrl', SfdcUrl.PRODUCTION); } catch (err) { const message: string = (isObject(err) ? Reflect.get(err, 'message') ?? err : err) as string; logger.debug(`error occurred while trying to determine loginUrl: ${message}`); - loginUrl = SfdcUrl.PRODUCTION; + return SfdcUrl.PRODUCTION; } - throwIfLightning(loginUrl); - - logger.debug(`loginUrl: ${loginUrl}`); - return loginUrl; }; - -const throwIfLightning = (urlString?: string): void => { - if (urlString?.match(/\.lightning\..*force\.com/)) { +const throwIfLightning = (urlString: string): void => { + const url = new SfdcUrl(urlString); + if (url.isLightningDomain() || (url.isInternalUrl() && url.origin.includes('.lightning.'))) { throw new SfError(messages.getMessage('lightningInstanceUrl'), 'LightningDomain', [ messages.getMessage('flags.instance-url.description'), ]);