Skip to content

Commit 7412db1

Browse files
committed
Refactor cli
1 parent 44d558d commit 7412db1

3 files changed

Lines changed: 44 additions & 50 deletions

File tree

cli.js

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env node
22

3+
'use strict';
4+
35
const gravatar = require('./lib/gravatar');
46
const 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+
2240
const 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

3654
const 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-
7562
function 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

8066
function 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));
9581
process.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+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"type": "git",
1212
"url": "git://github.com/emerleite/node-gravatar.git"
1313
},
14+
"homepage": "https://github.com/emerleite/node-gravatar",
1415
"files": [
1516
"lib"
1617
],

test/gravatar.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('CLI', function () {
120120
it('accepts an email argument with options and writes gravatar URL to STDOUT', function (done) {
121121
nixt()
122122
.run('./cli.js zeke@sikelianos.com -p https -s 500 -d retro')
123-
.stdout('Gravatar (avatar):\nhttps://gravatar.com/avatar/8f344b1c4bdcfc28bd848e97e94c3523?default=retro&size=500')
123+
.stdout('Gravatar (avatar):\nhttps://gravatar.com/avatar/8f344b1c4bdcfc28bd848e97e94c3523?size=500&default=retro')
124124
.end(done);
125125
});
126126
it('outputs usage if -h arg is present', function (done) {
@@ -135,7 +135,7 @@ describe('CLI', function () {
135135
it('accepts an email argument with options and writes gravatar URL to STDOUT', function (done) {
136136
nixt()
137137
.run('./cli.js avatar zeke@sikelianos.com -p https -s 500 -d retro')
138-
.stdout('Gravatar (avatar):\nhttps://gravatar.com/avatar/8f344b1c4bdcfc28bd848e97e94c3523?default=retro&size=500')
138+
.stdout('Gravatar (avatar):\nhttps://gravatar.com/avatar/8f344b1c4bdcfc28bd848e97e94c3523?size=500&default=retro')
139139
.end(done);
140140
});
141141
});

0 commit comments

Comments
 (0)