Browse Source
* Add `teams ls` command * Fix Prettier * Add `teams list` command alias * Add table output primitive * Use table output primitive to list team * Fix name/email for user * Change indicator for current teammaster
CHaBou
8 years ago
committed by
Leo Lamprecht
3 changed files with 97 additions and 0 deletions
@ -0,0 +1,62 @@ |
|||
const chalk = require('chalk') |
|||
|
|||
const wait = require('../../lib/utils/output/wait') |
|||
const cfg = require('../../lib/cfg') |
|||
const info = require('../../lib/utils/output/info') |
|||
const error = require('../../lib/utils/output/error') |
|||
const { tick: tickChar } = require('../../lib/utils/output/chars') |
|||
const table = require('../../lib/utils/output/table') |
|||
|
|||
module.exports = async function({ teams, token }) { |
|||
const stopSpinner = wait('Fetching teams') |
|||
const list = (await teams.ls()).teams |
|||
let { user, currentTeam } = await cfg.read({ token }) |
|||
const accountIsCurrent = !currentTeam |
|||
stopSpinner() |
|||
|
|||
if (accountIsCurrent) { |
|||
currentTeam = { |
|||
slug: user.username || user.email |
|||
} |
|||
} |
|||
|
|||
const teamList = list.map(({ slug, name }) => { |
|||
return { |
|||
name, |
|||
value: slug, |
|||
current: slug === currentTeam.slug ? tickChar : '' |
|||
} |
|||
}) |
|||
|
|||
teamList.unshift({ |
|||
name: user.email, |
|||
value: user.username || user.email, |
|||
current: (accountIsCurrent && tickChar) || '' |
|||
}) |
|||
|
|||
// Let's bring the current team to the beginning of the list
|
|||
if (!accountIsCurrent) { |
|||
const index = teamList.findIndex( |
|||
choice => choice.value === currentTeam.slug |
|||
) |
|||
const choice = teamList.splice(index, 1)[0] |
|||
teamList.unshift(choice) |
|||
} |
|||
|
|||
// Printing
|
|||
const count = teamList.length |
|||
if (!count) { |
|||
// Maybe should not happen
|
|||
error(`No team found`) |
|||
return |
|||
} |
|||
|
|||
info(`${chalk.bold(count)} team${count > 1 ? 's' : ''} found`) |
|||
console.log() |
|||
|
|||
table( |
|||
['', 'id', 'email / name'], |
|||
teamList.map(team => [team.current, team.value, team.name]), |
|||
[1, 5] |
|||
) |
|||
} |
@ -0,0 +1,27 @@ |
|||
const chalk = require('chalk') |
|||
const printf = require('printf') |
|||
|
|||
const printLine = (data, sizes) => |
|||
data.reduce((line, col, i) => { |
|||
return line + printf(`%-${sizes[i]}s`, col) |
|||
}, '') |
|||
|
|||
// Print a table
|
|||
module.exports = (fieldNames = [], data = [], margins = []) => { |
|||
// Compute size of each column
|
|||
const sizes = data |
|||
.reduce((acc, row) => { |
|||
return row.map((col, i) => { |
|||
const currentMaxColSize = acc[i] || 0 |
|||
const colSize = (col && col.length) || 0 |
|||
return Math.max(currentMaxColSize, colSize) |
|||
}) |
|||
}, fieldNames.map(col => col.length)) |
|||
// Add margin to all columns except the last
|
|||
.map((size, i) => (i < margins.length && size + margins[i]) || size) |
|||
|
|||
// Print header
|
|||
console.log(chalk.grey(printLine(fieldNames, sizes))) |
|||
// Print content
|
|||
data.forEach(row => console.log(printLine(row, sizes))) |
|||
} |
Loading…
Reference in new issue