Skip to content

Commit 61a7fcc

Browse files
authored
Merge pull request #166 from salesforcecli/org_open_refactoring
chore: using sfdx-core utill function
2 parents 34cf7cc + 58f2eb5 commit 61a7fcc

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dependencies": {
88
"@oclif/config": "^1",
99
"@salesforce/command": "^3.0.3",
10-
"@salesforce/core": "^2.17.0",
10+
"@salesforce/core": "^2.27.0",
1111
"@salesforce/kit": "^1.4.5",
1212
"open": "8.2.1",
1313
"tslib": "^2"

src/commands/force/org/open.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import { EOL } from 'os';
8-
import { URL } from 'url';
98

109
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
11-
import { Messages, Org, MyDomainResolver, SfdxError, sfdc } from '@salesforce/core';
12-
import { Env, Duration } from '@salesforce/kit';
10+
import { Messages, Org, SfdcUrl, SfdxError } from '@salesforce/core';
11+
import { Duration, Env } from '@salesforce/kit';
1312
import { openUrl } from '../../../shared/utils';
1413

1514
Messages.importMessagesDirectory(__dirname);
@@ -51,7 +50,22 @@ export class OrgOpenCommand extends SfdxCommand {
5150
return output;
5251
}
5352
// we actually need to open the org
54-
await this.checkLightningDomain(url);
53+
try {
54+
this.ux.startSpinner(messages.getMessage('domainWaiting'));
55+
const sfdcUrl = new SfdcUrl(url);
56+
await sfdcUrl.checkLightningDomain();
57+
} catch (err) {
58+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
59+
if (err.message?.includes('timeout')) {
60+
const domain = `https://${/https?:\/\/([^.]*)/.exec(url)[1]}.lightning.force.com`;
61+
const timeout = new Duration(new Env().getNumber('SFDX_DOMAIN_RETRY', 240), Duration.Unit.SECONDS);
62+
this.logger.debug(`Did not find IP for ${domain} after ${timeout.seconds} seconds`);
63+
throw new SfdxError(messages.getMessage('domainTimeoutError'), 'domainTimeoutError', [
64+
messages.getMessage('domainTimeoutAction'),
65+
]);
66+
}
67+
throw SfdxError.wrap(err);
68+
}
5569
await openUrl(url);
5670
return output;
5771
}
@@ -64,32 +78,6 @@ export class OrgOpenCommand extends SfdxCommand {
6478
const instanceUrlClean = instanceUrl.replace(/\/$/, '');
6579
return `${instanceUrlClean}/secur/frontdoor.jsp?sid=${accessToken}`;
6680
}
67-
68-
private async checkLightningDomain(url: string): Promise<void> {
69-
const domain = `https://${/https?:\/\/([^.]*)/.exec(url)[1]}.lightning.force.com`;
70-
const timeout = new Duration(new Env().getNumber('SFDX_DOMAIN_RETRY', 240), Duration.Unit.SECONDS);
71-
if (sfdc.isInternalUrl(url) || timeout.seconds === 0) {
72-
return;
73-
}
74-
75-
const resolver = await MyDomainResolver.create({
76-
url: new URL(domain),
77-
timeout,
78-
frequency: new Duration(1, Duration.Unit.SECONDS),
79-
});
80-
this.ux.startSpinner(messages.getMessage('domainWaiting'));
81-
82-
try {
83-
const ip = await resolver.resolve();
84-
this.logger.debug(`Found IP ${ip} for ${domain}`);
85-
return;
86-
} catch (error) {
87-
this.logger.debug(`Did not find IP for ${domain} after ${timeout.seconds} seconds`);
88-
throw new SfdxError(messages.getMessage('domainTimeoutError'), 'domainTimeoutError', [
89-
messages.getMessage('domainTimeoutAction'),
90-
]);
91-
}
92-
}
9381
}
9482

9583
interface OrgOpenOutput {

test/commands/force/org/open.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ describe('open commands', () => {
103103

104104
test
105105
.do(() => {
106-
spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').rejects());
106+
spies.set(
107+
'resolver',
108+
stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').throws({ message: 'timeout' })
109+
);
107110
})
108111
.stdout()
109112
.command(['force:org:open', '--json', '--targetusername', username, '--path', testPath])
@@ -173,7 +176,10 @@ describe('open commands', () => {
173176

174177
test
175178
.do(() => {
176-
spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').rejects());
179+
spies.set(
180+
'resolver',
181+
stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').throws({ message: 'timeout' })
182+
);
177183
})
178184
.stderr()
179185
.command(['force:org:open', '--targetusername', username, '--path', testPath])

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@
627627
chalk "^2.4.2"
628628
cli-ux "^4.9.3"
629629

630-
"@salesforce/core@^2.16.3", "@salesforce/core@^2.17.0", "@salesforce/core@^2.2.0", "@salesforce/core@^2.24.0":
630+
"@salesforce/core@^2.16.3", "@salesforce/core@^2.2.0", "@salesforce/core@^2.24.0", "@salesforce/core@^2.27.0":
631631
version "2.27.0"
632632
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-2.27.0.tgz#47c036a5195839cd0efae4b3f61e551274ff66a0"
633633
integrity sha512-T+fAkcSVCWn8OC/hJ5cJT1wWwUYzhQIc8xhKA3K/yLib5Y2WV9goD9D+35oZqikJRkHGiKv2uN9DsR4ip4WwXg==

0 commit comments

Comments
 (0)