/
update.js
128 lines (104 loc) · 3.96 KB
/
update.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const { handleAsyncError: handle } = require('../../utils/async')
const { createManagementClient } = require('../../utils/contentful-clients')
const { successEmoji } = require('../../utils/emojis')
const { getHeadersFromOption } = require('../../utils/headers')
const { success } = require('../../utils/log')
const { createExtension } = require('./create')
const {
assertExtensionValuesProvided,
assertForceOrCorrectVersionProvided
} = require('./utils/assertions')
const { logExtension } = require('./utils/log-as-table')
const prepareData = require('./utils/prepare-data')
const readSrcDocFile = require('./utils/read-srcdoc-file')
module.exports.command = 'update'
module.exports.desc = 'Update an extension'
module.exports.builder = yargs => {
return yargs
.option('id', { type: 'string', describe: 'Extension id' })
.option('name', { type: 'string', describe: 'Extension name' })
.option('management-token', {
alias: 'mt',
type: 'string',
describe: 'Contentful management API token'
})
.option('space-id', { type: 'string', describe: 'Space id' })
.option('environment-id', { type: 'string', describe: 'Environment id' })
.option('field-types', { type: 'array', describe: 'Field types' })
.option('descriptor', {
type: 'string',
describe: 'Path to an extension descriptor file'
})
.option('src', { type: 'string', describe: 'URL to extension bundle' })
.option('srcdoc', { type: 'string', describe: 'Path to extension bundle' })
.option('sidebar', {
type: 'boolean',
// We set the default to undefined so the descriptor file value will be
// used instead of arg value unless explicitly passed in
default: undefined,
describe: 'Render the extension in the sidebar'
})
.option('version', {
type: 'number',
describe: 'Current version of the extension for optimistic locking'
})
.option('force', {
type: 'boolean',
describe: 'Force operation without explicit version'
})
.option('installation-parameters', {
type: 'string',
describe: 'JSON string of installation parameter key-value pairs'
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}
async function updateExtension(extension, data) {
extension.extension = data.extension
extension.parameters = data.parameters
const updated = await extension.update()
return updated
}
module.exports.updateExtension = updateExtension
async function updateExtensionHandler(argv) {
const data = await prepareData(argv)
await assertExtensionValuesProvided(data, 'update')
if (data.extension.srcdoc) {
await readSrcDocFile(data.extension)
}
const { managementToken, activeSpaceId, activeEnvironmentId } = argv.context
const client = await createManagementClient({
accessToken: managementToken,
feature: 'extension-update',
headers: getHeadersFromOption(argv.header)
})
const space = await client.getSpace(activeSpaceId)
const environment = await space.getEnvironment(activeEnvironmentId)
let extension = null
let operation = 'update'
try {
extension = await environment.getUiExtension(data.id)
} catch (e) {
if (argv.force) {
operation = 'create'
} else {
throw e
}
}
if (operation === 'update' && extension) {
await assertForceOrCorrectVersionProvided(argv, extension.sys.version)
const updated = await updateExtension(extension, data)
success(`${successEmoji} Successfully updated extension:\n`)
logExtension(updated, activeSpaceId, activeEnvironmentId)
} else if (operation === 'create') {
const created = await createExtension(environment, data)
success(`${successEmoji} Successfully created extension:\n`)
logExtension(created, activeSpaceId, activeEnvironmentId)
}
}
module.exports.updateExtensionHandler = updateExtensionHandler
module.exports.handler = handle(updateExtensionHandler)