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

fix: return environments keyed by environment type #148

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"author": "Salesforce",
"bugs": "https://github.com/salesforcecli/cli/issues",
"dependencies": {
"@oclif/core": "^0.5.38",
"@salesforce/core": "3.6.1",
"@salesforce/sf-plugins-core": "^0.0.23",
"@oclif/core": "^0.5.39",
"@salesforce/core": "3.6.2",
"@salesforce/sf-plugins-core": "^0.0.25",
"change-case": "^4.1.2",
"cli-ux": "^5.6.3",
"open": "^8.2.0",
Expand All @@ -21,7 +21,7 @@
"@salesforce/dev-scripts": "^0.9.18",
"@salesforce/plugin-command-reference": "^2.0.6",
"@salesforce/plugin-config": "^2.2.0",
"@salesforce/plugin-functions": "^0.2.38",
"@salesforce/plugin-functions": "^0.2.47",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "1.3.21",
"@types/shelljs": "^0.8.8",
Expand Down
9 changes: 6 additions & 3 deletions schemas/env-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"$ref": "#/definitions/Environments",
"definitions": {
"Environments": {
"type": "array",
"items": {
"$ref": "#/definitions/JsonObject"
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"$ref": "#/definitions/JsonObject"
}
}
},
"JsonObject": {
Expand Down
50 changes: 42 additions & 8 deletions src/commands/env/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@
import { Flags } from '@oclif/core';
import { cli } from 'cli-ux';
import { Messages } from '@salesforce/core';
import { SfCommand, JsonObject, SfHook } from '@salesforce/sf-plugins-core';
import { SfCommand, JsonObject, SfHook, EnvList as Env } from '@salesforce/sf-plugins-core';
import { toKey, toValue } from '../../utils';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-env', 'list');

export type Environments = JsonObject[];
const envTypeValues = Object.keys(Env.EnvType);
const envOrderBy = (a: Env.Table<JsonObject>, b: Env.Table<JsonObject>): number => {
// both a && b are well known
if (envTypeValues.includes(a.type) && envTypeValues.includes(a.type)) {
if (a.type === Env.EnvType.salesforceOrgs && b.type !== Env.EnvType.salesforceOrgs) return -1;
if (a.type !== Env.EnvType.salesforceOrgs && b.type === Env.EnvType.salesforceOrgs) return 1;
if (a.type === Env.EnvType.scratchOrgs && b.type !== Env.EnvType.scratchOrgs) return -1;
return 1;
}
// both a && b are user defined - use natural sort
if (!envTypeValues.includes(a.type) && !envTypeValues.includes(a.type)) {
return a.type.localeCompare(b.type);
}
// well known always come before user defined
if (envTypeValues.includes(a.type)) return -1;
return 1;
};

export type Environments = {
[type: string]: JsonObject[];
};

export default class EnvList extends SfCommand<Environments> {
public static readonly summary = messages.getMessage('summary');
Expand Down Expand Up @@ -73,20 +93,21 @@ export default class EnvList extends SfCommand<Environments> {
sort: this.flags.sort,
};

const final: Environments = [];
let final: Environments = {};

const results = await SfHook.run(this.config, 'sf:env:list', { all: this.flags.all });
const tables = results.successes
.map((r) => r.result)
.reduce((x, y) => x.concat(y), [])
.filter((t) => t.data.length > 0);
.filter((t) => t.data.length > 0)
.sort(envOrderBy);

if (tables.length === 0) {
this.log(messages.getMessage('error.NoResultsFound'));
return [];
return {};
} else {
for (const table of tables) {
final.push(...table.data);
final = { ...final, ...{ [table.type]: table.data } };
if (!this.jsonEnabled()) {
const columns = table.data.flatMap(Object.keys).reduce((x, y) => {
if (x[y]) return x;
Expand All @@ -97,8 +118,21 @@ export default class EnvList extends SfCommand<Environments> {
return { ...x, [y]: columnEntry };
}, {});

cli.table(table.data, columns, { ...tableOpts, title: table.title });
this.log();
if (
Object.entries(columns).some(([, value]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you extract this to a method so that it's easier to read?

if (this.flags?.columns) {
return this.flags?.columns.includes(value['header']);
}
return true;
})
) {
cli.table(table.data, columns, { ...tableOpts, title: table.title });
this.log();
} else {
cli.warn(
messages.getMessage('warning.RequestedColumnsNotPresentInEnvironment', [tableOpts.columns, table.title])
);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/envList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { AuthInfo, Messages, OrgAuthorization } from '@salesforce/core';
import { SfHook } from '@salesforce/sf-plugins-core';
import { SfHook, EnvList } from '@salesforce/sf-plugins-core';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-env', 'list');
Expand Down Expand Up @@ -59,12 +59,14 @@ const hook: SfHook.EnvList<SalesforceOrg> = async function (opts) {
}

const salesforceOrgs = {
type: EnvList.EnvType.salesforceOrgs,
title: 'Salesforce Orgs',
data: extractData(grouped.nonScratchOrgs),
keys: KEYS,
};

const scratchOrgs = {
type: EnvList.EnvType.scratchOrgs,
title: 'Scratch Orgs',
data: extractData(grouped.scratchOrgs),
keys: KEYS,
Expand Down
13 changes: 8 additions & 5 deletions test/commands/env/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect, test } from '@oclif/test';
import { OrgAuthorization } from '@salesforce/core';
import { SfHook } from '@salesforce/sf-plugins-core';
import { KEYS, SalesforceOrg } from '../../../src/hooks/envList';
import { Environments } from '../../../lib/commands/env/list';

const expectedSfOrgs: SalesforceOrg[] = [
{
Expand All @@ -29,8 +29,11 @@ const expectedSfOrgs: SalesforceOrg[] = [
},
];

const expectedEnvironments: Environments = {
salesforceOrgs: expectedSfOrgs,
};
const makeTableObj = (title: string, data: SalesforceOrg[]) => {
return { title, data, keys: KEYS };
return { type: 'salesforceOrgs', title, data, keys: KEYS };
};

describe('list unit tests', () => {
Expand All @@ -42,8 +45,8 @@ describe('list unit tests', () => {
.stdout()
.command(['env:list', '--json'])
.it('should list active orgs with json output', (ctx) => {
const sfOrgs = JSON.parse(ctx.stdout) as { result: OrgAuthorization[] };
expect(sfOrgs.result).to.be.deep.equal(expectedSfOrgs);
const sfOrgs = JSON.parse(ctx.stdout) as { result: Environments };
expect(sfOrgs.result).to.be.deep.equal(expectedEnvironments);
});

test
Expand Down Expand Up @@ -71,7 +74,7 @@ describe('list unit tests', () => {
failures: [],
}))
.stdout()
.command(['env:list', '--columns', 'org Id,username'])
.command(['env:list', '--columns', 'Org Id', '--columns', 'Username'])
.it('should list active orgs with human output and display selected columns', (ctx) => {
const stdout = ctx.stdout;
expect(stdout).to.be.ok;
Expand Down
4 changes: 4 additions & 0 deletions test/hooks/envList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ describe('envList hook', () => {
const result = await hook({ all: false });
expect(result).to.deep.equal([
{
type: 'salesforceOrgs',
title: 'Salesforce Orgs',
data: EXPECTED_ORGS.slice(0, 2),
keys: KEYS,
},
{
type: 'scratchOrgs',
title: 'Scratch Orgs',
data: [EXPECTED_ORGS[2]],
keys: KEYS,
Expand All @@ -128,11 +130,13 @@ describe('envList hook', () => {
const result = await hook({ all: true });
expect(result).to.deep.equal([
{
type: 'salesforceOrgs',
title: 'Salesforce Orgs',
data: EXPECTED_ORGS.slice(0, 2),
keys: KEYS,
},
{
type: 'scratchOrgs',
title: 'Scratch Orgs',
data: EXPECTED_ORGS.slice(2),
keys: KEYS,
Expand Down
65 changes: 33 additions & 32 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,10 @@
is-wsl "^2.1.1"
tslib "^2.0.0"

"@oclif/core@^0.5.17", "@oclif/core@^0.5.31", "@oclif/core@^0.5.33", "@oclif/core@^0.5.34":
version "0.5.37"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-0.5.37.tgz#acf8815bcb405851be59f2de5bafe74171f8dd8f"
integrity sha512-9tTIGfDJIGD7jt4jSCpIhwaALtztzTHGCXuKyj7PYtF0zs0/BayPa7YEFqJjaTjO3Ea53awxdIbL6kLS7bLyTg==
"@oclif/[email protected].39", "@oclif/core@^0.5.38", "@oclif/core@^0.5.39":
version "0.5.39"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-0.5.39.tgz#d00705f31c5e6617145e84bb9dd50156cf3b01c5"
integrity sha512-4XusxLX8PnHDQxtRP25PImlkIj1Mlx6wt0NWb1FxQGvTJOAgXGJZl3YB02ZeXZLYbeKA2A3AqqxFTTKbADnZng==
dependencies:
"@oclif/linewrap" "^1.0.0"
chalk "^4.1.0"
Expand All @@ -698,10 +698,10 @@
widest-line "^3.1.0"
wrap-ansi "^7.0.0"

"@oclif/core@^0.5.38":
version "0.5.38"
resolved "https://registry.npmjs.org/@oclif/core/-/core-0.5.38.tgz#293e14e426fa340d68c549171925c6c18884b995"
integrity sha512-mR4YKwGAdVhN9mN7DEPnRl/fopmVu2C4xlJYI68+6nW/uZQzZ9WjsiRgjwAMOWdAju8mG35+sVlwSg0hDIlCCA==
"@oclif/core@^0.5.31", "@oclif/core@^0.5.34":
version "0.5.37"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-0.5.37.tgz#acf8815bcb405851be59f2de5bafe74171f8dd8f"
integrity sha512-9tTIGfDJIGD7jt4jSCpIhwaALtztzTHGCXuKyj7PYtF0zs0/BayPa7YEFqJjaTjO3Ea53awxdIbL6kLS7bLyTg==
dependencies:
"@oclif/linewrap" "^1.0.0"
chalk "^4.1.0"
Expand Down Expand Up @@ -978,10 +978,10 @@
sfdx-faye "^1.0.9"
ts-retry-promise "^0.6.0"

"@salesforce/[email protected].1", "@salesforce/core@^3.6.1":
version "3.6.1"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.6.1.tgz#2f5fb779036008d08cf2fa213dece1f808d29f4e"
integrity sha512-TIrlx7k3paTi162Wlqkk6Bnu6MEJxUkuW0fKuqPPnc0uuRzXg+apGmL/G7Mk6E/FQEnWY+0TCPnUJwdn5SWoqw==
"@salesforce/[email protected].2", "@salesforce/core@^3.6.1", "@salesforce/core@^3.6.2":
version "3.6.2"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.6.2.tgz#a67f065fc0095e35ff553c7acf7b9f6ac116e4ef"
integrity sha512-X3V8Voun/u/B0O3ZeR/DLgUB5IwqTgpn80i+/XRLTVh0U2TH1/pV6948DVxkw3SRQ8NMN7JZCTq/Dq3GhGXf/g==
dependencies:
"@salesforce/bunyan" "^2.0.0"
"@salesforce/kit" "^1.5.8"
Expand Down Expand Up @@ -1122,22 +1122,22 @@
cli-ux "^5.6.3"
tslib "^2"

"@salesforce/plugin-functions@^0.2.38":
version "0.2.39"
resolved "https://registry.yarnpkg.com/@salesforce/plugin-functions/-/plugin-functions-0.2.39.tgz#74be107837482bd32c91682162d4b75e246f8891"
integrity sha512-4eZ7mf+wPiwi3EepVqL8uMfatymIP+SR5kIQaXnaHQmxW6rs1JApQa3BKU3mT7XKAFtrGUd4xNmq+KQKy3P8MQ==
"@salesforce/plugin-functions@^0.2.47":
version "0.2.47"
resolved "https://registry.yarnpkg.com/@salesforce/plugin-functions/-/plugin-functions-0.2.47.tgz#33a083228585d675bde1c7ef64772d057f87be1a"
integrity sha512-mBen61fJcSqokoChU9f4JSZ9waKxq6EdDedqdQAiE86kG7zpAtbY1cxdsUt3D/BGW4KrpVQFikqKETb23pbjxw==
dependencies:
"@heroku-cli/color" "^1.1.14"
"@heroku-cli/schema" "^1.0.25"
"@heroku/eventsource" "^1.0.7"
"@heroku/function-toml" "^0.0.3"
"@heroku/functions-core" "0.1.2"
"@heroku/project-descriptor" "0.0.5"
"@oclif/core" "^0.5.17"
"@oclif/core" "0.5.39"
"@oclif/plugin-not-found" "^1.2.4"
"@salesforce/core" "3.3.1"
"@salesforce/plugin-org" "^1.6.7"
"@salesforce/sf-plugins-core" "^0.0.15"
"@salesforce/sf-plugins-core" "^0.0.24"
"@salesforce/ts-sinon" "^1.3.18"
"@salesforce/ts-types" "^1.5.5"
axios "^0.21.1"
Expand Down Expand Up @@ -1179,24 +1179,25 @@
resolved "https://registry.yarnpkg.com/@salesforce/schemas/-/schemas-1.1.0.tgz#bbf94a11ee036f2b0ec6ba82306cd9565a6ba26b"
integrity sha512-6D7DvE6nFxpLyyTnrOIbbAeCJw2r/EpinFAcMh6gU0gA/CGfSbwV/8uR3uHLYL2zCyCZLH8jJ4dZ3BzCMqc+Eg==

"@salesforce/sf-plugins-core@^0.0.15":
version "0.0.15"
resolved "https://registry.yarnpkg.com/@salesforce/sf-plugins-core/-/sf-plugins-core-0.0.15.tgz#f7458258c14aad6c1819511ae9a9dd1d474b4f6a"
integrity sha512-k5i2s2mmx7prWZrZUp8FByU9Gdi7AwgpBz0cF+9TpQ8nLdqIyWZez3fNRr79SUTBFE1/sExHtN/wUJX5j5m1tA==
"@salesforce/sf-plugins-core@^0.0.24":
version "0.0.24"
resolved "https://registry.yarnpkg.com/@salesforce/sf-plugins-core/-/sf-plugins-core-0.0.24.tgz#3432cf8ba2393bbb9d515ceb83e6f98c3f75d6b6"
integrity sha512-1hy8eAGoepJ0gb88bnmL5x4egNmYElIEJpGjJhCA84XnRuzL827qiXlA58WxuW1YtYTeIDu6/8W79HWTdfVeDA==
dependencies:
"@oclif/core" "^0.5.33"
"@salesforce/kit" "^1.5.8"
"@salesforce/ts-types" "^1.5.13"
cli-ux "^5.6.2"
"@oclif/core" "^0.5.38"
"@salesforce/core" "^3.6.1"
"@salesforce/kit" "^1.5.17"
"@salesforce/ts-types" "^1.5.20"
cli-ux "^5.6.3"
inquirer "^8.1.1"

"@salesforce/sf-plugins-core@^0.0.23":
version "0.0.23"
resolved "https://registry.npmjs.org/@salesforce/sf-plugins-core/-/sf-plugins-core-0.0.23.tgz#3019efe54cedbd6d20e01a135660089c0dcbe6bc"
integrity sha512-4yGxxUrSh5y6m+kakWMerTmeFCDzdmWAOZgJ6t0wbGNM66GK4ylG8BlS/s11oEuUWKtKUBOdANH6TwJp44ppew==
"@salesforce/sf-plugins-core@^0.0.25":
version "0.0.25"
resolved "https://registry.yarnpkg.com/@salesforce/sf-plugins-core/-/sf-plugins-core-0.0.25.tgz#bb8012cae2911c8965d959595006a570ab5c6405"
integrity sha512-2mxQvEldZYG5BSvFNReeu/MIZ0h2+6KYfGC8i1lWan8YYsUbrzdWS6JK3pVw/IOMTuBXuxs05vOVVmzsR6vyCA==
dependencies:
"@oclif/core" "^0.5.38"
"@salesforce/core" "^3.6.1"
"@oclif/core" "^0.5.39"
"@salesforce/core" "^3.6.2"
"@salesforce/kit" "^1.5.17"
"@salesforce/ts-types" "^1.5.20"
cli-ux "^5.6.3"
Expand Down