-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use cli-table to return the reviewers
- Loading branch information
Víctor Guzmán
committed
Nov 30, 2022
1 parent
1cf1bb4
commit 2b02b78
Showing
2 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Table, { HorizontalTable, Cell } from 'cli-table3' | ||
import chalk from 'chalk' | ||
import { UserEntity } from '../api/api' | ||
|
||
export async function render(reviewers: UserEntity[]) { | ||
const { reviewerHeaders, columnsNumber } = generateReviewerHeaders() | ||
const infoHeaders = generateInfoHeaders(columnsNumber) | ||
const content = await generateContent(reviewers, columnsNumber) | ||
const table = new Table() as HorizontalTable | ||
table.push( | ||
...infoHeaders, | ||
reviewerHeaders, | ||
...content | ||
) | ||
return table | ||
} | ||
|
||
function generateReviewerHeaders() { | ||
const headers = [ | ||
{ content: chalk.bold.greenBright('Display Name'), hAlign: 'left' }, | ||
{ content: chalk.bold.greenBright('AccountId'), hAlign: 'right' } | ||
] | ||
return { | ||
reviewerHeaders: headers.map((r) => r as Cell), | ||
columnsNumber: headers.length | ||
} | ||
} | ||
|
||
function generateInfoHeaders(colSpan: number) { | ||
return [ | ||
[{ colSpan: colSpan, hAlign: 'center', content: chalk.bold('Reviewers for the current user:') }] | ||
].map((r) => r as Cell[]) | ||
} | ||
|
||
async function generateContent(reviewers: UserEntity[], colSpan: number) { | ||
let content = await generateUserContent(reviewers) | ||
if (content.length === 0) { | ||
content = [ | ||
[{ colSpan: colSpan, content: chalk.redBright('No reviewers'), hAlign: 'center' }] | ||
] | ||
} | ||
return content.map((r) => r as Cell[]) | ||
} | ||
|
||
async function generateUserContent(reviewers: UserEntity[]) { | ||
return Promise.all( | ||
reviewers.map(async (reviewer) => { | ||
const tableContent = { | ||
displayName: { colSpan: 1, content: chalk.yellow(reviewer.displayName), hAlign: 'left' }, | ||
accountId: { colSpan: 1, content: reviewer.accountId, hAlign: 'left' } | ||
} | ||
return Object.values(tableContent) | ||
}) | ||
) | ||
} |