-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathorgOpenCommandBase.ts
More file actions
85 lines (72 loc) · 3.06 KB
/
Copy pathorgOpenCommandBase.ts
File metadata and controls
85 lines (72 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* 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 { platform } from 'node:os';
import { apps } from 'open';
import { SfCommand } from '@salesforce/sf-plugins-core';
import { Connection, Messages, Org, SfdcUrl, SfError } from '@salesforce/core';
import { env } from '@salesforce/kit';
import utils, { handleDomainError } from './orgOpenUtils.js';
import { type OrgOpenOutput } from './orgTypes.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'open');
type OrgOpenFlags = {
'url-only': boolean;
browser?: 'chrome' | 'firefox' | 'edge';
path?: string;
private: boolean;
};
export abstract class OrgOpenCommandBase<T> extends SfCommand<T> {
public static enableJsonFlag = true;
// Set by concrete classes in `run()`
protected org!: Org;
protected connection!: Connection;
protected async openOrgUI(flags: OrgOpenFlags, frontDoorUrl: string, retUrl?: string): Promise<OrgOpenOutput> {
const orgId = this.org.getOrgId();
const url = `${frontDoorUrl}${
retUrl ? `&${frontDoorUrl.includes('.jsp?otp=') ? `startURL=${retUrl}` : `retURL=${retUrl}`}` : ''
}`;
// TODO: better typings in sfdx-core for orgs read from auth files
const username = this.org.getUsername() as string;
const output = { orgId, url, username };
// NOTE: Deliberate use of `||` here since getBoolean() defaults to false, and we need to consider both env vars.
const containerMode = env.getBoolean('SF_CONTAINER_MODE') || env.getBoolean('SFDX_CONTAINER_MODE');
// security warning only for --url-only OR containerMode
if (flags['url-only'] || containerMode) {
const sharedMessages = Messages.loadMessages('@salesforce/plugin-org', 'messages');
this.warn(sharedMessages.getMessage('SecurityWarning'));
this.log('');
}
if (containerMode) {
// instruct the user that they need to paste the URL into the browser
this.styledHeader('Action Required!');
this.log(messages.getMessage('containerAction', [orgId, url]));
return output;
}
if (flags['url-only']) {
// this includes the URL
this.logSuccess(messages.getMessage('humanSuccess', [orgId, username, url]));
return output;
}
this.logSuccess(messages.getMessage('humanSuccessNoUrl', [orgId, username]));
// we actually need to open the org
try {
this.spinner.start(messages.getMessage('domainWaiting'));
await new SfdcUrl(url).checkLightningDomain();
this.spinner.stop();
} catch (err) {
handleDomainError(err, url, env);
}
const cp = await utils.openUrl(url, {
...(flags.browser ? { app: { name: apps[flags.browser] } } : {}),
...(flags.private ? { newInstance: platform() === 'darwin', app: { name: apps.browserPrivate } } : {}),
});
cp.on('error', (err) => {
throw SfError.wrap(err);
});
return output;
}
}