-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: consolidate bugs, docs, repo command logic
All three of these commands do the same thing: open a manifest and find a url inside to open it. The finding of that manifest was not very consistent across these three commands. Some work with workspaces while others don't. Some work correctly with `--prefix` while others don't. This PR consolidates these commands so that they all are consistent in how they find the manifest being referenced. The specifics of which url they open are still left to each command. The util that only these three commands were using was consolidated into their base class.
- Loading branch information
Showing
11 changed files
with
159 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,19 @@ | ||
const pacote = require('pacote') | ||
const openUrl = require('../utils/open-url.js') | ||
const hostedFromMani = require('../utils/hosted-git-info-from-manifest.js') | ||
const log = require('../utils/log-shim') | ||
const BaseCommand = require('../base-command.js') | ||
class Docs extends BaseCommand { | ||
const PackageUrlCmd = require('../package-url-cmd.js') | ||
class Docs extends PackageUrlCmd { | ||
static description = 'Open documentation for a package in a web browser' | ||
static name = 'docs' | ||
static params = [ | ||
'browser', | ||
'registry', | ||
'workspace', | ||
'workspaces', | ||
'include-workspace-root', | ||
] | ||
|
||
static usage = ['[<pkgname> [<pkgname> ...]]'] | ||
static ignoreImplicitWorkspace = false | ||
|
||
async exec (args) { | ||
if (!args || !args.length) { | ||
args = ['.'] | ||
} | ||
|
||
await Promise.all(args.map(pkg => this.getDocs(pkg))) | ||
} | ||
|
||
async execWorkspaces (args, filters) { | ||
await this.setWorkspaces(filters) | ||
return this.exec(this.workspacePaths) | ||
} | ||
|
||
async getDocs (pkg) { | ||
const opts = { ...this.npm.flatOptions, fullMetadata: true } | ||
const mani = await pacote.manifest(pkg, opts) | ||
const url = this.getDocsUrl(mani) | ||
log.silly('docs', 'url', url) | ||
await openUrl(this.npm, url, `${mani.name} docs available at the following URL`) | ||
} | ||
|
||
getDocsUrl (mani) { | ||
getUrl (spec, mani) { | ||
if (mani.homepage) { | ||
return mani.homepage | ||
} | ||
|
||
const info = hostedFromMani(mani) | ||
const info = this.hostedFromMani(mani) | ||
if (info) { | ||
return info.docs() | ||
} | ||
|
||
return 'https://www.npmjs.com/package/' + mani.name | ||
return `https://www.npmjs.com/package/${mani.name}` | ||
} | ||
} | ||
module.exports = Docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Base command for opening urls from a package manifest (bugs, docs, repo) | ||
|
||
const pacote = require('pacote') | ||
const hostedGitInfo = require('hosted-git-info') | ||
|
||
const openUrl = require('./utils/open-url.js') | ||
const log = require('./utils/log-shim') | ||
|
||
const BaseCommand = require('./base-command.js') | ||
class PackageUrlCommand extends BaseCommand { | ||
static ignoreImplicitWorkspace = false | ||
static params = [ | ||
'browser', | ||
'registry', | ||
'workspace', | ||
'workspaces', | ||
'include-workspace-root', | ||
] | ||
|
||
static usage = ['[<pkgname> [<pkgname> ...]]'] | ||
|
||
async exec (args) { | ||
if (!args || !args.length) { | ||
args = ['.'] | ||
} | ||
|
||
for (const arg of args) { | ||
// XXX It is very odd that `where` is how pacote knows to look anywhere | ||
// other than the cwd. | ||
const opts = { | ||
...this.npm.flatOptions, | ||
where: this.npm.localPrefix, | ||
fullMetadata: true, | ||
} | ||
const mani = await pacote.manifest(arg, opts) | ||
const url = this.getUrl(arg, mani) | ||
log.silly(this.name, 'url', url) | ||
await openUrl(this.npm, url, `${mani.name} ${this.name} available at the following URL`) | ||
} | ||
} | ||
|
||
async execWorkspaces (args, filters) { | ||
await this.setWorkspaces(filters) | ||
return this.exec(this.workspacePaths) | ||
} | ||
|
||
// given a manifest, try to get the hosted git info from it based on | ||
// repository (if a string) or repository.url (if an object) returns null | ||
// if it's not a valid repo, or not a known hosted repo | ||
hostedFromMani (mani) { | ||
const r = mani.repository | ||
const rurl = !r ? null | ||
: typeof r === 'string' ? r | ||
: typeof r === 'object' && typeof r.url === 'string' ? r.url | ||
: null | ||
|
||
// hgi returns undefined sometimes, but let's always return null here | ||
return (rurl && hostedGitInfo.fromUrl(rurl.replace(/^git\+/, ''))) || null | ||
} | ||
} | ||
module.exports = PackageUrlCommand |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.