-
Notifications
You must be signed in to change notification settings - Fork 52
/
cli.js
executable file
·96 lines (87 loc) · 2.7 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
var gravatar = require('./lib/gravatar')
var emailValidator = require("email-validator")
var mainOpts = {
size: {
alias: 's',
describe: 'size of the requested image. (1-2048)'
},
protocol: {
alias: 'p',
describe: 'specifies the protocol of the url',
choices: ['http', 'https']
},
default: {
alias: 'd',
describe: 'default image when no profile image found. [choices: "404", "mm", "identicon", "monsterid", "wavatar", "retro", "blank", "an image url"]'
}
};
var setUsage = function(yargs){
return yargs
.options(mainOpts)
.help('h')
.alias('h', 'help')
.alias('v', 'version')
.version()
.describe('v', 'show version information')
.epilogue('Useful Links:' +
'\n- https://en.gravatar.com/site/implement/images/' +
'\n- https://en.gravatar.com/site/implement/profiles/'+
'\n- https://github.com/emerleite/node-gravatar'
)
};
var yargs = setUsage(require('yargs'))
.usage('Usage: $0 command somebody@example.com [options]')
.command('avatar', 'avatar somebody@example.com [options]', mainOpts)
.command('profile', 'profile somebody@example.com [options]', {
format: {
alias: 'f',
describe: 'format of the requested profile url',
choices: ['json', 'xml', 'qr', 'php', 'vcf']
},
callback: {
alias: 'c',
describe: 'name of a callback function when using json profile url eg. doSomething'
}
})
.example('$0 somebody@example.com')
.example('$0 avatar somebody@example.com')
.example('$0 profile somebody@example.com')
var argv = yargs.argv;
var command = argv._[0];
var email = argv._[1];
var getOptions = function(argv){
var options = {};
options.default = argv.d || argv.default;
options.size = argv.s || argv.size;
options.protocol = argv.p || argv.protocol
options.format = argv.f || argv.format
options.callback = argv.c || argv.callback
// prune options
for (var prop in options) {
if(options.hasOwnProperty(prop) && !options[prop]){
delete options[prop];
}
}
return options;
};
function printAvatarUrl(email, options){
console.log('Gravatar (avatar):')
return gravatar.url(email, options) + '\n'
}
var exec = function(argv){
var options = getOptions(argv);
if(command === 'profile'){
console.log('Gravatar (profile):')
return gravatar.profile_url(email, options) + '\n'
}else if(command === 'avatar' && emailValidator.validate(email)){
return printAvatarUrl(email, options)
}else if(emailValidator.validate(command)){
return printAvatarUrl(command, options)
}else{
yargs.showHelp();
return '';
}
}
process.stdout.write( exec(argv) )
process.exit()