-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcleanup.ts
More file actions
165 lines (135 loc) · 5.14 KB
/
cleanup.ts
File metadata and controls
165 lines (135 loc) · 5.14 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
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
import type { SemVer } from '@std/semver'
import type { SupabaseClient } from '@supabase/supabase-js'
import type { BundleCleanupOptions } from '../schemas/bundle'
import type { Database } from '../types/supabase.types'
import { confirm as confirmC, intro, isCancel, log, outro } from '@clack/prompts'
import {
format,
greaterThan,
increment,
lessThan,
parse,
} from '@std/semver'
import { check2FAComplianceForApp, checkAppExistsAndHasPermissionOrgErr } from '../api/app'
import { checkAlerts } from '../api/update'
import { deleteSpecificVersion, displayBundles, getActiveAppVersions, getChannelsVersion } from '../api/versions'
import {
createSupabaseClient,
findSavedKey,
getAppId,
getConfig,
getHumanDate,
OrganizationPerm,
resolveUserIdFromApiKey,
} from '../utils'
async function removeVersions(
toRemove: Database['public']['Tables']['app_versions']['Row'][],
supabase: SupabaseClient<Database>,
appId: string,
silent: boolean,
) {
for await (const row of toRemove) {
if (!silent)
log.warn(`Removing ${row.name} created on ${getHumanDate(row.created_at)}`)
await deleteSpecificVersion(supabase, appId, row.name)
}
}
function getRemovableVersionsInSemverRange(
data: Database['public']['Tables']['app_versions']['Row'][],
bundleVersion: SemVer,
nextMajorVersion: SemVer,
) {
const toRemove: Database['public']['Tables']['app_versions']['Row'][] = []
for (const row of data ?? []) {
const rowVersion = parse(row.name)
if (greaterThan(rowVersion, bundleVersion) && lessThan(rowVersion, nextMajorVersion))
toRemove.push(row)
}
return toRemove
}
export async function cleanupBundleInternal(appId: string, options: BundleCleanupOptions, silent = false) {
if (!silent)
intro('Cleanup versions in Capgo')
await checkAlerts()
options.apikey = options.apikey || findSavedKey()
const { bundle, keep = 4 } = options
const force = options.force || false
const ignoreChannel = options.ignoreChannel || false
const extConfig = await getConfig()
appId = getAppId(appId, extConfig?.config)
if (!options.apikey) {
if (!silent)
log.error('Missing API key, you need to provide an API key to delete your app')
throw new Error('Missing API key')
}
if (!appId) {
if (!silent)
log.error('Missing argument, you need to provide a appid, or be in a capacitor project')
throw new Error('Missing appId')
}
const supabase = await createSupabaseClient(options.apikey, options.supaHost, options.supaAnon)
await check2FAComplianceForApp(supabase, appId, silent)
await resolveUserIdFromApiKey(supabase, options.apikey)
await checkAppExistsAndHasPermissionOrgErr(supabase, options.apikey, appId, OrganizationPerm.write, silent, true)
if (!silent)
log.info('Querying all available versions in Capgo')
let allVersions: (Database['public']['Tables']['app_versions']['Row'] & { keep?: string })[] = await getActiveAppVersions(supabase, appId)
const versionInUse = await getChannelsVersion(supabase, appId)
if (!silent)
log.info(`Total active versions in Capgo: ${allVersions?.length ?? 0}`)
if (!allVersions?.length) {
if (!silent)
log.error('No versions found, aborting cleanup')
throw new Error('No versions found')
}
if (bundle) {
const bundleVersion = parse(bundle)
const nextMajorVersion = increment(bundleVersion, 'major')
if (!silent)
log.info(`Querying available versions in Capgo between ${format(bundleVersion)} and ${format(nextMajorVersion)}`)
allVersions = getRemovableVersionsInSemverRange(allVersions, bundleVersion, nextMajorVersion) as (Database['public']['Tables']['app_versions']['Row'] & { keep: string })[]
if (!silent)
log.info(`Active versions in Capgo between ${format(bundleVersion)} and ${format(nextMajorVersion)}: ${allVersions?.length ?? 0}`)
}
const toRemove: (Database['public']['Tables']['app_versions']['Row'] & { keep?: string })[] = []
let kept = 0
for (const v of allVersions) {
const isInUse = versionInUse.find(vi => vi === v.id)
if (kept < keep || (isInUse && !ignoreChannel)) {
v.keep = isInUse ? '✅ (Linked to channel)' : '✅'
kept += 1
}
else {
v.keep = '❌'
toRemove.push(v)
}
}
if (!toRemove.length) {
if (!silent)
log.warn('Nothing to be removed, aborting removal...')
return { removed: 0, kept }
}
if (!silent)
displayBundles(allVersions)
if (!force) {
if (!silent) {
const doDelete = await confirmC({ message: 'Do you want to continue removing the versions specified?' })
if (isCancel(doDelete) || !doDelete) {
log.warn('Not confirmed, aborting removal...')
throw new Error('Cleanup cancelled by user')
}
}
else {
throw new Error('Cleanup requires force=true in SDK mode to prevent accidental deletions')
}
}
if (!silent)
log.success('You have confirmed removal, removing versions now')
await removeVersions(toRemove, supabase, appId, silent)
if (!silent)
outro('Done ✅')
return { removed: toRemove.length, kept }
}
export async function cleanupBundle(appId: string, options: BundleCleanupOptions) {
return cleanupBundleInternal(appId, options)
}