-
Notifications
You must be signed in to change notification settings - Fork 150
/
cli.ts
275 lines (234 loc) Β· 8.69 KB
/
cli.ts
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import * as path from 'path'
import type { RawAxiosRequestConfig } from 'axios'
import chalk from 'chalk'
import inquirer from 'inquirer'
import { Listr } from 'listr2'
import { createManagementClient } from './lib/contentful-client'
import createMigrationParser, { ParseResult } from '../lib/migration-parser'
import { renderPlan, renderRuntimeErrors, renderValidationErrors } from './lib/render-migration'
import renderStepsErrors from './lib/steps-errors'
import writeErrorsToLog from './lib/write-errors-to-log'
import { RequestBatch } from '../lib/offline-api'
import { getConfig } from './lib/config'
import ValidationError from '../lib/interfaces/errors'
import { PlainClientAPI } from 'contentful-management'
import trim from 'lodash/trim'
import { SpaceAccessError } from '../lib/errors'
import pThrottle from 'p-throttle'
const { version } = require('../../package.json')
class ManyError extends Error {
public errors: (Error | ValidationError)[]
constructor(message: string, errors: (Error | ValidationError)[]) {
super(message)
this.errors = errors
}
}
class BatchError extends Error {
public batch: RequestBatch
public errors: Error[]
constructor(message: string, batch: RequestBatch, errors: Error[]) {
super(message)
this.batch = batch
this.errors = errors
}
}
function hasManyErrors(error: unknown): error is ManyError | BatchError {
return error instanceof ManyError || error instanceof BatchError
}
const makeTerminatingFunction =
({ shouldThrow }) =>
(error: any) => {
if (shouldThrow) {
throw error
} else {
process.exit(1)
}
}
export const createMakeRequest = (
client: PlainClientAPI,
{ spaceId, environmentId, limit = 10, host = 'api.contentful.com' }
) => {
const throttle = pThrottle({
limit,
interval: 1000,
strict: false
})
const makeBaseUrl = (url: string) => {
const parts = [
`https://${host}`,
'spaces',
spaceId,
'environments',
environmentId,
trim(url, '/')
]
return parts.filter((x) => x !== '').join('/')
}
return function makeRequest(requestConfig: RawAxiosRequestConfig) {
const { url, ...config } = requestConfig
const fullUrl = makeBaseUrl(url)
return throttle(() => client.raw.http(fullUrl, config))()
}
}
const getMigrationFunctionFromFile = (
filePath: string,
terminate: ReturnType<typeof makeTerminatingFunction>
) => {
try {
return require(filePath)
} catch (e) {
const message = chalk`{red.bold The ${filePath} script could not be parsed, as it seems to contain syntax errors.}\n`
console.error(message)
console.error(e)
terminate(new Error(message))
}
}
const createRun = ({ shouldThrow }) =>
async function run(argv) {
const terminate = makeTerminatingFunction({ shouldThrow })
const migrationFunction =
argv.migrationFunction || getMigrationFunctionFromFile(argv.filePath, terminate)
const application = argv.managementApplication || `contentful.migration-cli/${version}`
const feature = argv.managementFeature || `migration-library`
const clientConfig = Object.assign(
{
application,
feature
},
getConfig(argv)
)
// allow users to override the default host via the contentful-cli
argv.host && Object.assign(clientConfig, { host: argv.host })
const client = createManagementClient(clientConfig)
const makeRequest = createMakeRequest(client, {
spaceId: clientConfig.spaceId,
environmentId: clientConfig.environmentId,
limit: argv.requestLimit,
host: clientConfig.host
})
const migrationParser = createMigrationParser(makeRequest, clientConfig)
let parseResult: ParseResult
try {
parseResult = await migrationParser(migrationFunction)
} catch (e) {
if (e instanceof SpaceAccessError) {
const message = [
chalk`{red.bold ${e.message}}\n`,
chalk`π¨ {bold.red Migration unsuccessful}`
].join('\n')
console.error(message)
terminate(new Error(message))
}
console.error(e)
terminate(e)
}
if (parseResult.hasStepsValidationErrors()) {
renderStepsErrors(parseResult.stepsValidationErrors)
terminate(new ManyError('Step Validation Errors', parseResult.stepsValidationErrors))
}
if (parseResult.hasPayloadValidationErrors()) {
renderStepsErrors(parseResult.payloadValidationErrors)
terminate(new ManyError('Payload Validation Errors', parseResult.payloadValidationErrors))
}
const migrationName = argv.migrationFunction
? argv.migrationFunction.name
: path.basename(argv.filePath, '.js')
const errorsFile = path.join(process.cwd(), `errors-${migrationName}-${Date.now()}.log`)
const batches = parseResult.batches
if (parseResult.hasValidationErrors()) {
renderValidationErrors(batches, argv.environmentId)
terminate(new ManyError('Validation Errors', parseResult.getValidationErrors()))
}
if (parseResult.hasRuntimeErrors()) {
renderRuntimeErrors(batches, errorsFile)
await writeErrorsToLog(parseResult.getRuntimeErrors(), errorsFile)
terminate(new ManyError('Runtime Errors', parseResult.getRuntimeErrors()))
}
await renderPlan(batches, argv.environmentId, argv.quiet)
const serverErrorsWritten = []
const tasks = batches.map((batch) => {
return {
title: batch.intent.toPlanMessage().heading,
task: () =>
new Listr([
{
title: 'Making requests',
task: async (_ctx, task) => {
// TODO: We wanted to make this an async interator
// So we should not inspect the length but have a property for that
const numRequests = batch.requests.length
const requestErrors = []
let requestsDone = 0
for (const request of batch.requests) {
requestsDone += 1
task.title = `Making requests (${requestsDone}/${numRequests})`
task.output = `${request.method} ${request.url} at V${request.headers['X-Contentful-Version']}`
await makeRequest(request).catch((error) => {
serverErrorsWritten.push(writeErrorsToLog(error, errorsFile))
let errorMessage
if (error instanceof TypeError) {
errorMessage = {
message: 'Value does not match the expected type',
details: {
message: error.message.toString()
}
}
} else {
const parsed = JSON.parse(error.message)
errorMessage = {
status: parsed.statusText,
message: parsed.message,
details: parsed.details,
url: parsed.request.url
}
}
requestErrors.push(new Error(JSON.stringify(errorMessage)))
})
}
// Finish batch and only then throw all errors in there
if (requestErrors.length) {
throw new BatchError(`Batch failed`, batch, requestErrors)
}
}
}
])
}
})
const confirm = async function (options: { skipConfirmation: boolean }) {
if (options.skipConfirmation) {
return { applyMigration: true }
}
return inquirer.prompt([
{
type: 'confirm',
message: 'Do you want to apply the migration',
name: 'applyMigration'
}
])
}
const answers = await confirm({ skipConfirmation: argv.yes })
if (answers.applyMigration) {
try {
const successfulMigration = await new Listr(tasks).run()
console.log(chalk`π {bold.green Migration successful}`)
return successfulMigration
} catch (err: unknown) {
console.error(chalk`π¨ {bold.red Migration unsuccessful}`)
if (err instanceof Error) {
console.error(chalk`{red ${err.message}}\n`)
if (hasManyErrors(err) && Array.isArray(err.errors)) {
err.errors.forEach((err: any) => console.error(chalk`{red ${err}}\n\n`))
}
} else {
console.error(chalk`{red ${err}}\n`)
}
await Promise.all(serverErrorsWritten)
console.error(`Please check the errors log for more details: ${errorsFile}`)
terminate(err)
}
} else {
console.warn(chalk`β οΈ {bold.yellow Migration aborted}`)
}
}
export const runMigration = createRun({ shouldThrow: true })
export default createRun({ shouldThrow: false })