qrcode-terminal.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env node
  2. /*!
  3. * Module dependencies.
  4. */
  5. var qrcode = require('../lib/main'),
  6. path = require('path'),
  7. fs = require('fs');
  8. /*!
  9. * Parse the process name
  10. */
  11. var name = process.argv[1].replace(/^.*[\\\/]/, '').replace('.js', '');
  12. /*!
  13. * Parse the input
  14. */
  15. if (process.stdin.isTTY) {
  16. // called with input as argument, e.g.:
  17. // ./qrcode-terminal.js "INPUT"
  18. var input = process.argv[2];
  19. handleInput(input);
  20. } else {
  21. // called with piped input, e.g.:
  22. // echo "INPUT" | ./qrcode-terminal.js
  23. var readline = require('readline');
  24. var interface = readline.createInterface({
  25. input: process.stdin,
  26. output: process.stdout,
  27. terminal: false
  28. });
  29. interface.on('line', function(line) {
  30. handleInput(line);
  31. });
  32. }
  33. /*!
  34. * Process the input
  35. */
  36. function handleInput(input) {
  37. /*!
  38. * Display help
  39. */
  40. if (!input || input === '-h' || input === '--help') {
  41. help();
  42. process.exit();
  43. }
  44. /*!
  45. * Display version
  46. */
  47. if (input === '-v' || input === '--version') {
  48. version();
  49. process.exit();
  50. }
  51. /*!
  52. * Render the QR Code
  53. */
  54. qrcode.generate(input);
  55. }
  56. /*!
  57. * Helper functions
  58. */
  59. function help() {
  60. console.log([
  61. '',
  62. 'Usage: ' + name + ' <message>',
  63. '',
  64. 'Options:',
  65. ' -h, --help output usage information',
  66. ' -v, --version output version number',
  67. '',
  68. 'Examples:',
  69. '',
  70. ' $ ' + name + ' hello',
  71. ' $ ' + name + ' "hello world"',
  72. ''
  73. ].join('\n'));
  74. }
  75. function version() {
  76. var packagePath = path.join(__dirname, '..', 'package.json'),
  77. packageJSON = JSON.parse(fs.readFileSync(packagePath), 'utf8');
  78. console.log(packageJSON.version);
  79. }