Skip to content

Commit

Permalink
fix: install should use npm to check registry
Browse files Browse the repository at this point in the history
Modify plugins.ts#hasPackage to use npm instead of https to check if a package exists on the registry.
#282
  • Loading branch information
peternhale committed Jun 23, 2021
1 parent 01139cd commit 170fc73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
32 changes: 17 additions & 15 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {CLIError} from '@oclif/errors'
import cli from 'cli-ux'
import * as fs from 'fs'
import * as fse from 'fs-extra'
import HTTP from 'http-call'
import loadJSON from 'load-json-file'
import * as path from 'path'
import * as semver from 'semver'
import {exec} from 'child_process'

import {uniq, uniqWith} from './util'
import Yarn from './yarn'
Expand Down Expand Up @@ -240,20 +240,22 @@ export default class Plugins {
}

private async npmHasPackage(name: string): Promise<boolean> {
try {
const http: typeof HTTP = require('http-call').HTTP
const url = `${this.npmRegistry}/${name.replace('/', '%2f')}`
await http.get(url)
return true
} catch (error) {
this.debug(error)

if (error.statusCode === 404) {
return false
}

throw error
}
return new Promise((resolve, reject) => {
exec(`npm show ${name} dist-tags`, {
encoding: 'utf-8',
maxBuffer: 2048 * 2048,
}, error => {
if (error) {
try {
return resolve(false)
} catch {
reject(new Error(`Could not run npm show for ${name}`))
}
} else {
return resolve(true)
}
})
})
}

private async savePJSON(pjson: Config.PJSON.User) {
Expand Down
5 changes: 1 addition & 4 deletions test/commands/plugins/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ describe('command', () => {
.it('installs and uninstalls jdxcode/oclif-debug')

test
.nock('https://registry.npmjs.org', api => api
.get('/@heroku-cli%2fplugin-stubbed')
.reply(503, ''))
.command(['plugins:install', 'stubbed'], {reset: true})
.catch(/HTTP Error 503/)
.catch(/1/)
.stdout()
.command(['plugins'], {reset: true})
.do(output => expect(output.stdout).to.equal('no plugins installed\n'))
Expand Down

0 comments on commit 170fc73

Please sign in to comment.