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