|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// MODULES // |
| 5 | + |
| 6 | +var fs = require( 'fs' ); |
| 7 | +var path = require( 'path' ); |
| 8 | +var parseArgs = require( 'minimist' ); |
| 9 | +var notifier = require( 'update-notifier' ); |
| 10 | +var pkg = require( './../package.json' ); |
| 11 | +var opts = require( './opts.json' ); |
| 12 | +var detect = require( './../lib' ); |
| 13 | + |
| 14 | + |
| 15 | +// FUNCTIONS // |
| 16 | + |
| 17 | +/** |
| 18 | +* Performs initialization tasks. |
| 19 | +* |
| 20 | +* @private |
| 21 | +* @example |
| 22 | +* init(); |
| 23 | +*/ |
| 24 | +function init() { |
| 25 | + var opts; |
| 26 | + |
| 27 | + // Check if newer versions exist for this package: |
| 28 | + opts = { |
| 29 | + 'pkg': pkg |
| 30 | + }; |
| 31 | + notifier( opts ).notify(); |
| 32 | + |
| 33 | + // Set the process title to allow the process to be more easily identified: |
| 34 | + process.title = pkg.name; |
| 35 | + process.stdout.on( 'error', process.exit ); |
| 36 | +} // end FUNCTION init() |
| 37 | + |
| 38 | +/** |
| 39 | +* Prints usage information. |
| 40 | +* |
| 41 | +* @private |
| 42 | +* @example |
| 43 | +* help(); |
| 44 | +* // => '...' |
| 45 | +*/ |
| 46 | +function help() { |
| 47 | + var fpath = path.join( __dirname, 'usage.txt' ); |
| 48 | + fs.createReadStream( fpath ) |
| 49 | + .pipe( process.stdout ) |
| 50 | + .on( 'close', onClose ); |
| 51 | + |
| 52 | + function onClose() { |
| 53 | + process.exit( 0 ); |
| 54 | + } |
| 55 | +} // end FUNCTION help() |
| 56 | + |
| 57 | +/** |
| 58 | +* Prints the package version. |
| 59 | +* |
| 60 | +* @private |
| 61 | +* @example |
| 62 | +* version(); |
| 63 | +* // => '#.#.#' |
| 64 | +*/ |
| 65 | +function version() { |
| 66 | + var msg = pkg.version.toString()+'\n'; |
| 67 | + process.stdout.write( msg, 'utf8' ); |
| 68 | + process.exit( 0 ); |
| 69 | +} // end FUNCTION version() |
| 70 | + |
| 71 | + |
| 72 | +// VARIABLES // |
| 73 | + |
| 74 | +var args; |
| 75 | + |
| 76 | + |
| 77 | +// MAIN // |
| 78 | + |
| 79 | +init(); |
| 80 | + |
| 81 | +// Parse command-line arguments: |
| 82 | +args = parseArgs( process.argv.slice( 2 ), opts ); |
| 83 | + |
| 84 | +if ( args.help ) { |
| 85 | + return help(); |
| 86 | +} |
| 87 | +if ( args.version ) { |
| 88 | + return version(); |
| 89 | +} |
| 90 | +console.log( detect() ); |
0 commit comments