Skip to content

Commit

Permalink
feat: improve print function and normalize outputs (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nacho Anaya authored Aug 2, 2021
1 parent fc8196c commit 96dadb1
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 79 deletions.
64 changes: 52 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
"conf-cli": "0.1.9",
"consola": "2.15.0",
"console.table": "0.10.0",
"moment": "2.29.1",
"open": "7.3.1",
"date-fns": "2.23.0",
"text-table": "0.2.0",
"update-notifier": "5.0.1",
"yaml": "1.10.0"
},
Expand All @@ -82,7 +82,6 @@
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "3.3.1",
"eslint-plugin-promise": "4.2.1",
"globby": "10.0.2",
"jest": "26.6.3",
"lint-staged": "11.0.1",
"prettier": "2.2.1",
Expand Down
11 changes: 2 additions & 9 deletions src/commands/checks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { Command, flags } = require('@oclif/command')
const checks = require('../modules/checks')
const config = require('../services/config')

const defaultOutput = config.get('output')
const { output } = require('../services/flags')

class ChecksCommand extends Command {
static args = [
Expand Down Expand Up @@ -37,12 +35,7 @@ class ChecksCommand extends Command {
ChecksCommand.description = 'Manage Checks'

ChecksCommand.flags = {
output: flags.string({
char: 'o',
description: 'output type',
default: defaultOutput,
options: ['text', 'table', 'json'],
}),
output,
checkName: flags.string({
char: 'c',
description: 'Check upon which to execute action',
Expand Down
13 changes: 3 additions & 10 deletions src/commands/status.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { Command, flags } = require('@oclif/command')
const { Command } = require('@oclif/command')
const checkStatuses = require('../modules/check_statuses')
const config = require('../services/config')

const defaultOutput = config.get('output')
const { output } = require('../services/flags')

class StatusCommand extends Command {
static args = [
Expand All @@ -23,12 +21,7 @@ class StatusCommand extends Command {
StatusCommand.description = 'Status dashboard'

StatusCommand.flags = {
output: flags.string({
char: 'o',
description: 'output type',
default: defaultOutput,
options: ['text', 'json'],
}),
output,
}

module.exports = StatusCommand
6 changes: 4 additions & 2 deletions src/modules/check_statuses/info.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const consola = require('consola')
const chalk = require('chalk')
const moment = require('moment')
const formatDistanceToNow = require('date-fns/formatDistanceToNow')

const { checkStatuses } = require('../../services/api')

const { print } = require('../../services/utils')

async function getStatus({ output } = {}) {
try {
const { data } = await checkStatuses.getAll()

const formatted = data.map((checkStatus) => {
return {
status: checkStatus.hasFailures
Expand All @@ -16,7 +18,7 @@ async function getStatus({ output } = {}) {
? chalk.bgYellow('Degraded')
: chalk.bgGreen('Passing'),
name: checkStatus.name,
'last updated': moment(checkStatus.updated_at).fromNow(),
'last updated': formatDistanceToNow(new Date(checkStatus.updated_at)),
}
})
print(formatted, { output })
Expand Down
4 changes: 2 additions & 2 deletions src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const api = axios.create({
})

api.interceptors.request.use(function (config) {
cli.action.start('Fetching..')
cli.action.start('Fetching')
return config
})

api.interceptors.response.use(
(res) => {
cli.action.stop('done')
cli.action.stop('done!')
return res
},
(error) => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { version } = require('../../package.json')
module.exports = {
version,
env: 'production',
output: 'text',
output: 'human',

production: {
apiUrl: 'https://api.checklyhq.com/',
Expand Down
13 changes: 13 additions & 0 deletions src/services/flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { flags } = require('@oclif/command')

const config = require('../services/config')
const defaultOutput = config.get('output')

module.exports = {
output: flags.string({
char: 'o',
description: 'output type',
default: defaultOutput,
options: ['plain', 'human', 'json'],
}),
}
33 changes: 10 additions & 23 deletions src/services/utils.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
const consola = require('consola')
const { cli } = require('cli-ux')
const table = require('text-table')
require('console.table')

function print(data, { output } = {}) {
if (!data) {
return
}

function print(data, { output = 'text', format = 'table', type = 'info' }) {
if (output === 'json') {
consola.log(JSON.stringify(data, null, 2))
return
}

process.stdout.write('\n')

if (format === 'table') {
cli.table(data, {
id: { header: 'ID' },
name: {},
checkType: { header: 'Check Type' },
frequency: {},
locations: {
get: (row) => row.locations.join(','),
},
activated: {
get: (row) => (row.activated === true ? '✅' : '❌'),
},
})
return
}

if (format === 'text') {
consola.log(
data.map((fields) => Object.values(fields).join(', ')).join('\n')
)
if (output === 'plain') {
console.log(table(data.map((fields) => Object.values(fields))))
return
}

consola[type](data)
console.table(data)
}

module.exports = {
Expand Down
17 changes: 0 additions & 17 deletions src/services/yml.js

This file was deleted.

0 comments on commit 96dadb1

Please sign in to comment.