This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
/
push-creds.js
77 lines (59 loc) · 2.35 KB
/
push-creds.js
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
/**
* @flow
*/
import { ApiV2, Exp, User } from 'xdl';
import log from '../log';
export default (program: any) => {
program
.command('push:android:upload [project-dir]')
.description('Uploads a Firebase Cloud Messaging key for Android push notifications.')
.option('--api-key [api-key]', 'Server API key for FCM.')
.asyncActionProjectDir(async (projectDir, options) => {
if (!options.apiKey || options.apiKey.length === 0) {
throw new Error('Must specify an API key to upload with --api-key.');
}
log('Reading project configuration...');
const {
args: { remotePackageName },
} = await Exp.getPublishInfoAsync(projectDir);
log('Logging in...');
let user = await User.getCurrentUserAsync();
let apiClient = ApiV2.clientForUser(user);
log("Setting API key on Expo's servers...");
await apiClient.putAsync(`credentials/push/android/${remotePackageName}`, {
fcmApiKey: options.apiKey,
});
log('All done!');
}, true);
program
.command('push:android:show [project-dir]')
.description('Print the value currently in use for FCM notifications for this project.')
.asyncActionProjectDir(async (projectDir, options) => {
const {
args: { remotePackageName },
} = await Exp.getPublishInfoAsync(projectDir);
let user = await User.getCurrentUserAsync();
let apiClient = ApiV2.clientForUser(user);
let result = await apiClient.getAsync(`credentials/push/android/${remotePackageName}`);
if (result.status === 'ok' && result.fcmApiKey) {
console.log(JSON.stringify(result));
} else {
throw new Error('Server returned an invalid result!');
}
}, true);
program
.command('push:android:clear [project-dir]')
.description('Deletes a previously uploaded FCM credential.')
.asyncActionProjectDir(async (projectDir, options) => {
log('Reading project configuration...');
const {
args: { remotePackageName },
} = await Exp.getPublishInfoAsync(projectDir);
log('Logging in...');
let user = await User.getCurrentUserAsync();
let apiClient = ApiV2.clientForUser(user);
log("Deleting API key from Expo's servers...");
await apiClient.deleteAsync(`credentials/push/android/${remotePackageName}`);
log('All done!');
}, true);
};