@capgo/cli 0.8.6 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/bin/set.ts CHANGED
@@ -1,19 +1,21 @@
1
- import { loadConfig } from '@capacitor/cli/dist/config';
2
- import axios from 'axios';
3
- import prettyjson from 'prettyjson';
4
1
  import { program } from 'commander';
5
- import { host } from './utils';
2
+ import { getConfig, createSupabaseClient, updateOrCreateChannel, host, formatError, findSavedKey } from './utils';
3
+ import { definitions } from './types_supabase';
6
4
 
7
- export const setChannel = async (appid, options) => {
8
- const { apikey, version, state, channel = 'dev' } = options;
9
- let config;
10
- let res;
11
- try {
12
- config = await loadConfig();
13
- } catch (err) {
14
- program.error("No capacitor config file found, run `cap init` first");
15
- }
5
+ interface Options {
6
+ apikey: string;
7
+ version: string;
8
+ state: string;
9
+ channel?: string;
10
+ }
11
+
12
+ export const setChannel = async (appid: string, options: Options) => {
13
+ let { version } = options;
14
+ const { state, channel = 'dev' } = options;
15
+ const apikey = options.apikey || findSavedKey()
16
+ const config = await getConfig();
16
17
  appid = appid || config?.app?.appId
18
+ version = version || config?.app?.package?.version
17
19
  let parsedState
18
20
  if (state === 'public' || state === 'private')
19
21
  parsedState = state === 'public'
@@ -32,25 +34,50 @@ export const setChannel = async (appid, options) => {
32
34
  console.log(`Set${channel} to @${state} in ${appid}`);
33
35
  }
34
36
  try {
35
- res = await axios({
36
- method: 'POST',
37
- url: `${host}/api/channel`,
38
- data: {
39
- version,
40
- public: parsedState,
41
- appid,
42
- channel,
43
- },
44
- validateStatus: () => true,
45
- headers: {
46
- 'authorization': apikey
47
- }
48
- })
37
+ const supabase = createSupabaseClient(apikey)
38
+ const { data: apiAccess, error: apiAccessError } = await supabase
39
+ .rpc('is_allowed_capgkey', { apikey, keymode: ['write', 'all'], app_id: appid })
40
+
41
+ if (!apiAccess || apiAccessError) {
42
+ program.error("Invalid API key or insufisant rights");
43
+ }
44
+ const { data: dataUser, error: userIdError } = await supabase
45
+ .rpc<string>('get_user_id', { apikey })
46
+
47
+ const userId = dataUser ? dataUser.toString() : '';
48
+
49
+ if (!userId || userIdError) {
50
+ program.error('Cannot verify user')
51
+ }
52
+ const channelPayload: Partial<definitions['channels']> = {
53
+ created_by: userId,
54
+ app_id: appid,
55
+ name: channel,
56
+ }
57
+ if (version) {
58
+ const { data, error: vError } = await supabase
59
+ .from<definitions['app_versions']>('app_versions')
60
+ .select()
61
+ .eq('app_id', appid)
62
+ .eq('name', version)
63
+ .eq('user_id', userId)
64
+ .eq('deleted', false)
65
+ if (vError || !data || !data.length)
66
+ program.error(`Cannot find version ${version}`);
67
+ channelPayload.version = data[0].id
68
+ }
69
+ if (parsedState !== undefined)
70
+ channelPayload.public = parsedState
71
+ try {
72
+ const { error: dbError } = await updateOrCreateChannel(supabase, channelPayload, apikey)
73
+ if (dbError)
74
+ program.error(`Cannot set channel ${formatError(dbError)}`);
75
+ }
76
+ catch (e) {
77
+ program.error(`Cannot set channel ${formatError(e)}`);
78
+ }
49
79
  } catch (err) {
50
- program.error(`Network Error \n${prettyjson.render(err.response.data)}`);
51
- }
52
- if (!res || res.status !== 200) {
53
- program.error(`Server Error \n${prettyjson.render(res.data)}`);
80
+ program.error(`Unknow error ${formatError(err)}`);
54
81
  }
55
82
  if (version) {
56
83
  console.log(`Done ✅`);
@@ -0,0 +1,6 @@
1
+ // CapacitorConfig
2
+
3
+ declare module '@capacitor/cli/dist/config' {
4
+ export function loadConfig(): CapacitorConfig;
5
+ };
6
+