Skip to content

Commit a9c198f

Browse files
author
Matt Gaunt
committed
Updating to new actions for CLI
1 parent 27a114f commit a9c198f

File tree

1 file changed

+92
-52
lines changed

1 file changed

+92
-52
lines changed

bin/web-push.js

100644100755
Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,108 @@
11
#! /usr/bin/env node
22
const fs = require('fs');
3-
const webPush = require('web-push');
4-
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
3+
const webPush = require('../src/index.js');
54

6-
const argv = require('minimist')(process.argv.slice(2));
5+
const printUsageDetails = () => {
6+
const spc = ' ';
77

8-
const usage = 'Use: web-push --endpoint=<url> --key=<browser key> ' +
9-
'[--auth=<auth secret>] [--ttl=<seconds>] [--payload=<message>] ' +
10-
'[--vapid-audience] [--vapid-subject] [--vapid-pvtkey] [--vapid-pubkey]';
8+
const actions = [
9+
{
10+
name: 'send-notification',
11+
options: [
12+
'--endpoint=<url>',
13+
'[--key=<browser key>]',
14+
'[--auth=<auth secret>]',
15+
'[--payload=<message>]',
16+
'[--ttl=<seconds>]',
17+
'[--vapid-subject]',
18+
'[--vapid-pubkey]',
19+
'[--vapid-pvtkey]'
20+
]
21+
}, {
22+
name: 'generate-vapid-keys',
23+
options: [
24+
'[--json]'
25+
]
26+
}
27+
];
28+
29+
let usage = '\nUsage: \n\n';
30+
actions.forEach(action => {
31+
usage += ' web-push ' + action.name;
32+
usage += ' ' + action.options.join(' ');
33+
usage += '\n\n';
34+
});
1135

12-
if (!argv.endpoint || !argv.key) {
1336
console.log(usage);
1437
process.exit(1);
15-
}
38+
};
39+
40+
const generateVapidKeys = returnJson => {
41+
const vapidKeys = webPush.generateVAPIDKeys();
1642

17-
const endpoint = argv.endpoint;
18-
const key = argv.key;
19-
const ttl = argv.ttl || 0;
20-
const payload = argv.payload || '';
21-
const auth = argv.auth || null;
43+
let outputText;
44+
if (returnJson) {
45+
outputText = JSON.stringify(vapidKeys);
46+
} else {
47+
const outputLine = '\n=======================================\n'
48+
outputText = outputLine + '\n' +
49+
'Public Key:\n' + vapidKeys.publicKey + '\n\n' +
50+
'Private Key:\n' + vapidKeys.privateKey + '\n' +
51+
outputLine;
52+
}
2253

23-
const vapidAudience = argv['vapid-audience'] || null;
24-
const vapidSubject = argv['vapid-subject'] || null;
25-
const vapidPubKey = argv['vapid-pubkey'] || null;
26-
const vapidPvtKey = argv['vapid-pvtkey'] || null;
54+
console.log(outputText);
55+
process.exit(0);
56+
};
2757

28-
function getKeys() {
29-
if (vapidPubKey && vapidPvtKey) {
30-
const publicKey = fs.readFileSync(vapidPubKey);
31-
const privateKey = fs.readFileSync(vapidPvtKey);
58+
const sendNotification = args => {
59+
webPush.setGCMAPIKey(process.env.GCM_API_KEY);
3260

33-
if (publicKey && privateKey) {
34-
return {
35-
privateKey,
36-
publicKey
37-
};
61+
const subscription = {
62+
endpoint: argv.endpoint,
63+
keys: {
64+
p256dh: argv.key || null,
65+
auth: argv.auth || null
3866
}
39-
}
67+
};
4068

41-
return webPush.generateVAPIDKeys();
42-
}
69+
const payload = argv.payload || null;
4370

44-
let params = {
45-
TTL: ttl,
46-
payload,
47-
userPublicKey: key
48-
};
49-
if (vapidAudience && vapidSubject) {
50-
const vapidKeys = getKeys();
51-
const vapid = {
52-
audience: vapidAudience,
53-
subject: `mailto:${vapidSubject}`,
54-
privateKey: vapidKeys.privateKey,
55-
publicKey: vapidKeys.publicKey
71+
const options = {
72+
TTL: argv.ttl || 0,
73+
vapid: {
74+
subject: argv['vapid-subject'] || null,
75+
publicKey: argv['vapid-pubkey'] || null,
76+
privateKey: argv['vapid-pvtkey'] || null
77+
}
5678
};
57-
params.vapid = vapid;
58-
}
59-
if (auth) {
60-
params.userAuth = auth;
79+
80+
webPush.sendNotification(subscription, payload, options)
81+
.then(() => {
82+
console.log('Push message sent.');
83+
}, err => {
84+
console.log('Error sending push message: ');
85+
console.log(err);
86+
})
87+
.then(() => {
88+
process.exit(0);
89+
});
90+
};
91+
92+
const action = process.argv[2];
93+
const argv = require('minimist')(process.argv.slice(3));
94+
switch (action) {
95+
case 'send-notification':
96+
if (!argv.endpoint || !argv.key) {
97+
return printUsageDetails();
98+
}
99+
100+
sendNotification(argv);
101+
break;
102+
case 'generate-vapid-keys':
103+
generateVapidKeys(argv.json || false);
104+
break;
105+
default:
106+
printUsageDetails();
107+
break;
61108
}
62-
webPush.sendNotification(endpoint, params).then(() => {
63-
console.log('Push message sent.');
64-
}, (err) => {
65-
console.log('Error sending push message: ', err);
66-
}).then(() => {
67-
process.exit(0);
68-
});

0 commit comments

Comments
 (0)