• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const npm = require('../npm.js')
3const output = require('./output.js')
4const opener = require('opener')
5
6// attempt to open URL in web-browser, print address otherwise:
7module.exports = function open (url, errMsg, cb, browser = npm.config.get('browser')) {
8  function printAlternateMsg () {
9    const json = npm.config.get('json')
10    const alternateMsg = json
11      ? JSON.stringify({
12        title: errMsg,
13        url
14      }, null, 2)
15      : `${errMsg}:\n\n${url}`
16
17    output(alternateMsg)
18  }
19
20  const skipBrowser = process.argv.indexOf('--no-browser') > -1
21
22  if (skipBrowser) {
23    printAlternateMsg()
24    return cb()
25  }
26
27  opener(url, { command: browser }, (er) => {
28    if (er && er.code === 'ENOENT') {
29      printAlternateMsg()
30      return cb()
31    } else {
32      return cb(er)
33    }
34  })
35}
36