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