/
get.js
70 lines (55 loc) · 2.13 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const Table = require('cli-table3')
const { createManagementClient } = require('../../utils/contentful-clients')
const { log } = require('../../utils/log')
const { handleAsyncError: handle } = require('../../utils/async')
const { getId } = require('../../utils/helpers')
const { getHeadersFromOption } = require('../../utils/headers')
module.exports.command = 'get'
module.exports.desc = 'Show a content type'
module.exports.builder = yargs => {
return yargs
.option('id', { type: 'string', demand: true, describe: 'Content Type id' })
.option('space-id', { alias: 's', type: 'string', describe: 'Space id' })
.option('management-token', {
alias: 'mt',
type: 'string',
describe: 'Contentful management API token'
})
.option('environment-id', { type: 'string', describe: 'Environment id' })
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}
async function ctShow(argv) {
const contentTypeId = getId(argv)
const { managementToken, activeSpaceId, activeEnvironmentId } = argv.context
const client = await createManagementClient({
accessToken: managementToken,
feature: 'content_type-get',
headers: getHeadersFromOption(argv.header)
})
const space = await client.getSpace(activeSpaceId)
const environment = await space.getEnvironment(activeEnvironmentId)
const result = await environment.getContentType(contentTypeId)
const { sys, name, displayField, fields } = result
const table = new Table({
head: ['CT Property', 'CT Value']
})
table.push(['ID', sys.id])
table.push(['Name', name])
table.push(['Display Field', displayField])
log(table.toString())
const fieldsTable = new Table({
head: ['*', 'Field ID', 'Field Name', 'Field Type', 'Required']
})
fields.forEach(({ id, name, type, required }) => {
const isDisplayField = id === displayField
const displayFieldIndicator = isDisplayField ? '*' : ''
fieldsTable.push([displayFieldIndicator, id, name, type, required])
})
log(fieldsTable.toString())
}
module.exports.handler = handle(ctShow)