package-url-cmd.js 1.9 KB

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