cli.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #! /usr/bin/env node
  2. 'use strict';
  3. const pkcs7 = require('../dist/pkcs7.cjs.js');
  4. const fs = require('fs');
  5. const path = require('path');
  6. const userArgs = process.argv;
  7. if (userArgs.indexOf('-h') !== -1 || userArgs.indexOf('--help') !== -1) {
  8. // eslint-disable-next-line
  9. console.log('usage: pkcs7');
  10. // eslint-disable-next-line
  11. console.log('pkcs7 expects input on stdin and outputs to stdout');
  12. process.exit();
  13. }
  14. if (userArgs.indexOf('-v') !== -1 || userArgs.indexOf('--version') !== -1) {
  15. // eslint-disable-next-line
  16. console.log(JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'))).version);
  17. process.exit();
  18. }
  19. const data = [];
  20. process.stdin.on('readable', function() {
  21. const chunk = process.stdin.read();
  22. if (chunk !== null) {
  23. data.push(chunk);
  24. }
  25. });
  26. process.stdin.on('end', function() {
  27. const buffer = Buffer.concat(data);
  28. const bytes = new Uint8Array(buffer.length);
  29. let i = buffer.length;
  30. while (i--) {
  31. bytes[i] = buffer[i];
  32. }
  33. // output the padded input
  34. process.stdout.write(new Buffer(pkcs7.pad(bytes)));
  35. });