Skip to content

Commit e9e5757

Browse files
committed
refactoring output
1 parent 1c4172a commit e9e5757

File tree

13 files changed

+117
-79
lines changed

13 files changed

+117
-79
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"@contentstack/cli-command": "0.1.0-beta",
99
"@oclif/command": "^1.8.0",
1010
"@oclif/config": "^1.17.0",
11+
"@types/git-diff": "^2.0.0",
1112
"@types/table": "^6.0.0",
13+
"@types/tmp": "^0.2.0",
1214
"axios": "^0.21.0",
1315
"cli-ux": "^5.5.0",
1416
"diff2html": "^3.1.17",

src/commands/command.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Command} from '@contentstack/cli-command'
2+
import { BuildOutput } from '../core/content-type/build-output';
23
import ContentstackClient from '../core/contentstack/client'
34

45
export default class ContentTypeCommand extends Command {
@@ -29,4 +30,25 @@ export default class ContentTypeCommand extends Command {
2930

3031
this.client = new ContentstackClient(this.cmaHost, this.authToken)
3132
}
33+
34+
printOutput(output: BuildOutput, who: string, what: string | null, where: string) {
35+
this.log(`Displaying ${who} ${what ? `for ${what}` : ''} on '${where}.'`)
36+
this.log('---\n')
37+
38+
if (output.hasResults) {
39+
if (output.header) {
40+
this.log(output.header)
41+
}
42+
43+
if (output.body) {
44+
this.log(output.body)
45+
}
46+
47+
if (output.footer) {
48+
this.log(output.footer)
49+
}
50+
} else {
51+
this.log(`No ${who} found.`)
52+
}
53+
}
3254
}

src/commands/content-type/audit.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {flags} from '@contentstack/cli-command'
33
import buildOutput from '../../core/content-type/audit'
44

55
export default class AuditCommand extends Command {
6-
static description = 'display audit logs for recent changes to a Content Type';
6+
static description = 'display Audit Logs for recent changes to a Content Type';
77

88
static examples = [
99
'$ csdx content-type:audit -s "xxxxxxxxxxxxxxxxxxx" -c "home_page"',
@@ -43,15 +43,8 @@ export default class AuditCommand extends Command {
4343
this.client.getUsers(this.apiKey),
4444
])
4545

46-
this.log(`Displaying audit logs for '${flags['content-type']}' on '${stack.name}.'\n`)
47-
4846
const output = buildOutput(audit.logs, users)
49-
50-
if (output.hasRows) {
51-
this.log(output.body)
52-
} else {
53-
this.log('No audit logs found.')
54-
}
47+
this.printOutput(output, 'Audit Logs', flags['content-type'], stack.name)
5548
} catch (error) {
5649
this.error(error, {exit: 1, suggestions: error.suggestions})
5750
}

src/commands/content-type/compare.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export default class CompareCommand extends Command {
77

88
static examples = [
99
'$ csdx content-type:compare -s "xxxxxxxxxxxxxxxxxxx" -c "home_page"',
10-
'$ csdx content-type:compare -s "xxxxxxxxxxxxxxxxxxx" -c "home_page" -left # -right #',
11-
'$ csdx content-type:compare -a "management token" -c "home_page" -left # -right #',
10+
'$ csdx content-type:compare -s "xxxxxxxxxxxxxxxxxxx" -c "home_page" -l # -r #',
11+
'$ csdx content-type:compare -a "management token" -c "home_page" -l # -r #',
1212
];
1313

1414
static flags = {
@@ -35,13 +35,13 @@ export default class CompareCommand extends Command {
3535

3636
left: flags.integer({
3737
char: 'l',
38-
description: 'previous Content Type version',
38+
description: 'Content Type version',
3939
required: true,
4040
}),
4141

4242
right: flags.integer({
4343
char: 'r',
44-
description: 'current Content Type version',
44+
description: 'Content Type version',
4545
required: true,
4646
}),
4747
}
@@ -57,8 +57,8 @@ export default class CompareCommand extends Command {
5757
this.client.getContentType(this.apiKey, flags['content-type'], true, flags.right),
5858
])
5959

60-
this.log(`Displaying details for '${flags['content-type']}' on '${stack.name}.'`)
61-
await buildOutput(flags['content-type'], previous, current)
60+
const output = await buildOutput(flags['content-type'], previous, current)
61+
this.printOutput(output, 'changes', flags['content-type'], stack.name)
6262
} catch (error) {
6363
this.error(error, {exit: 1, suggestions: error.suggestions})
6464
}

