Skip to content

Commit

Permalink
[cli] Replace tty-table dependency with simple list (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Dec 25, 2018
1 parent a946f2e commit ac9e691
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
1 change: 0 additions & 1 deletion packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"semver-compare": "^1.0.0",
"simple-get": "^3.0.2",
"split2": "^2.1.1",
"tty-table": "^2.6.8",
"validate-npm-package-name": "^3.0.0",
"webpack": "^4.14.0",
"which": "^1.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/scripts/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const compiler = webpack({
use: [{loader: 'babel-loader', options: babelRc}]
},
{
test: /node_modules[/\\](rc|tty-table)[/\\]/,
test: /node_modules[/\\](rc)[/\\]/,
use: [{loader: shebangLoader}]
}
]
Expand Down
57 changes: 41 additions & 16 deletions packages/@sanity/cli/src/commands/projects/listProjectsCommand.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
const Table = require('tty-table')
const {size, sortBy} = require('lodash')

const headings = ['id', 'members', 'name', 'url', 'created']
const helpText = `
Options
--sort <field> Sort output by specified column
--order <asc/desc> Sort output ascending/descending
Examples
# List projects
sanity projects list
# List projects sorted by member count, ascending
sanity projects list --sort=members --order=asc
`

const defaultFlags = {
sort: 'created',
order: 'desc'
}

export default {
name: 'list',
group: 'projects',
signature: '',
helpText,
description: 'Lists projects connected to your user',
action: async (args, context) => {
const {apiClient, output} = context
const {apiClient, output, chalk} = context
const flags = {...defaultFlags, ...args.extOptions}
const client = apiClient({
requireUser: true,
requireProject: false
Expand All @@ -15,21 +36,25 @@ export default {
method: 'GET',
uri: '/projects'
})
const maxWidth = col => projects.reduce((max, current) => Math.max(current[col].length, max), 0)
const rows = projects.map(({displayName, id, members = [], studioHost = ''}) => {
const studio = studioHost ? `https://${studioHost}.sanity.studio` : 'Not deployed'
const row = [id, members.length, displayName, studio]
return row
})
const table = new Table(
['id', 'members #', 'name', 'url'].map(value => ({
value,
align: 'left',
headerColor: 'green'
})),
rows

const ordered = sortBy(
projects.map(({displayName, id, members = [], studioHost = '', createdAt}) => {
const studio = studioHost ? `https://${studioHost}.sanity.studio` : 'Not deployed'
return [id, members.length, displayName, studio, createdAt]
}),
[headings.indexOf(flags.sort)]
)

output.print(table.render())
const rows = flags.order === 'asc' ? ordered : ordered.reverse()

const maxWidths = rows.reduce(
(max, row) => row.map((current, index) => Math.max(size(current), max[index])),
headings.map(str => size(str))
)

const printRow = row => row.map((col, i) => `${col}`.padEnd(maxWidths[i])).join(' ')

output.print(chalk.cyan(printRow(headings)))
rows.forEach(row => output.print(printRow(row)))
}
}

0 comments on commit ac9e691

Please sign in to comment.