diff options
Diffstat (limited to 'lib/utils/open-url.js')
-rw-r--r-- | lib/utils/open-url.js | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/lib/utils/open-url.js b/lib/utils/open-url.js index 28c2d038a..1fe456bd0 100644 --- a/lib/utils/open-url.js +++ b/lib/utils/open-url.js @@ -1,19 +1,12 @@ -const npm = require('../npm.js') const output = require('./output.js') const opener = require('opener') const { URL } = require('url') -const isUrlValid = url => { - try { - return /^(https?|file):$/.test(new URL(url).protocol) - } catch (_) { - return false - } -} - // attempt to open URL in web-browser, print address otherwise: -module.exports = function open (url, errMsg, cb, browser = npm.config.get('browser')) { +const open = async (npm, url, errMsg) => { + const browser = npm.config.get('browser') + function printAlternateMsg () { const json = npm.config.get('json') const alternateMsg = json @@ -28,18 +21,28 @@ module.exports = function open (url, errMsg, cb, browser = npm.config.get('brows if (browser === false) { printAlternateMsg() - return cb() + return } - if (!isUrlValid(url)) - return cb(new Error('Invalid URL: ' + url)) + try { + if (!/^(https?|file):$/.test(new URL(url).protocol)) + throw new Error() + } catch (_) { + throw new Error('Invalid URL: ' + url) + } const command = browser === true ? null : browser - opener(url, { command }, (er) => { - if (er && er.code === 'ENOENT') { - printAlternateMsg() - return cb() - } else - return cb(er) + await new Promise((resolve, reject) => { + opener(url, { command }, (err) => { + if (err) { + if (err.code === 'ENOENT') + printAlternateMsg() + else + return reject(err) + } + return resolve() + }) }) } + +module.exports = open |