src/commands/content-type/details.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@ export default class DetailsCommand extends Command {
4343
this.client.getContentTypeReferences(this.apiKey, flags['content-type']),
4444
])
4545

46-
this.log(`Displaying details for '${flags['content-type']}' on '${stack.name}.'`)
4746
const output = buildOutput(contentType, references)
48-
49-
if (output.hasRows) {
50-
this.log(output.header)
51-
this.log(output.body)
52-
} else {
53-
this.log('No details found.')
54-
}
47+
this.printOutput(output, 'details', flags['content-type'], stack.name)
5548
} catch (error) {
5649
this.error(error, {exit: 1, suggestions: error.suggestions})
5750
}

src/commands/content-type/list.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ export default class ListCommand extends Command {
4646
])
4747

4848
const output = buildOutput(contentTypes, flags.order)
49-
50-
this.log(`Displaying Content Types for '${stack.name}.'\n`)
51-
52-
if (output.hasRows) {
53-
this.log(output.body)
54-
this.log(output.footer)
55-
} else {
56-
this.log('No Content Types found.')
57-
}
49+
this.printOutput(output, 'Content Types', null, stack.name)
5850
} catch (error) {
5951
this.error(error, {exit: 1, suggestions: error.suggestions})
6052
}

src/core/content-type/audit.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as tableImport from 'table'
2+
import { BuildOutput } from './build-output'
23
import * as format from './formatting'
34

45
const {table} = tableImport
56

6-
export default function buildOutput(logs: any, users: any) {
7+
export default function buildOutput(logs: any, users: any): BuildOutput {
78
const rows = []
89

910
rows.push(['Event', 'User', 'Modified', 'Version'])
@@ -18,7 +19,9 @@ export default function buildOutput(logs: any, users: any) {
1819
}
1920

2021
return {
22+
header: null,
2123
body: table(rows),
22-
hasRows: rows.length > 1,
24+
footer: null,
25+
hasResults: rows.length > 1,
2326
}
2427
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type BuildOutput = {
2+
header: string | null,
3+
body: string,
4+
footer: string | null,
5+
hasResults: boolean
6+
}

src/core/content-type/compare.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import cli from 'cli-ux'
2+
import * as fs from 'fs'
3+
import * as tmp from 'tmp'
4+
import * as Diff2html from 'diff2html'
5+
import gitDiff from 'git-diff'
6+
import { BuildOutput } from './build-output'
27

3-
const Diff2html = require('diff2html')
4-
const gitDiff = require('git-diff')
5-
6-
const tmp = require('tmp')
7-
const fs = require('fs')
8-
9-
export default async function buildOutput(contentTypeName: string, previous: any, current: any) {
8+
export default async function buildOutput(contentTypeName: string, previous: any, current: any): Promise<BuildOutput> {
109
const diffString = buildDiffString(previous, current)
1110
const diffTree = Diff2html.parse(diffString)
1211
const diffHtml = Diff2html.html(diffTree, {
@@ -20,6 +19,13 @@ export default async function buildOutput(contentTypeName: string, previous: any
2019
fs.writeFileSync(path, html(diffHtml))
2120
await cli.open(path)
2221
})
22+
23+
return {
24+
header: null,
25+
body: 'Please check the browser output.',
26+
footer: null,
27+
hasResults: true
28+
}
2329
}
2430

2531
function buildDiffString(previous: any, current: any) {

0 commit comments

Comments
 (0)