Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support targeting browser in "open" command #211

Merged
merged 2 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
{
"command": "force:org:open",
"plugin": "@salesforce/plugin-org",
"flags": ["path", "urlonly", "json", "loglevel", "targetusername", "apiversion"]
"flags": ["path", "urlonly", "json", "loglevel", "targetusername", "apiversion", "browser"]
}
]
6 changes: 4 additions & 2 deletions messages/open.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"description": "open your default scratch org, or another specified org\nTo open a specific page, specify the portion of the URL after \"yourInstance.salesforce.com/\" as --path.\nFor example, specify \"--path lightning\" to open Lightning Experience, or specify \"--path /apex/YourPage\" to open a Visualforce page.\nTo generate a URL but not launch it in your browser, specify --urlonly.",
"description": "open your default scratch org, or another specified org\nTo open a specific page, specify the portion of the URL after \"yourInstance.salesforce.com/\" as --path.\nFor example, specify \"--path lightning\" to open Lightning Experience, or specify \"--path /apex/YourPage\" to open a Visualforce page.\nTo generate a URL but not launch it in your browser, specify --urlonly.\nTo open in a specific browser, use the --browser parameter. Supported browsers are \"chrome\", \"edge\", and \"firefox\". If you don't specify --browser, the org opens in your default browser.",
"examples": [
"sfdx force:org:open",
"sfdx force:org:open -u [email protected]",
"sfdx force:org:open -u MyTestOrg1",
"sfdx force:org:open -r -p lightning"
"sfdx force:org:open -r -p lightning",
"sfdx force:org:open -u [email protected] -b firefox"
],
"browser": "browser where the org opens",
"cliPath": "navigation URL path",
"urlonly": "display navigation URL, but don’t launch browser",
"containerAction": "You are in a headless environment. To access the org %s, open this URL in a browser:\n\n%s",
Expand Down
14 changes: 13 additions & 1 deletion src/commands/force/org/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EOL } from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages, Org, SfdcUrl, SfdxError } from '@salesforce/core';
import { Duration, Env } from '@salesforce/kit';
import open = require('open');
import { openUrl } from '../../../shared/utils';

Messages.importMessagesDirectory(__dirname);
Expand All @@ -20,6 +21,12 @@ export class OrgOpenCommand extends SfdxCommand {
public static readonly examples = messages.getMessage('examples').split(EOL);
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
browser: flags.string({
char: 'b',
description: messages.getMessage('browser'),
options: ['chrome', 'edge', 'firefox'], // These are ones supported by "open" package
exclusive: ['urlonly'],
}),
path: flags.string({
char: 'p',
description: messages.getMessage('cliPath'),
Expand Down Expand Up @@ -71,7 +78,12 @@ export class OrgOpenCommand extends SfdxCommand {
}
throw SfdxError.wrap(err);
}
await openUrl(url);

const openOptions = this.flags.browser
? { app: { name: open.apps[this.flags.browser as string] as open.AppName } }
: {};

await openUrl(url, openOptions);
return output;
}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const getAliasByUsername = async (username: string): Promise<string> => {
return keys?.length ? keys[keys.length - 1] : undefined;
};

export const openUrl = async (url: string): Promise<ChildProcess> => {
return open(url);
export const openUrl = async (url: string, options: open.Options): Promise<ChildProcess> => {
return open(url, options);
};
38 changes: 38 additions & 0 deletions test/commands/force/org/open.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const orgId = '000000000000000';
const username = '[email protected]';
const testPath = '/lightning/whatever';
const testInstance = 'https://cs1.my.salesforce.com';
const testBrowser = 'firefox';
const accessToken = 'testAccessToken';
const expectedDefaultUrl = `${testInstance}/secur/frontdoor.jsp?sid=${accessToken}`;
const expectedUrl = `${expectedDefaultUrl}&retURL=${encodeURIComponent(testPath)}`;
Expand Down Expand Up @@ -189,4 +190,41 @@ describe('open commands', () => {
expect(spies.get('open').callCount).to.equal(0);
});
});

describe('browser argument', () => {
test
.do(() => {
spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').resolves('1.1.1.1'));
})
.stdout()
.command(['force:org:open', '--targetusername', username, '--path', testPath])
.it('calls open with no browser argument', () => {
expect(spies.get('resolver').callCount).to.equal(1);
expect(spies.get('open').callCount).to.equal(1);
expect(spies.get('open').args[0][1]).to.eql({});
});

test
.do(() => {
spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').resolves('1.1.1.1'));
})
.stdout()
.command(['force:org:open', '--targetusername', username, '--path', testPath, '-b', testBrowser])
.it('calls open with a browser argument', () => {
expect(spies.get('resolver').callCount).to.equal(1);
expect(spies.get('open').callCount).to.equal(1);
expect(spies.get('open').args[0][1]).to.not.eql({});
});

test
.do(() => {
spies.set('resolver', stubMethod($$.SANDBOX, MyDomainResolver.prototype, 'resolve').resolves('1.1.1.1'));
})
.stdout()
.command(['force:org:open', '--targetusername', username, '--path', testPath, '-b', 'duff'])
.it('does not call open as passed unknown browser name', () => {
expect(spies.get('resolver').callCount).to.equal(0);
expect(spies.get('open').callCount).to.equal(0);
});
});
});