From a1e5ba0b26b2c4f59bbca9e59656f302bceba9e8 Mon Sep 17 00:00:00 2001 From: Jarmo Isotalo Date: Fri, 5 May 2017 12:49:48 -0700 Subject: [PATCH] Remove id's from now-alias (#500) * Remove id's from now-alias * Colors * Less hacky formatting --- bin/now-alias.js | 120 ++++++++++++++++++++++++++++++----------------- 1 file changed, 76 insertions(+), 44 deletions(-) diff --git a/bin/now-alias.js b/bin/now-alias.js index 1a52786..d9776d5 100755 --- a/bin/now-alias.js +++ b/bin/now-alias.js @@ -5,6 +5,9 @@ const chalk = require('chalk') const minimist = require('minimist') const table = require('text-table') const ms = require('ms') +const printf = require('printf') +require('epipebomb')() +const supportsColor = require('supports-color') // Ours const strlen = require('../lib/strlen') @@ -33,6 +36,9 @@ const argv = minimist(process.argv.slice(2), { const subcommand = argv._[0] +const grayWidth = 10 +const underlineWidth = 11 + // Options const help = () => { console.log(` @@ -185,54 +191,81 @@ async function run({ token, config: { currentTeam, user } }) { const aliases = await alias.ls() aliases.sort((a, b) => new Date(b.created) - new Date(a.created)) const current = new Date() - - const header = [ - ['', 'id', 'source', 'url', 'created'].map(s => chalk.dim(s)) - ] - const text = list.length === 0 - ? null - : table( - header.concat( - aliases.map(_alias => { - const _url = chalk.underline(`https://${_alias.alias}`) - const target = _alias.deploymentId - let _sourceUrl - if (urls.get(target)) { - _sourceUrl = chalk.underline(`https://${urls.get(target)}`) - } else if (_alias.rules) { - _sourceUrl = chalk.gray(`[${_alias.rules.length} custom rule${_alias.rules.length > 1 ? 's' : ''}]`) - } else { - _sourceUrl = chalk.gray('') - } - - const time = chalk.gray( - ms(current - new Date(_alias.created)) + ' ago' - ) - return [ - '', - // We default to `''` because some early aliases didn't - // have an uid associated - _alias.uid === null ? '' : _alias.uid, - _sourceUrl, - _url, - time - ] - }) - ), - { - align: ['l', 'r', 'l', 'l'], - hsep: ' '.repeat(2), - stringLength: strlen - } + const sourceUrlLength = + aliases.reduce((acc, i) => { + return Math.max( + acc, + (i.deploymentId && urls.get(i.deploymentId).length) || 0 ) - + }, 0) + 9 + const aliasLength = + aliases.reduce((acc, i) => { + return Math.max(acc, (i.alias && i.alias.length) || 0) + }, 0) + 8 const elapsed_ = ms(new Date() - start_) console.log(`> ${aliases.length} alias${aliases.length === 1 ? '' : 'es'} found ${chalk.gray(`[${elapsed_}]`)} under ${chalk.bold((currentTeam && currentTeam.slug) || user.username || user.email)}`) - - if (text) { - console.log('\n' + text + '\n') + console.log() + + if (supportsColor) { + const urlSpecHeader = `%-${sourceUrlLength + 1}s` + const aliasSpecHeader = `%-${aliasLength + 1}s` + console.log( + printf( + ` ${chalk.gray(urlSpecHeader + ' ' + aliasSpecHeader + ' %5s')}`, + 'source', + 'url', + 'age' + ) + ) + } else { + const urlSpecHeader = `%-${sourceUrlLength}s` + const aliasSpecHeader = `%-${aliasLength}s` + console.log( + printf( + ` ${urlSpecHeader} ${aliasSpecHeader} %5s`, + 'source', + 'url', + 'age' + ) + ) } + let text = '' + aliases.forEach(_alias => { + let urlSpec = sourceUrlLength + let aliasSpec = aliasLength + let ageSpec = 5 + const _url = chalk.underline(`https://${_alias.alias}`) + const target = _alias.deploymentId + let _sourceUrl + if (supportsColor) { + aliasSpec += underlineWidth + ageSpec += grayWidth + } + if (urls.get(target)) { + _sourceUrl = chalk.underline(`https://${urls.get(target)}`) + if (supportsColor) { + urlSpec += grayWidth + } + } else if (_alias.rules) { + _sourceUrl = chalk.gray(`[${_alias.rules.length} custom rule${_alias.rules.length > 1 ? 's' : ''}]`) + if (supportsColor) { + urlSpec += underlineWidth + } + } else { + _sourceUrl = chalk.gray('') + } + + const time = chalk.gray(ms(current - new Date(_alias.created))) + text += printf( + ` %-${urlSpec}s %-${aliasSpec}s %${ageSpec}s\n`, + _sourceUrl, + _url, + time + ) + }) + + console.log(text) break } case 'rm': @@ -311,7 +344,6 @@ async function run({ token, config: { currentTeam, user } }) { currentTeam, user ) - break } if (argv.rules) {