11cb0ef41Sopenharmony_ci// Base command for opening urls from a package manifest (bugs, docs, repo)
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst pacote = require('pacote')
41cb0ef41Sopenharmony_ciconst hostedGitInfo = require('hosted-git-info')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst openUrl = require('./utils/open-url.js')
71cb0ef41Sopenharmony_ciconst log = require('./utils/log-shim')
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst BaseCommand = require('./base-command.js')
101cb0ef41Sopenharmony_ciclass PackageUrlCommand extends BaseCommand {
111cb0ef41Sopenharmony_ci  static params = [
121cb0ef41Sopenharmony_ci    'browser',
131cb0ef41Sopenharmony_ci    'registry',
141cb0ef41Sopenharmony_ci    'workspace',
151cb0ef41Sopenharmony_ci    'workspaces',
161cb0ef41Sopenharmony_ci    'include-workspace-root',
171cb0ef41Sopenharmony_ci  ]
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  static workspaces = true
201cb0ef41Sopenharmony_ci  static ignoreImplicitWorkspace = false
211cb0ef41Sopenharmony_ci  static usage = ['[<pkgname> [<pkgname> ...]]']
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  async exec (args) {
241cb0ef41Sopenharmony_ci    if (!args || !args.length) {
251cb0ef41Sopenharmony_ci      args = ['.']
261cb0ef41Sopenharmony_ci    }
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    for (const arg of args) {
291cb0ef41Sopenharmony_ci      // XXX It is very odd that `where` is how pacote knows to look anywhere
301cb0ef41Sopenharmony_ci      // other than the cwd.
311cb0ef41Sopenharmony_ci      const opts = {
321cb0ef41Sopenharmony_ci        ...this.npm.flatOptions,
331cb0ef41Sopenharmony_ci        where: this.npm.localPrefix,
341cb0ef41Sopenharmony_ci        fullMetadata: true,
351cb0ef41Sopenharmony_ci      }
361cb0ef41Sopenharmony_ci      const mani = await pacote.manifest(arg, opts)
371cb0ef41Sopenharmony_ci      const url = this.getUrl(arg, mani)
381cb0ef41Sopenharmony_ci      log.silly(this.name, 'url', url)
391cb0ef41Sopenharmony_ci      await openUrl(this.npm, url, `${mani.name} ${this.name} available at the following URL`)
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  async execWorkspaces (args) {
441cb0ef41Sopenharmony_ci    if (args && args.length) {
451cb0ef41Sopenharmony_ci      return this.exec(args)
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci    await this.setWorkspaces()
481cb0ef41Sopenharmony_ci    return this.exec(this.workspacePaths)
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  // given a manifest, try to get the hosted git info from it based on
521cb0ef41Sopenharmony_ci  // repository (if a string) or repository.url (if an object) returns null
531cb0ef41Sopenharmony_ci  // if it's not a valid repo, or not a known hosted repo
541cb0ef41Sopenharmony_ci  hostedFromMani (mani) {
551cb0ef41Sopenharmony_ci    const r = mani.repository
561cb0ef41Sopenharmony_ci    const rurl = !r ? null
571cb0ef41Sopenharmony_ci      : typeof r === 'string' ? r
581cb0ef41Sopenharmony_ci      : typeof r === 'object' && typeof r.url === 'string' ? r.url
591cb0ef41Sopenharmony_ci      : null
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    // hgi returns undefined sometimes, but let's always return null here
621cb0ef41Sopenharmony_ci    return (rurl && hostedGitInfo.fromUrl(rurl.replace(/^git\+/, ''))) || null
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_cimodule.exports = PackageUrlCommand
66