-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathcli.js
executable file
·133 lines (114 loc) · 2.97 KB
/
cli.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
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
#! /usr/bin/env node
/* eslint consistent-return:0 */
import * as webPush from '../src/index.js';
import minimist from 'minimist';
const printUsageDetails = () => {
const actions = [
{
name: 'send-notification',
options: [
'--endpoint=<url>',
'[--key=<browser key>]',
'[--auth=<auth secret>]',
'[--payload=<message>]',
'[--ttl=<seconds>]',
'[--encoding=<encoding type>]',
'[--vapid-subject=<vapid subject>]',
'[--vapid-pubkey=<public key url base64>]',
'[--vapid-pvtkey=<private key url base64>]',
'[--proxy=<http proxy uri, e.g: http://127.0.0.1:8889>]',
'[--gcm-api-key=<api key>]'
]
}, {
name: 'generate-vapid-keys',
options: [
'[--json]'
]
}
];
let usage = '\nUsage: \n\n';
actions.forEach(action => {
usage += ' web-push ' + action.name;
usage += ' ' + action.options.join(' ');
usage += '\n\n';
});
console.log(usage);
process.exit(1);
};
const generateVapidKeys = returnJson => {
const vapidKeys = webPush.generateVAPIDKeys();
let outputText;
if (returnJson) {
outputText = JSON.stringify(vapidKeys);
} else {
const outputLine = '\n=======================================\n';
outputText = outputLine + '\n'
+ 'Public Key:\n' + vapidKeys.publicKey + '\n\n'
+ 'Private Key:\n' + vapidKeys.privateKey + '\n'
+ outputLine;
}
console.log(outputText);
process.exit(0);
};
const sendNotification = args => {
if (process.env.GCM_API_KEY) {
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
}
const subscription = {
endpoint: args.endpoint,
keys: {
p256dh: args.key || null,
auth: args.auth || null
}
};
const payload = args.payload || null;
const options = {};
if (args.ttl) {
options.TTL = args.ttl;
}
if (args['vapid-subject'] || args['vapid-pubkey'] || args['vapid-pvtkey']) {
options.vapidDetails = {
subject: args['vapid-subject'] || null,
publicKey: args['vapid-pubkey'] || null,
privateKey: args['vapid-pvtkey'] || null
};
}
if (args.proxy) {
options.proxy = args.proxy;
}
if (args['gcm-api-key']) {
options.gcmAPIKey = args['gcm-api-key'];
}
if (args.encoding) {
options.contentEncoding = args.encoding;
}
webPush.sendNotification(subscription, payload, options)
.then(() => {
console.log('Push message sent.');
}, err => {
console.log('Error sending push message: ');
console.log(err);
})
.then(() => {
process.exit(0);
});
};
const executeCliAction = () => {
const action = process.argv[2];
const argv = minimist(process.argv.slice(3));
switch (action) {
case 'send-notification':
if (!argv.endpoint) {
return printUsageDetails();
}
sendNotification(argv);
break;
case 'generate-vapid-keys':
generateVapidKeys(argv.json || false);
break;
default:
printUsageDetails();
break;
}
};
executeCliAction();