'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));
};

@@ -873,157 +873,8 @@
 e')%0A
-    .option('link', %7B%0A        alias: 'l',%0A        describe: 'Choose link',%0A        choices: %5B'auto', 'npm', 'npms'%5D,%0A        default: 'auto',%0A    %7D)%0A
     
@@ -887,16 +887,16 @@
 d(1, 1)%0A
+
     .opt
@@ -985,16 +985,16 @@
 ource',%0A
-
         
@@ -1013,16 +1013,62 @@
 tring',%0A
+            choices: %5B'auto', 'npm', 'npms'%5D,%0A
         

Fix duplicate option.