1
0

cli.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #! /usr/bin/env node
  2. /* eslint consistent-return:0 */
  3. 'use strict';
  4. const webPush = require('../src/index.js');
  5. const printUsageDetails = () => {
  6. const actions = [
  7. {
  8. name: 'send-notification',
  9. options: [
  10. '--endpoint=<url>',
  11. '[--key=<browser key>]',
  12. '[--auth=<auth secret>]',
  13. '[--payload=<message>]',
  14. '[--ttl=<seconds>]',
  15. '[--encoding=<encoding type>]',
  16. '[--vapid-subject=<vapid subject>]',
  17. '[--vapid-pubkey=<public key url base64>]',
  18. '[--vapid-pvtkey=<private key url base64>]',
  19. '[--proxy=<http proxy uri, e.g: http://127.0.0.1:8889>]',
  20. '[--gcm-api-key=<api key>]'
  21. ]
  22. }, {
  23. name: 'generate-vapid-keys',
  24. options: [
  25. '[--json]'
  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. });
  35. console.log(usage);
  36. process.exit(1);
  37. };
  38. const generateVapidKeys = returnJson => {
  39. const vapidKeys = webPush.generateVAPIDKeys();
  40. let outputText;
  41. if (returnJson) {
  42. outputText = JSON.stringify(vapidKeys);
  43. } else {
  44. const outputLine = '\n=======================================\n';
  45. outputText = outputLine + '\n'
  46. + 'Public Key:\n' + vapidKeys.publicKey + '\n\n'
  47. + 'Private Key:\n' + vapidKeys.privateKey + '\n'
  48. + outputLine;
  49. }
  50. console.log(outputText);
  51. process.exit(0);
  52. };
  53. const sendNotification = args => {
  54. if (process.env.GCM_API_KEY) {
  55. webPush.setGCMAPIKey(process.env.GCM_API_KEY);
  56. }
  57. const subscription = {
  58. endpoint: args.endpoint,
  59. keys: {
  60. p256dh: args.key || null,
  61. auth: args.auth || null
  62. }
  63. };
  64. const payload = args.payload || null;
  65. const options = {};
  66. if (args.ttl) {
  67. options.TTL = args.ttl;
  68. }
  69. if (argv['vapid-subject'] || argv['vapid-pubkey'] || argv['vapid-pvtkey']) {
  70. options.vapidDetails = {
  71. subject: args['vapid-subject'] || null,
  72. publicKey: args['vapid-pubkey'] || null,
  73. privateKey: args['vapid-pvtkey'] || null
  74. };
  75. }
  76. if (args.proxy) {
  77. options.proxy = args.proxy;
  78. }
  79. if (args['gcm-api-key']) {
  80. options.gcmAPIKey = args['gcm-api-key'];
  81. }
  82. if (args.encoding) {
  83. options.contentEncoding = args.encoding;
  84. }
  85. webPush.sendNotification(subscription, payload, options)
  86. .then(() => {
  87. console.log('Push message sent.');
  88. }, err => {
  89. console.log('Error sending push message: ');
  90. console.log(err);
  91. })
  92. .then(() => {
  93. process.exit(0);
  94. });
  95. };
  96. const action = process.argv[2];
  97. const argv = require('minimist')(process.argv.slice(3));
  98. switch (action) {
  99. case 'send-notification':
  100. if (!argv.endpoint) {
  101. return printUsageDetails();
  102. }
  103. sendNotification(argv);
  104. break;
  105. case 'generate-vapid-keys':
  106. generateVapidKeys(argv.json || false);
  107. break;
  108. default:
  109. printUsageDetails();
  110. break;
  111. }