-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathset.ts
More file actions
134 lines (116 loc) · 3.92 KB
/
set.ts
File metadata and controls
134 lines (116 loc) · 3.92 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
import type { Buffer } from 'node:buffer'
import type { Options } from '../api/app'
import { existsSync, readFileSync } from 'node:fs'
import { intro, log, outro } from '@clack/prompts'
import { checkAppExistsAndHasPermissionOrgErr, defaultAppIconPath, getAppIconStoragePath, newIconPath } from '../api/app'
import {
createSupabaseClient,
findSavedKey,
formatError,
getAppId,
getConfig,
getContentType,
getOrganizationId,
OrganizationPerm,
sendEvent,
} from '../utils'
export async function setAppInternal(appId: string, options: Options, silent = false) {
if (!silent)
intro('Set app')
options.apikey = options.apikey || findSavedKey()
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 upload your bundle')
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 checkAppExistsAndHasPermissionOrgErr(supabase, options.apikey, appId, OrganizationPerm.admin, silent)
const organizationUid = await getOrganizationId(supabase, appId)
const { name, icon, retention, exposeMetadata } = options
if (retention && Number.isNaN(Number(retention))) {
if (!silent)
log.error('retention value must be a number')
throw new Error('Retention value must be a number')
}
else if (retention && retention < 0) {
if (!silent)
log.error('retention value cannot be less than 0')
throw new Error('Retention value cannot be less than 0')
}
else if (retention && retention >= 63113904) {
if (!silent)
log.error('retention value cannot be greater than 63113904 seconds (2 years)')
throw new Error('Retention value cannot be greater than 63113904 seconds (2 years)')
}
let iconBuff: Buffer | undefined
let iconType: string | undefined
const iconPath = getAppIconStoragePath(organizationUid, appId)
let iconUrl: string | undefined = defaultAppIconPath
if (icon && existsSync(icon)) {
iconBuff = readFileSync(icon)
const contentType = getContentType(icon)
iconType = contentType || 'image/png'
if (!silent)
log.warn(`Found app icon ${icon}`)
}
else if (existsSync(newIconPath)) {
iconBuff = readFileSync(newIconPath)
const contentType = getContentType(newIconPath)
iconType = contentType || 'image/png'
if (!silent)
log.warn(`Found app icon ${newIconPath}`)
}
else if (!silent) {
log.warn(`Cannot find app icon in any of the following locations: ${icon}, ${newIconPath}`)
}
if (iconBuff && iconType) {
const { error } = await supabase.storage
.from('images')
.upload(iconPath, iconBuff, {
contentType: iconType,
upsert: true,
})
if (error) {
if (!silent)
log.error(`Could not set app ${formatError(error)}`)
throw new Error(`Could not set app: ${formatError(error)}`)
}
iconUrl = iconPath
}
const { error: dbError } = await supabase
.from('apps')
.update({
icon_url: iconUrl,
name,
retention: !retention ? undefined : retention * 24 * 60 * 60,
expose_metadata: exposeMetadata,
})
.eq('app_id', appId)
if (dbError) {
if (!silent)
log.error(`Could not set app ${formatError(dbError)}`)
throw new Error(`Could not set app: ${formatError(dbError)}`)
}
await sendEvent(options.apikey, {
channel: 'app',
event: 'App Updated',
icon: '📝',
user_id: organizationUid,
tags: { 'app-id': appId },
notify: false,
notifyConsole: true,
}).catch(() => {})
if (!silent)
outro('Done ✅')
return true
}
export async function setApp(appId: string, options: Options) {
return setAppInternal(appId, options)
}