-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdeprecate.ts
More file actions
91 lines (77 loc) · 3.58 KB
/
Copy pathdeprecate.ts
File metadata and controls
91 lines (77 loc) · 3.58 KB
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
import chalk from 'chalk'
import { createRegistryClient } from '../../api/clients/IOClients/infra/Registry'
import { ErrorReport } from '../../api/error/ErrorReport'
import { ManifestEditor, ManifestValidator } from '../../api/manifest'
import { SessionManager } from '../../api/session/SessionManager'
import { TelemetryCollector } from '../../lib/telemetry/TelemetryCollector'
import { parseLocator } from '../../api/locator'
import log from '../../api/logger'
import { returnToPreviousAccount, switchAccount } from '../../api/modules/auth/switch'
import { promptConfirm } from '../../api/modules/prompts'
let originalAccount
let originalWorkspace
const switchToVendorMessage = (vendor: string): string => {
return `You are trying to deprecate this app in an account that differs from the indicated vendor. Do you want to deprecate in account ${chalk.blue(
vendor
)}?`
}
const promptDeprecate = (appsList: string[]) =>
promptConfirm(
`Are you sure you want to deprecate app${appsList.length > 1 ? 's' : ''} ${chalk.green(appsList.join(', '))}?`
)
const deprecateApp = async (app: string): Promise<void> => {
const { vendor, name, version } = parseLocator(app)
const session = SessionManager.getSingleton()
if (vendor !== session.account) {
const canSwitchToVendor = await promptConfirm(switchToVendorMessage(vendor))
if (!canSwitchToVendor) {
return
}
await switchAccount(vendor, {})
}
const context = { account: vendor, workspace: 'master', authToken: session.token }
const registry = createRegistryClient(context)
return registry.deprecateApp(`${vendor}.${name}`, version)
}
const prepareAndDeprecateApps = async (appsList: string[]): Promise<any> => {
for (const app of appsList) {
ManifestValidator.validateApp(app)
log.debug('Starting to deprecate app:', app)
try {
// eslint-disable-next-line no-await-in-loop
await deprecateApp(app)
log.info('Successfully deprecated', app)
} catch (e) {
const errReport = ErrorReport.create({ originalError: e })
if (e.response && e.response.status && e.response.status === 404) {
log.error(`Error deprecating ${app}. App not found`)
errReport.logErrorForUser({ coreLogLevelDefault: 'debug' })
TelemetryCollector.getCollector().registerError(errReport)
} else if (e.message && e.response.statusText) {
log.error(`Error deprecating ${app}. ${e.message}. ${e.response.statusText}`)
errReport.logErrorForUser({ coreLogLevelDefault: 'debug' })
TelemetryCollector.getCollector().registerError(errReport)
return returnToPreviousAccount({ previousAccount: originalAccount, previousWorkspace: originalWorkspace })
} else {
// eslint-disable-next-line no-await-in-loop
await returnToPreviousAccount({ previousAccount: originalAccount, previousWorkspace: originalWorkspace })
throw errReport
}
}
}
await returnToPreviousAccount({ previousAccount: originalAccount, previousWorkspace: originalWorkspace })
}
export default async (optionalApps: string[], options, pipeline: boolean = false) => {
const preConfirm = options.y || options.yes
const { account, workspace } = SessionManager.getSingleton()
originalAccount = account
originalWorkspace = workspace
const appsList = optionalApps.length > 0 ? optionalApps : [(await ManifestEditor.getManifestEditor()).appLocator]
if(!pipeline){
if (!preConfirm && !(await promptDeprecate(appsList))) {
return
}
}
log.debug(`Deprecating app${appsList.length > 1 ? 's' : ''}: ${appsList.join(', ')}`)
return prepareAndDeprecateApps(appsList)
}