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

Slac/table #736

Merged
merged 8 commits into from
Oct 31, 2024
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@oclif/core": "^4",
"@salesforce/core": "^8.6.1",
"@salesforce/kit": "^3.2.3",
"@salesforce/sf-plugins-core": "^11.3.12",
"@salesforce/sf-plugins-core": "^12.0.7",
"change-case": "^5.4.4"
},
"devDependencies": {
Expand Down
17 changes: 9 additions & 8 deletions src/commands/org/delete/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const messages = Messages.loadMessages('@salesforce/plugin-signups', 'shape.dele

export type OrgShapeDeleteResult = {
orgId: string;
} & DeleteAllResult
} & DeleteAllResult;

export class OrgShapeDeleteCommand extends SfCommand<OrgShapeDeleteResult | undefined> {
public static readonly summary = messages.getMessage('summary');
Expand Down Expand Up @@ -69,15 +69,16 @@ export class OrgShapeDeleteCommand extends SfCommand<OrgShapeDeleteResult | unde
if (deleteRes.failures.length > 0) {
setExitCode(68);

this.styledHeader('Partial Success');
this.logSuccess(messages.getMessage('humanSuccess', [orgId]));
this.log('');
this.styledHeader('Failures');
const columns = {
shapeId: { header: 'Shape ID' },
message: { header: 'Error Message' },
};
this.table(deleteRes.failures, columns);
this.table({
data: deleteRes.failures,
columns: [
{ key: 'shapeId', name: 'Shape ID' },
{ key: 'message', name: 'Error Message' },
],
title: 'Failures',
});
} else if (deleteRes.failures.length === deleteRes.shapeIds.length) {
setExitCode(1);
} else {
Expand Down
27 changes: 7 additions & 20 deletions src/commands/org/list/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ import utils, { OrgShapeListResult } from '../../../shared/orgShapeListUtils.js'
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-signups', 'shape.list');

// default columns for the shape list
const orgShapeColumns = {
alias: {
header: 'ALIAS',
get: (data: OrgShapeListResult): string => data.alias ?? '',
},
username: { header: 'USERNAME' },
orgId: { header: 'ORG ID' },
status: { header: 'SHAPE STATUS' },
createdBy: { header: 'CREATED BY' },
createdDate: { header: 'CREATED DATE' },
};

export class OrgShapeListCommand extends SfCommand<OrgShapeListResult[]> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
Expand All @@ -51,13 +38,13 @@ export class OrgShapeListCommand extends SfCommand<OrgShapeListResult[]> {
return orgShapes;
}

this.styledHeader('Org Shapes');
this.table(
orgShapes.map((shape) =>
shape.status === 'Active' ? { ...shape, status: StandardColors.success(shape.status) } : shape
),
orgShapeColumns
);
this.table({
data: orgShapes.map((shape) => ({
...(shape.status === 'Active' ? { ...shape, status: StandardColors.success(shape.status) } : shape),
})),
title: 'Org Shapes',
overflow: 'wrap',
});
return orgShapes;
}
}
51 changes: 17 additions & 34 deletions src/shared/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ const dateTimeFormatter = (dateString?: string): string =>
})
: '';

const rowDateTimeFormatter = (row: OrgSnapshot, field: keyof OrgSnapshot): string => dateTimeFormatter(row[field]);

const ORG_SNAPSHOT_COLUMNS = {
Id: {},
SnapshotName: { header: 'Snapshot Name' },
Status: {},
SourceOrg: { header: 'Source Org Id' },
CreatedDate: {
header: 'Created Date',
get: (row: OrgSnapshot): string => rowDateTimeFormatter(row, 'CreatedDate'),
},
LastModifiedDate: {
header: 'Last Modified Date',
get: (row: OrgSnapshot): string => rowDateTimeFormatter(row, 'LastModifiedDate'),
},
ExpirationDate: {
header: 'Expiration Date',
get: (row: OrgSnapshot): string => (row.ExpirationDate ? new Date(row.ExpirationDate).toLocaleDateString() : ''),
},
};

export const invalidTypeErrorHandler = (e: unknown): never => {
if (e instanceof Error && e.name === 'INVALID_TYPE') {
e.message = messages.getMessage('snapshotNotEnabled');
Expand Down Expand Up @@ -104,8 +83,8 @@ export const queryByNameOrId = async (conn: Connection, nameOrId: string): Promi
};

export const printSingleRecordTable = (snapshotRecord: OrgSnapshot): void => {
new Ux().table(
Object.entries(snapshotRecord)
new Ux().table({
data: Object.entries(snapshotRecord)
.filter(([key]) => key !== 'attributes')
// remove empty error field
.filter(([key, value]) => key !== 'Error' || typeof value === 'string')
Expand All @@ -117,22 +96,26 @@ export const printSingleRecordTable = (snapshotRecord: OrgSnapshot): void => {
}))
// null/undefined becomes empty string
.map((row) => (row.Value ? row : { ...row, Value: '' })),
{ Name: {}, Value: {} }
);
columns: ['Name', 'Value'],
});
};

