Skip to content

Commit

Permalink
Merge pull request #759 from salesforcecli/mdonnalley/new-table
Browse files Browse the repository at this point in the history
feat: use new table
  • Loading branch information
soridalac authored Oct 23, 2024
2 parents 0a21bb6 + bc3006c commit 4cc107b
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 190 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"@oclif/core": "^4",
"@salesforce/core": "^8.6.2",
"@salesforce/sf-plugins-core": "^11.3.10",
"@salesforce/sf-plugins-core": "^12.0.4",
"fast-levenshtein": "^3.0.0"
},
"devDependencies": {
Expand Down Expand Up @@ -86,6 +86,7 @@
"reformat": "prettier --config .prettierrc --write './*.{js,json,md}' './**/*.{ts,json,md}'",
"test": "wireit",
"test:nuts": "nyc mocha \"test/**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel --jobs 20",
"test:nuts:alias": "nyc mocha \"test/**/alias/**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel --jobs 20",
"test:nuts:config": "nyc mocha \"test/**/config/**/*.nut.ts\" --slow 4500 --timeout 600000 --parallel --jobs 20",
"test:only": "wireit",
"version": "oclif readme"
Expand Down
34 changes: 11 additions & 23 deletions src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,28 @@ export type AliasResult = {

export type AliasResults = AliasResult[];

type AliasColumns = {
alias: { header: string };
value: { header: string };
success?: { header: string };
message?: { header: string };
};

export abstract class AliasCommand<T> extends SfCommand<T> {
protected output(title: string, results: AliasResults): void {
if (results.length === 0) {
this.log('No results');
return;
}

const columns: AliasColumns = {
alias: { header: 'Alias' },
value: { header: 'Value' },
};

if (title.includes('Set') || title.includes('Unset')) {
columns.success = { header: 'Success' };
}

// If any result contains an Error, add the header and grab the message off of Error
if (results.some((result) => result.error)) {
process.exitCode = 1;

columns.message = { header: 'Message' };

// results.map((r) => ({ ...r, message: r.error?.message }));
results.map((result) => (result.message = result.error?.message));
}

this.table(results, columns, { title, 'no-truncate': true });
const data = results.map((result) => ({
alias: result.alias,
value: result.value,
...(title.includes('Set') || title.includes('Unset') ? { success: result.success } : {}),
...(result.error ? { message: result.error.message } : {}),
}));

this.table({
data,
title,
});
}
}

Expand Down
48 changes: 16 additions & 32 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,22 @@ export const output = (ux: Ux, responses: Msg[], command: 'set' | 'unset' | 'lis
return;
}

ux.table(
responses,
{
name: { header: 'Name' },
...(verbose
? {
location: {
header: 'Location',
get: (row) => row.location ?? '',
},
}
: {}),
...(command === 'unset'
? {}
: {
value: {
header: 'Value',
get: (row) => row.value,
},
}),
...(command === 'list' ? {} : { success: { header: 'Success' } }),
...(responses.some((msg) => msg.error)
? {
message: {
header: 'Message',
get: (row): string => row.error?.message ?? '',
},
}
: {}),
},
{ title: commandToTitleMapping[command] }
);
const data = responses.map((response) => ({
name: response.name,
...(verbose ? { location: response.location ?? '' } : {}),
...(command === 'unset' ? {} : { value: response.value }),
...(command === 'list' ? {} : { success: response.success }),
...(responses.some((msg) => msg.error)
? {
message: response.error?.message ?? '',
}
: {}),
}));

ux.table({
data,
title: commandToTitleMapping[command],
});
};

const commandToTitleMapping = {
Expand Down
29 changes: 18 additions & 11 deletions test/commands/alias/list.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('alias list NUTs', () => {
});

it('lists no aliases stdout', () => {
const res: string = execCmd('alias list').shellOutput;
const res = execCmd('alias list').shellOutput;
expect(res).to.include('No results');
});
});
Expand All @@ -60,10 +60,13 @@ describe('alias list NUTs', () => {
});

it('lists singular result correctly stdout', () => {
const res: string = execCmd('alias list', { ensureExitCode: 0 }).shellOutput;
expect(res).to.include('Alias List\n====='); // Table header
expect(res).to.include('Alias Value');
expect(res).to.include('DevHub [email protected]');
const res = execCmd('alias list', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;
expect(res).to.include('Alias List'); // Table header
expect(res).to.include('alias value');
expect(res).to.include('DevHub [email protected]');
});
});

Expand Down Expand Up @@ -94,12 +97,16 @@ describe('alias list NUTs', () => {
});

it('lists multiple results correctly stdout', () => {
const res: string = execCmd('alias list', { ensureExitCode: 0 }).shellOutput;
expect(res).to.include('Alias List\n====='); // Table header
expect(res).to.include('Alias Value');
expect(res).to.include('DevHub [email protected]');
expect(res).to.include('Admin [email protected]');
expect(res).to.include('user [email protected]');
const res = execCmd('alias list', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;

expect(res).to.include('Alias List'); // Table header
expect(res).to.include('alias value');
expect(res).to.include('DevHub [email protected]');
expect(res).to.include('Admin [email protected]');
expect(res).to.include('user [email protected]');
});
});
});
54 changes: 13 additions & 41 deletions test/commands/alias/set.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ describe('alias set NUTs', () => {
});

