Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add npm tag lookup to determineChannel correctly #563

Merged
merged 5 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/package-lock.json
/tmp
/oclif-plugin-update-*.tgz
/.vscode
/.vscode
/.idea
27 changes: 0 additions & 27 deletions src/github.ts

This file was deleted.

24 changes: 16 additions & 8 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Updater {
return
}

const channel = options.channel || await this.determineChannel()
const channel = options.channel || await this.determineChannel(version)
const current = await this.determineCurrentVersion()

if (version) {
Expand Down Expand Up @@ -268,14 +268,22 @@ export class Updater {
return current === updated
}

private async determineChannel(): Promise<string> {
private async determineChannel(version?:string): Promise<string> {
const channelPath = path.join(this.config.dataDir, 'channel')
if (fs.existsSync(channelPath)) {
const channel = await fs.readFile(channelPath, 'utf8')
return String(channel).trim()
}

return this.config.channel || 'stable'
const channel = fs.existsSync(channelPath) ? (await fs.readFile(channelPath, 'utf8')).trim() : 'stable'

try {
const {body} = await HTTP.get<{'dist-tags':Record<string, string>}>(`${this.config.npmRegistry ?? 'https://registry.npmjs.org'}/${this.config.pjson.name}`)
const tags = body['dist-tags']
const tag = Object.keys(tags).find(v => tags[v] === version) ?? channel
// convert from npm style tag defaults to OCLIF style
if (tag === 'latest') return 'stable'
if (tag === 'latest-rc') return 'stable-rc'
return tag
} catch {
return channel
}
}

private determinePlatform(): Interfaces.PlatformTypes {
Expand Down Expand Up @@ -303,7 +311,7 @@ export class Updater {

private async setChannel(channel: string): Promise<void> {
const channelPath = path.join(this.config.dataDir, 'channel')
fs.writeFile(channelPath, channel, 'utf8')
await fs.writeFile(channelPath, channel, 'utf8')
}

private async logChop(): Promise<void> {
Expand Down
81 changes: 81 additions & 0 deletions test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as sinon from 'sinon'
import stripAnsi = require('strip-ansi')
import * as extract from '../src/tar'
import {expect} from 'chai'
import HTTP from 'http-call'

type OutputCollectors = {
stdout: string[];
Expand Down Expand Up @@ -153,6 +154,86 @@ describe('update plugin', () => {
expect(stdout).to.matches(/Updating CLI from 2.0.0 to 2.0.1/)
})

it('will get the correct channel and use default registry', async () => {
const request = sandbox.spy(HTTP, 'get')
const hash = 'f289627'
config.pjson.name = '@oclif/plugin-update'
clientRoot = setupClientRoot({config})
const platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
const versionManifestRegex = new RegExp(`example-cli-v2.0.1-${hash}-${config.platform}-${config.arch}-buildmanifest`)
const tarballRegex = new RegExp(`tarballs\\/example-cli\\/example-cli-v2.0.1\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`)
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)

sandbox.stub(extract, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

nock(/oclif-staging.s3.amazonaws.com/)
.get(platformRegex)
.reply(200, {version: '2.0.1'})
.get(manifestRegex)
.reply(200, {version: '2.0.1'})
.get(versionManifestRegex)
.reply(200, {version: '2.0.1'})
.get(tarballRegex)
.reply(200, gzContents, {
'X-Transfer-Length': String(gzContents.length),
'content-length': String(gzContents.length),
'Content-Encoding': 'gzip',
})
.get(indexRegex)
.reply(200, {
'2.0.1': `versions/example-cli/2.0.1/${hash}/example-cli-v2.0.1-${config.platform}-${config.arch}.gz`,
})

updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, version: '2.0.1'})
expect(request.callCount).to.equal(3)
expect(request.firstCall.args[0]).to.include('https://registry.npmjs.org/@oclif/plugin-update')
})
it('will get the correct channel and use a custom registry', async () => {
const request = sandbox.spy(HTTP, 'get')
const hash = 'f289627'
config.pjson.name = '@oclif/plugin-update'
config.npmRegistry = 'https://myCustomRegistry.com'
clientRoot = setupClientRoot({config})
const platformRegex = new RegExp(`tarballs\\/example-cli\\/${config.platform}-${config.arch}`)
const manifestRegex = new RegExp(`channels\\/stable\\/example-cli-${config.platform}-${config.arch}-buildmanifest`)
const versionManifestRegex = new RegExp(`example-cli-v2.0.1-${hash}-${config.platform}-${config.arch}-buildmanifest`)
const tarballRegex = new RegExp(`tarballs\\/example-cli\\/example-cli-v2.0.1\\/example-cli-v2.0.1-${config.platform}-${config.arch}gz`)
const indexRegex = new RegExp(`example-cli-${config.platform}-${config.arch}-tar-gz.json`)

sandbox.stub(extract, 'extract').resolves()
sandbox.stub(zlib, 'gzipSync').returns(Buffer.alloc(1, ' '))

const gzContents = zlib.gzipSync(' ')

nock(/oclif-staging.s3.amazonaws.com/)
.get(platformRegex)
.reply(200, {version: '2.0.1'})
.get(manifestRegex)
.reply(200, {version: '2.0.1'})
.get(versionManifestRegex)
.reply(200, {version: '2.0.1'})
.get(tarballRegex)
.reply(200, gzContents, {
'X-Transfer-Length': String(gzContents.length),
'content-length': String(gzContents.length),
'Content-Encoding': 'gzip',
})
.get(indexRegex)
.reply(200, {
'2.0.1': `versions/example-cli/2.0.1/${hash}/example-cli-v2.0.1-${config.platform}-${config.arch}.gz`,
})

updater = initUpdater(config)
await updater.runUpdate({autoUpdate: false, version: '2.0.1'})
expect(request.callCount).to.equal(3)
expect(request.firstCall.args[0]).to.include('https://myCustomRegistry.com/@oclif/plugin-update')
})

it('should not update - not updatable', async () => {
clientRoot = setupClientRoot({config})
// unset binPath
Expand Down