open-url.js 819 B

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