From bc0b06721afed517c92a7bfbcf52af8710908475 Mon Sep 17 00:00:00 2001 From: Jarmo Isotalo Date: Thu, 4 May 2017 16:16:13 -0700 Subject: [PATCH] Sort `now scale ls` output similarly to `now ls` (#494) * Sort `now scale ls` similarly how `now ls` is sorted * Remove unused dependency --- bin/now-list.js | 49 +++++------------------------------------ bin/now-scale.js | 9 +++++--- lib/sort-deployments.js | 30 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 46 deletions(-) create mode 100644 lib/sort-deployments.js diff --git a/bin/now-list.js b/bin/now-list.js index 2c5dfd3..2c9251f 100755 --- a/bin/now-list.js +++ b/bin/now-list.js @@ -1,7 +1,6 @@ #!/usr/bin/env node // Packages -const fs = require('fs-promise') const minimist = require('minimist') const chalk = require('chalk') const ms = require('ms') @@ -15,6 +14,7 @@ const login = require('../lib/login') const cfg = require('../lib/cfg') const { handleError, error } = require('../lib/error') const logo = require('../lib/utils/output/logo') +const sort = require('../lib/sort-deployments') const argv = minimist(process.argv.slice(2), { string: ['config', 'token'], @@ -28,8 +28,7 @@ const argv = minimist(process.argv.slice(2), { }) const help = () => { - console.log( - ` + console.log(` ${chalk.bold(`${logo} now list`)} [app] ${chalk.dim('Options:')} @@ -50,8 +49,7 @@ const help = () => { ${chalk.cyan('$ now ls my-app')} ${chalk.dim('Alias:')} ls -` - ) +`) } if (argv.help) { @@ -146,9 +144,7 @@ async function list({ token, config: { currentTeam, user } }) { return Math.max(acc, (i.url && i.url.length) || 0) }, 0) + 5 const timeNow = new Date() - console.log( - `> ${deployments.length} deployment${deployments.length === 1 ? '' : 's'} found under ${chalk.bold((currentTeam && currentTeam.slug) || user.username || user.email)} ${chalk.grey('[' + ms(timeNow - start) + ']')}` - ) + console.log(`> ${deployments.length} deployment${deployments.length === 1 ? '' : 's'} found under ${chalk.bold((currentTeam && currentTeam.slug) || user.username || user.email)} ${chalk.grey('[' + ms(timeNow - start) + ']')}`) let shouldShowAllInfo = false for (const app of apps) { @@ -162,16 +158,12 @@ async function list({ token, config: { currentTeam, user } }) { } } if (!argv.all && shouldShowAllInfo) { - console.log( - `> To expand the list and see instances run ${chalk.cyan('`now ls --all [app]`')}` - ) + console.log(`> To expand the list and see instances run ${chalk.cyan('`now ls --all [app]`')}`) } console.log() sorted.forEach(([name, deps]) => { const listedDeployments = argv.all ? deps : deps.slice(0, 5) - console.log( - `${chalk.bold(name)} ${chalk.gray('(' + listedDeployments.length + ' of ' + deps.length + ' total)')}` - ) + console.log(`${chalk.bold(name)} ${chalk.gray('(' + listedDeployments.length + ' of ' + deps.length + ' total)')}`) const urlSpec = `%-${urlLength}s` console.log( printf( @@ -223,32 +215,3 @@ async function list({ token, config: { currentTeam, user } }) { console.log() }) } - -async function sort(apps) { - let pkg - try { - const json = await fs.readFile('package.json') - pkg = JSON.parse(json) - } catch (err) { - pkg = {} - } - - return apps - .map(([name, deps]) => { - deps = deps.slice().sort((a, b) => { - return b.created - a.created - }) - return [name, deps] - }) - .sort(([nameA, depsA], [nameB, depsB]) => { - if (pkg.name === nameA) { - return -1 - } - - if (pkg.name === nameB) { - return 1 - } - - return depsB[0].created - depsA[0].created - }) -} diff --git a/bin/now-scale.js b/bin/now-scale.js index ae5266e..14ac3a5 100755 --- a/bin/now-scale.js +++ b/bin/now-scale.js @@ -17,6 +17,7 @@ const login = require('../lib/login') const exit = require('../lib/utils/exit') const logo = require('../lib/utils/output/logo') const info = require('../lib/scale-info') +const sort = require('../lib/sort-deployments'); const argv = minimist(process.argv.slice(2), { string: ['config', 'token'], @@ -247,14 +248,16 @@ async function list(scale) { apps.set(dep.name, deps.concat(dep)) } - const timeNow = new Date() + const sorted = await sort([...apps]); + + const timeNow = new Date(); const urlLength = deployments.reduce((acc, i) => { return Math.max(acc, (i.url && i.url.length) || 0) }, 0) + 5 - for (const app of apps) { - const depls = argv.all ? app[1] : app[1].slice(0, 5) + for (const app of sorted) { + const depls = argv.all ? app[1] : app[1].slice(0, 5); console.log( `${chalk.bold(app[0])} ${chalk.gray('(' + depls.length + ' of ' + app[1].length + ' total)')}` ) diff --git a/lib/sort-deployments.js b/lib/sort-deployments.js new file mode 100644 index 0000000..a0c1f15 --- /dev/null +++ b/lib/sort-deployments.js @@ -0,0 +1,30 @@ +const fs = require('fs-promise') + +module.exports = async function(apps) { + let pkg + try { + const json = await fs.readFile('package.json') + pkg = JSON.parse(json) + } catch (err) { + pkg = {} + } + + return apps + .map(([name, deps]) => { + deps = deps.slice().sort((a, b) => { + return b.created - a.created + }) + return [name, deps] + }) + .sort(([nameA, depsA], [nameB, depsB]) => { + if (pkg.name === nameA) { + return -1 + } + + if (pkg.name === nameB) { + return 1 + } + + return depsB[0].created - depsA[0].created + }) +}