Skip to content

Commit

Permalink
feat: add help section helper function
Browse files Browse the repository at this point in the history
@W-9791511@
  • Loading branch information
peternhale committed Sep 10, 2021
1 parent beb6c48 commit 379d14f
Show file tree
Hide file tree
Showing 4 changed files with 965 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"@oclif/core": "^0.5.35",
"@salesforce/core": "^3.6.0",
"@salesforce/kit": "^1.5.8",
"@salesforce/ts-types": "^1.5.13",
"cli-ux": "^5.6.2",
Expand Down
41 changes: 41 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import { Separator, ChoiceOptions, ChoiceBase } from 'inquirer';
import { Dictionary, Nullable, ensureString } from '@salesforce/ts-types';
import { HelpSection } from '@oclif/core';
import { ORG_CONFIG_ALLOWED_PROPERTIES, OrgConfigProperties } from '@salesforce/core';
import { SFDX_ALLOWED_PROPERTIES, SfdxPropertyKeys } from '@salesforce/core';
import { EnvironmentVariable, SUPPORTED_ENV_VARS } from '@salesforce/core';

/**
* Generate a formatted table for list and checkbox prompts
Expand Down Expand Up @@ -54,3 +58,40 @@ export function generateTableChoices<T>(

return choicesOptions;
}

/**
* Function to build a help section for command help.
* Takes a string to be used as section header text and an array of enums
* that identify the variable or property to be included in the help
* body.
*
* @param header
* @param vars
*/
export function toHelpSection(
header: string,
...vars: Array<OrgConfigProperties | SfdxPropertyKeys | EnvironmentVariable>
): HelpSection {
const body = vars
.map((v) => {
const orgConfig = ORG_CONFIG_ALLOWED_PROPERTIES.find(({ key }) => {
return key === v;
});
if (orgConfig) {
return { name: orgConfig.key, description: orgConfig.description };
}
const sfdxProperty = SFDX_ALLOWED_PROPERTIES.find(({ key }) => key === v);
if (sfdxProperty) {
return { name: sfdxProperty.key.valueOf(), description: sfdxProperty.description };
}
const envVar = Object.entries(SUPPORTED_ENV_VARS).find(([k]) => k === v);

if (envVar) {
const [eKey, data] = envVar;
return { name: eKey, description: data.description };
}
return undefined;
})
.filter((b) => b);
return { header, body } as HelpSection;
}
63 changes: 62 additions & 1 deletion test/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import { expect } from 'chai';
import { Separator } from 'inquirer';
import stripAnsi = require('strip-ansi');
import { generateTableChoices } from '../../src/util';
import { EnvironmentVariable, SUPPORTED_ENV_VARS } from '@salesforce/core';
import { ORG_CONFIG_ALLOWED_PROPERTIES, OrgConfigProperties } from '@salesforce/core';
import { SfdxPropertyKeys, SFDX_ALLOWED_PROPERTIES } from '@salesforce/core';
import { generateTableChoices, toHelpSection } from '../../src/util';

describe('generateTableChoices', () => {
const columns = {
Expand Down Expand Up @@ -48,3 +51,61 @@ describe('generateTableChoices', () => {
expect(tableChoices[2]).to.have.property('name').and.equal('my-app a-long-org my-app ');
});
});

describe('toHelpSection', () => {
it('should produce help section for env vars', () => {
const envVarSection = toHelpSection('ENV VAR SECTION', EnvironmentVariable.SFDX_ACCESS_TOKEN);
expect(envVarSection).to.have.property('header', 'ENV VAR SECTION');
expect(envVarSection).to.have.property('body').to.have.property('length', 1);
expect(envVarSection.body[0]).to.deep.equal({
name: 'SFDX_ACCESS_TOKEN',
description: SUPPORTED_ENV_VARS[EnvironmentVariable.SFDX_ACCESS_TOKEN].description,
});
});
it('should produce help section for org config vars', () => {
const orgConfigSection = toHelpSection('ORG CONFIG VAR SECTION', OrgConfigProperties.TARGET_ORG);
expect(orgConfigSection).to.have.property('header', 'ORG CONFIG VAR SECTION');
expect(orgConfigSection).to.have.property('body').to.have.property('length', 1);
const orgConfig = ORG_CONFIG_ALLOWED_PROPERTIES.find(({ key }) => key === OrgConfigProperties.TARGET_ORG);
expect(orgConfigSection.body[0]).to.deep.equal({
name: 'target-org',
description: orgConfig.description,
});
});
it('should produce help section for sfdx config vars', () => {
const sfdxConfigSection = toHelpSection('SFDX CONFIG VAR SECTION', SfdxPropertyKeys.INSTANCE_URL);
expect(sfdxConfigSection).to.have.property('header', 'SFDX CONFIG VAR SECTION');
expect(sfdxConfigSection).to.have.property('body').to.have.property('length', 1);
const sfdxConfig = SFDX_ALLOWED_PROPERTIES.find(({ key }) => key === SfdxPropertyKeys.INSTANCE_URL);
expect(sfdxConfigSection.body[0]).to.deep.equal({
name: 'instanceUrl',
description: sfdxConfig.description,
});
});
it('should produce help section for mixed config vars', () => {
const mixedSection = toHelpSection(
'MIXED VAR SECTION',
EnvironmentVariable.SFDX_ACCESS_TOKEN,
OrgConfigProperties.TARGET_ORG,
SfdxPropertyKeys.INSTANCE_URL
);
expect(mixedSection).to.have.property('header', 'MIXED VAR SECTION');
expect(mixedSection).to.have.property('body').to.have.property('length', 3);
const sfdxConfig = SFDX_ALLOWED_PROPERTIES.find(({ key }) => key === SfdxPropertyKeys.INSTANCE_URL);
const orgConfig = ORG_CONFIG_ALLOWED_PROPERTIES.find(({ key }) => key === OrgConfigProperties.TARGET_ORG);
expect(mixedSection.body).to.deep.equal([
{
name: 'SFDX_ACCESS_TOKEN',
description: SUPPORTED_ENV_VARS[EnvironmentVariable.SFDX_ACCESS_TOKEN].description,
},
{
name: 'target-org',
description: orgConfig.description,
},
{
name: 'instanceUrl',
description: sfdxConfig.description,
},
]);
});
});
Loading

0 comments on commit 379d14f

Please sign in to comment.