Skip to content

Commit

Permalink
feat(dataset copy list): Format output with console-table-printer (#3303
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tzhelyazkova committed Jun 8, 2022
1 parent 82f20d1 commit 4e775f3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/@sanity/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"chokidar": "^3.0.0",
"configstore": "^5.0.1",
"date-fns": "^2.16.1",
"console-table-printer": "^2.11.0",
"debug": "^3.2.7",
"deep-sort-object": "^1.0.1",
"es6-promisify": "^6.0.0",
Expand Down
61 changes: 41 additions & 20 deletions packages/@sanity/core/src/actions/dataset/listDatasetCopyJobs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {parseISO, formatDistanceToNow, formatDistance} from 'date-fns'
const {Table} = require('console-table-printer')

module.exports = async function listDatasetCopyJobs(args, context) {
const {apiClient, output, chalk} = context
Expand Down Expand Up @@ -30,8 +31,18 @@ module.exports = async function listDatasetCopyJobs(args, context) {
}

if (response && response.length > 0) {
output.print('Dataset copy jobs for this project:')
const print = []
const table = new Table({
title: 'Dataset copy jobs for this project in descending order',
columns: [
{name: 'id', title: 'Job ID', alignment: 'left'},
{name: 'sourceDataset', title: 'Source Dataset', alignment: 'left'},
{name: 'targetDataset', title: 'Target Dataset', alignment: 'left'},
{name: 'state', title: 'State', alignment: 'left'},
{name: 'withHistory', title: 'With history', alignment: 'left'},
{name: 'timeStarted', title: 'Time started', alignment: 'left'},
{name: 'timeTaken', title: 'Time taken', alignment: 'left'},
],
})

response.forEach((job) => {
const {id, state, createdAt, updatedAt, sourceDataset, targetDataset, withHistory} = job
Expand All @@ -46,26 +57,36 @@ module.exports = async function listDatasetCopyJobs(args, context) {
timeTaken = formatDistance(parseISO(updatedAt), parseISO(createdAt))
}

print.push({
'Job ID': id,
State: state,
History: withHistory,
'Time started': timeStarted === '' ? '' : `${timeStarted} ago`,
'Time taken': timeTaken,
'Source dataset': sourceDataset,
'Target dataset': targetDataset,
})
let color
switch (state) {
case 'completed':
color = 'green'
break
case 'failed':
color = 'red'
break
case 'pending':
color = 'yellow'
break
default:
color = ''
}

table.addRow(
{
id,
state,
withHistory,
timeStarted: `${timeStarted} ago`,
timeTaken,
sourceDataset,
targetDataset,
},
{color}
)
})

output.table(print, [
'Job ID',
'Source dataset',
'Target dataset',
'State',
'History',
'Time started',
'Time taken',
])
table.printTable()
} else {
output.print("This project doesn't have any dataset copy jobs")
}
Expand Down

3 comments on commit 4e775f3

@vercel
Copy link

@vercel vercel bot commented on 4e775f3 Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

studio-workshop – ./dev/workshop

studio-workshop.sanity.build
studio-workshop-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4e775f3 Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4e775f3 Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.