export const printRecordTable = (snapshotRecords: OrgSnapshot[]): void => {
if (snapshotRecords.length === 0) {
new Ux().log('No snapshots found');
return;
}

new Ux().table(
// we know what columns we want, so filter out the other fields
snapshotRecords.map((s) =>
Object.fromEntries(Object.entries(s).filter(([key]) => Object.keys(ORG_SNAPSHOT_COLUMNS).includes(key)))
),
ORG_SNAPSHOT_COLUMNS,
{ title: `Org Snapshots [${snapshotRecords.length}]`, 'no-truncate': true }
);
new Ux().table({
data: snapshotRecords.map((s) => ({
Id: s.Id,
'Snapshot Name': s.SnapshotName,
Status: s.Status,
'Source Org Id': s.SourceOrg,
'Created Date': dateTimeFormatter(s.CreatedDate),
'Last Modified Date': dateTimeFormatter(s.LastModifiedDate),
'Expiration Date': s.ExpirationDate ? new Date(s.ExpirationDate).toLocaleDateString() : '',
})),
title: `Org Snapshots [${snapshotRecords.length}]`,
overflow: 'wrap',
});
};
15 changes: 7 additions & 8 deletions test/shape/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ describe('org:shape:delete', () => {
// stubs
let uxLogStub: sinon.SinonStub;
let uxTableStub: sinon.SinonStub;
let uxStyledHeaderStub: sinon.SinonStub;

beforeEach(async () => {
await config.load();
uxLogStub = sandbox.stub(SfCommand.prototype, 'log');
uxStyledHeaderStub = sandbox.stub(SfCommand.prototype, 'styledHeader');
uxTableStub = sandbox.stub(SfCommand.prototype, 'table');

await $$.stubAuths(testOrg);
Expand Down Expand Up @@ -122,14 +120,15 @@ describe('org:shape:delete', () => {
const command = new OrgShapeDeleteCommand(['--noprompt', '--targetusername', testOrg.username], config);
await command.run();

expect(uxStyledHeaderStub.firstCall.args[0]).to.equal('Partial Success');
expect(uxLogStub.getCalls().some((c) => c.args[0] === 'Successfully deleted org shape for 00D000000000000004.'));
expect(uxLogStub.getCalls().some((c) => c.args[0] === ''));
expect(uxStyledHeaderStub.secondCall.args[0]).to.equal('Failures');
expect(uxTableStub.firstCall.args[0]).to.deep.equal([{ shapeId: '3SR000000000124', message: 'MALFORMED ID' }]);
expect(uxTableStub.firstCall.args[1]).to.deep.equal({
shapeId: { header: 'Shape ID' },
message: { header: 'Error Message' },
expect(uxTableStub.firstCall.args[0]).to.deep.equal({
data: [{ shapeId: '3SR000000000124', message: 'MALFORMED ID' }],
columns: [
{ key: 'shapeId', name: 'Shape ID' },
{ key: 'message', name: 'Error Message' },
],
title: 'Failures',
});
});

Expand Down
17 changes: 8 additions & 9 deletions test/shape/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('org:shape:list', () => {
// stubs
let uxLogStub: sinon.SinonStub;
let uxTableStub: sinon.SinonStub;
let uxStyledHeaderStub: sinon.SinonStub;

const $$ = new TestContext();
const testOrg = new MockTestOrgData();
Expand All @@ -33,7 +32,6 @@ describe('org:shape:list', () => {
beforeEach(async () => {
await config.load();
uxLogStub = sandbox.stub(SfCommand.prototype, 'log');
uxStyledHeaderStub = sandbox.stub(SfCommand.prototype, 'styledHeader');
uxTableStub = sandbox.stub(SfCommand.prototype, 'table');
});

Expand Down Expand Up @@ -78,13 +76,14 @@ describe('org:shape:list', () => {
const command = new OrgShapeListCommand([], config);
await command.run();
expect(uxLogStub.notCalled).to.be.true;
expect(uxStyledHeaderStub.firstCall.args[0]).to.equal('Org Shapes');
expect((uxTableStub.firstCall.args[0] as OrgShapeListResult[]).length).to.equal(2);
expect(uxTableStub.firstCall.args[0]).to.deep.equal(
shapes.map((shape) =>
shape.status === 'Active' ? { ...shape, status: StandardColors.success(shape.status) } : shape
)
);
expect((uxTableStub.firstCall.args[0] as { data: OrgShapeListResult[] }).data.length).to.equal(2);
expect(uxTableStub.firstCall.args[0]).to.deep.equal({
data: shapes.map((shape) => ({
...(shape.status === 'Active' ? { ...shape, status: StandardColors.success(shape.status) } : shape),
})),
title: 'Org Shapes',
overflow: 'wrap',
});
});

it('no devhub org', async () => {
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"extends": "@salesforce/dev-config/tsconfig-strict-esm",
"compilerOptions": {
"outDir": "lib",
"baseUrl": ".",
"skipLibCheck": true,
"strictNullChecks": true,
"rootDir": "src",
"lib": ["es2020"]
Expand Down
Loading