it('alias set multiple values stdout', () => {
const res: string = execCmd('alias set [email protected] [email protected]', {
const res = execCmd('alias set [email protected] [email protected]', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;

expect(res).to.include('Alias Set\n====='); // Table header
expect(res).to.include('Alias Value');
expect(res).to.include('DevHub [email protected]');
expect(res).to.include('Admin [email protected]');
expect(res).to.include('Alias Set'); // Table header
expect(res).to.include('alias value success');
expect(res).to.include('DevHub [email protected] true');
expect(res).to.include('Admin [email protected] true');
});

it('alias set with spaces in value', () => {
Expand Down Expand Up @@ -101,36 +102,6 @@ describe('alias set NUTs', () => {

expect(res).to.include('Found duplicate argument');
});

// this behavior is now allowed to allow plugin-settings in sfdx
it.skip('alias set DevHub= shows error message to use alias unset command', () => {
const res = execCmd('alias set DevHub=', {
ensureExitCode: 1,
}).shellOutput;

expect(res).to.include('Alias Set\n====='); // Table header
expect(res).to.include('Alias Value Success Message');
expect(res).to.include(`DevHub false ${messages.getMessages('error.ValueRequired')}`);
});

it.skip('alias set DevHub= shows error to use alias unset command (json)', () => {
const result = execCmd('alias set DevHub= --json', {
ensureExitCode: 1,
}).jsonOutput?.result;

expect(result).to.deep.equal([
{
alias: 'DevHub',
success: false,
error: {
name: 'ValueRequiredError',
exitCode: 1,
},
message:
'You must provide a value when setting an alias. Use `sf alias unset my-alias-name` to remove existing aliases.',
},
]);
});
});

describe('alias set overwrites existing entry', () => {
Expand All @@ -151,14 +122,15 @@ describe('alias set NUTs', () => {
});

it('alias set overwrites entry correctly stdout', () => {
const res: string = execCmd('alias set [email protected] [email protected]', {
const res = execCmd('alias set [email protected] [email protected]', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;

expect(res).to.include('Alias Set\n====='); // Table header
expect(res).to.include('Alias Value');
expect(res).to.include('DevHub [email protected]');
expect(res).to.include('Admin [email protected]');
expect(res).to.include('Alias Set'); // Table header
expect(res).to.include('alias value success');
expect(res).to.include('DevHub [email protected] true');
expect(res).to.include('Admin [email protected] true');
});
});

Expand All @@ -174,7 +146,7 @@ describe('alias set NUTs', () => {
});

it('alias set without varargs stdout', () => {
const res: string = execCmd('alias set', {
const res = execCmd('alias set', {
ensureExitCode: 1,
}).shellOutput.stderr;

Expand Down
29 changes: 16 additions & 13 deletions test/commands/alias/unset.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ describe('alias unset NUTs', () => {
});

it('unsetting non-existent key is a success (stdout)', () => {
const res: string = execCmd('alias unset noAlias', {
const res = execCmd('alias unset noAlias', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;

expect(res).to.include('Alias Unset\n====='); // Table header
expect(res).to.include('Alias Value Success');
expect(res).to.include('noAlias true');
expect(res).to.include('Alias Unset'); // Table header
expect(res).to.include('alias value success');
expect(res).to.include('noAlias true');
});
});

Expand Down Expand Up @@ -82,13 +83,14 @@ describe('alias unset NUTs', () => {
});

it('alias unset DevHub', () => {
const res: string = execCmd('alias unset DevHub', {
const res = execCmd('alias unset DevHub', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;

expect(res).to.include('Alias Unset\n====='); // Table header
expect(res).to.include('Alias Value Success');
expect(res).to.include('DevHub [email protected] true');
expect(res).to.include('Alias Unset'); // Table header
expect(res).to.include('alias value success');
expect(res).to.include('DevHub [email protected] true');
});
});

Expand Down Expand Up @@ -143,14 +145,15 @@ describe('alias unset NUTs', () => {
});

it('alias unset DevHub user', () => {
const res: string = execCmd('alias unset DevHub user', {
const res = execCmd('alias unset DevHub user', {
ensureExitCode: 0,
env: { ...process.env, SF_NO_TABLE_STYLE: 'true' },
}).shellOutput;

expect(res).to.include('Alias Unset\n====='); // Table header
expect(res).to.include('Alias Value Success');
expect(res).to.include('DevHub [email protected] true');
expect(res).to.include('user [email protected] true');
expect(res).to.include('Alias Unset'); // Table header
expect(res).to.include('alias value success');
expect(res).to.include('DevHub [email protected] true');
expect(res).to.include('user [email protected] true');
});

it('removes all aliases when passing --all', () => {
Expand Down
Loading

0 comments on commit 4cc107b

Please sign in to comment.