11cb0ef41Sopenharmony_ciconst readline = require('readline')
21cb0ef41Sopenharmony_ciconst open = require('./open-url.js')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cifunction print (npm, title, url) {
51cb0ef41Sopenharmony_ci  const json = npm.config.get('json')
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci  const message = json ? JSON.stringify({ title, url }) : `${title}:\n${url}`
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci  npm.output(message)
101cb0ef41Sopenharmony_ci}
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Prompt to open URL in browser if possible
131cb0ef41Sopenharmony_ciconst promptOpen = async (npm, url, title, prompt, emitter) => {
141cb0ef41Sopenharmony_ci  const browser = npm.config.get('browser')
151cb0ef41Sopenharmony_ci  const isInteractive = process.stdin.isTTY === true && process.stdout.isTTY === true
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  try {
181cb0ef41Sopenharmony_ci    if (!/^https?:$/.test(new URL(url).protocol)) {
191cb0ef41Sopenharmony_ci      throw new Error()
201cb0ef41Sopenharmony_ci    }
211cb0ef41Sopenharmony_ci  } catch (_) {
221cb0ef41Sopenharmony_ci    throw new Error('Invalid URL: ' + url)
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  print(npm, title, url)
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  if (browser === false || !isInteractive) {
281cb0ef41Sopenharmony_ci    return
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  const rl = readline.createInterface({
321cb0ef41Sopenharmony_ci    input: process.stdin,
331cb0ef41Sopenharmony_ci    output: process.stdout,
341cb0ef41Sopenharmony_ci  })
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  const tryOpen = await new Promise(resolve => {
371cb0ef41Sopenharmony_ci    rl.on('SIGINT', () => {
381cb0ef41Sopenharmony_ci      rl.close()
391cb0ef41Sopenharmony_ci      resolve('SIGINT')
401cb0ef41Sopenharmony_ci    })
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    rl.question(prompt, () => {
431cb0ef41Sopenharmony_ci      resolve(true)
441cb0ef41Sopenharmony_ci    })
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    if (emitter && emitter.addListener) {
471cb0ef41Sopenharmony_ci      emitter.addListener('abort', () => {
481cb0ef41Sopenharmony_ci        rl.close()
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci        // clear the prompt line
511cb0ef41Sopenharmony_ci        npm.output('')
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci        resolve(false)
541cb0ef41Sopenharmony_ci      })
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  })
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  if (tryOpen === 'SIGINT') {
591cb0ef41Sopenharmony_ci    throw new Error('canceled')
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  if (!tryOpen) {
631cb0ef41Sopenharmony_ci    return
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  await open(npm, url, 'Browser unavailable.  Please open the URL manually')
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cimodule.exports = promptOpen
70