diff --git a/bin/now-teams.js b/bin/now-teams.js index 1a29e5e..ec45eb3 100644 --- a/bin/now-teams.js +++ b/bin/now-teams.js @@ -111,6 +111,14 @@ async function run({ token, config: { currentTeam } }) { const args = argv._.slice(1) switch (subcommand) { + case 'list': + case 'ls': { + await require(resolve(__dirname, 'teams', 'list.js'))({ + teams, + token + }) + break + } case 'switch': case 'change': { await require(resolve(__dirname, 'teams', 'switch.js'))({ diff --git a/bin/teams/list.js b/bin/teams/list.js new file mode 100644 index 0000000..0505906 --- /dev/null +++ b/bin/teams/list.js @@ -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] + ) +} diff --git a/lib/utils/output/table.js b/lib/utils/output/table.js new file mode 100644 index 0000000..e70c27f --- /dev/null +++ b/lib/utils/output/table.js @@ -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))) +}