/
open.js
55 lines (48 loc) · 1.58 KB
/
open.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
'use strict';
const got = require('got');
const opn = require('opn');
const handleError = require('./util/handleError');
function getLink(argv, res) {
const links = res.body.collected.metadata.links;
if (argv.link === 'npms') {
return `https://npms.io/search?term=${argv.package}`;
}
if (argv.link === 'npm') {
return links.npm;
}
return links.repository || links.npm;
}
// --------------------------------------------------------
exports.command = 'open <package>';
exports.describe = 'Opens the package in your browser.';
exports.builder = (yargs) =>
yargs
.strict()
.usage('Usage: $0 open <package> [options]\n\nOpens the package in your browser..')
.example('$0 open gulp', 'Opens "gulp" package using auto source')
.example('$0 open gulp --link npms', 'Opens "gulp" package in `https://npms.io` service')
.option('link', {
alias: 'l',
describe: 'Choose link',
choices: ['auto', 'npm', 'npms'],
default: 'auto',
})
.demand(1, 1)
.options({
link: {
describe: 'Open <package> using supplied link source',
type: 'string',
default: 'auto',
},
api: {
describe: 'The API url',
default: 'https://api.npms.io/v2',
},
});
exports.handler = (argv) => {
got(`${argv.api}/package/${encodeURIComponent(argv.package)}`, { json: true })
.then((res) => getLink(argv, res))
.then((link) => opn(link, { wait: false }))
.then(() => { process.exitCode = 0; })
.catch((err) => handleError(err));
};