package-url-cmd.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const pacote = require('pacote')
  2. const { openUrl } = require('./utils/open-url.js')
  3. const { log } = require('proc-log')
  4. const BaseCommand = require('./base-cmd.js')
  5. // Base command for opening urls from a package manifest (bugs, docs, repo)
  6. class PackageUrlCommand extends BaseCommand {
  7. static params = [
  8. 'browser',
  9. 'registry',
  10. 'workspace',
  11. 'workspaces',
  12. 'include-workspace-root',
  13. ]
  14. static workspaces = true
  15. static ignoreImplicitWorkspace = false
  16. static usage = ['[<pkgname> [<pkgname> ...]]']
  17. async exec (args) {
  18. if (!args || !args.length) {
  19. args = ['.']
  20. }
  21. for (const arg of args) {
  22. // XXX It is very odd that `where` is how pacote knows to look anywhere other than the cwd.
  23. const opts = {
  24. ...this.npm.flatOptions,
  25. where: this.npm.localPrefix,
  26. fullMetadata: true,
  27. _isRoot: true,
  28. }
  29. const mani = await pacote.manifest(arg, opts)
  30. const url = this.getUrl(arg, mani)
  31. log.silly(this.name, 'url', url)
  32. await openUrl(this.npm, url, `${mani.name} ${this.name} available at the following URL`)
  33. }
  34. }
  35. async execWorkspaces (args) {
  36. if (args && args.length) {
  37. return this.exec(args)
  38. }
  39. await this.setWorkspaces()
  40. return this.exec(this.workspacePaths)
  41. }
  42. // given a manifest, try to get the hosted git info from it based on repository (if a string) or repository.url (if an object)
  43. // returns null if it's not a valid repo, or not a known hosted repo
  44. hostedFromMani (mani) {
  45. const hostedGitInfo = require('hosted-git-info')
  46. const r = mani.repository
  47. const rurl = !r ? null
  48. : typeof r === 'string' ? r
  49. : typeof r === 'object' && typeof r.url === 'string' ? r.url
  50. : null
  51. // hgi returns undefined sometimes, but let's always return null here
  52. return (rurl && hostedGitInfo.fromUrl(rurl.replace(/^git\+/, ''))) || null
  53. }
  54. }
  55. module.exports = PackageUrlCommand