11#!/usr/bin/env node
22
3+ 'use strict' ;
4+
35const gravatar = require ( './lib/gravatar' ) ;
46const emailValidator = require ( 'email-validator' ) ;
5- const path = require ( 'path ' ) ;
7+ const pkg = require ( './package.json ' ) ;
68
7- const mainOpts = {
9+ const options = {
810 size : {
911 alias : 's' ,
1012 describe : 'size of the requested image. (1-2048)'
@@ -16,80 +18,71 @@ const mainOpts = {
1618 } ,
1719 default : {
1820 alias : 'd' ,
19- describe : 'default image when no profile image found. [choices: "404", "mm", "identicon", "monsterid", "wavatar", "retro", "blank", "an image url"]'
21+ describe : `default image when no profile image found.
22+ [choices: "404", "mm", "identicon", "monsterid", "wavatar", "retro", "blank", "an image url"]`
23+ }
24+ } ;
25+
26+ const profileOptions = {
27+ format : {
28+ alias : 'f' ,
29+ describe : 'format of the requested profile url' ,
30+ choices : [ 'json' , 'xml' , 'qr' , 'php' , 'vcf' ]
31+ } ,
32+ callback : {
33+ alias : 'c' ,
34+ describe : 'name of a callback function when using json profile url eg. doSomething'
2035 }
2136} ;
37+
38+ const getOptions = argv => pick ( argv , [ ...Object . keys ( options ) , ...Object . keys ( profileOptions ) ] ) ;
39+
2240const setUsage = function ( yargs ) {
2341 return yargs
24- . options ( mainOpts )
42+ . options ( options )
2543 . help ( 'h' )
2644 . alias ( 'h' , 'help' )
2745 . alias ( 'v' , 'version' )
28- . version ( ( ) => path . join ( ' gravatar version: ' , require ( './package' ) . version ) )
46+ . version ( ` gravatar version: ${ pkg . version } ` )
2947 . describe ( 'v' , 'show version information' )
3048 . epilogue ( `Useful Links:
31- \n - https://en.gravatar.com/site/implement/images/
32- \n - https://en.gravatar.com/site/implement/profiles/
33- \n- https://github.com/emerleite/node-gravatar ` ) ;
49+ - https://en.gravatar.com/site/implement/images/
50+ - https://en.gravatar.com/site/implement/profiles/
51+ - ${ pkg . homepage } ` ) ;
3452} ;
3553
3654const yargs = setUsage ( require ( 'yargs' ) )
3755 . usage ( 'Usage: $0 command somebody@example.com [options]' )
38- . command ( 'avatar' , 'avatar somebody@example.com [options]' , mainOpts )
39- . command ( 'profile' , 'profile somebody@example.com [options]' , {
40- format : {
41- alias : 'f' ,
42- describe : 'format of the requested profile url' ,
43- choices : [ 'json' , 'xml' , 'qr' , 'php' , 'vcf' ]
44- } ,
45- callback : {
46- alias : 'c' ,
47- describe : 'name of a callback function when using json profile url eg. doSomething'
48- }
49- } )
56+ . command ( 'avatar' , 'avatar somebody@example.com [options]' , options )
57+ . command ( 'profile' , 'profile somebody@example.com [options]' , profileOptions )
5058 . example ( '$0 somebody@example.com' )
5159 . example ( '$0 avatar somebody@example.com' )
5260 . example ( '$0 profile somebody@example.com' ) ;
5361
54- const argv = yargs . argv ;
55- const command = argv . _ [ 0 ] ;
56- const email = argv . _ [ 1 ] ;
57-
58- const getOptions = function ( argv ) {
59- const options = { } ;
60- options . default = argv . d || argv . default ;
61- options . size = argv . s || argv . size ;
62- options . protocol = argv . p || argv . protocol ;
63- options . format = argv . f || argv . format ;
64- options . callback = argv . c || argv . callback ;
65-
66- // prune options
67- for ( const prop in options ) {
68- if ( options . hasOwnProperty ( prop ) && ! options [ prop ] ) {
69- delete options [ prop ] ;
70- }
71- }
72- return options ;
73- } ;
74-
7562function printAvatarUrl ( email , options ) {
76- console . log ( 'Gravatar (avatar):' ) ;
77- return `${ gravatar . url ( email , options ) } \n` ;
63+ console . log ( 'Gravatar (avatar):\n%s' , gravatar . url ( email , options ) ) ;
7864}
7965
8066function printAvatarProfile ( email , options ) {
81- console . log ( 'Gravatar (profile):' ) ;
82- return `${ gravatar . profile_url ( email , options ) } \n` ;
67+ console . log ( 'Gravatar (profile):\n%s' , gravatar . profileUrl ( email , options ) ) ;
8368}
8469
85- const exec = function ( argv ) {
70+ function exec ( argv ) {
71+ const [ command , email ] = argv . _ ;
8672 const options = getOptions ( argv ) ;
8773 if ( command === 'profile' ) return printAvatarProfile ( email , options ) ;
8874 if ( command === 'avatar' && emailValidator . validate ( email ) ) return printAvatarUrl ( email , options ) ;
8975 if ( emailValidator . validate ( command ) ) return printAvatarUrl ( command , options ) ;
9076 yargs . showHelp ( ) ;
9177 return '' ;
92- } ;
78+ }
9379
94- process . stdout . write ( exec ( argv ) ) ;
80+ process . stdout . write ( exec ( yargs . argv ) ) ;
9581process . exit ( ) ;
82+
83+ function pick ( obj , keys = [ ] ) {
84+ return keys . reduce ( ( acc , key ) => {
85+ if ( ! obj . hasOwnProperty ( key ) || ! obj [ key ] ) return acc ;
86+ return Object . assign ( acc , { [ key ] : obj [ key ] } ) ;
87+ } , { } ) ;
88+ }
0 commit comments