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

feat: show channel in --available table #840

Merged
merged 3 commits into from
Jun 12, 2024
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
73 changes: 61 additions & 12 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import select from '@inquirer/select'
import {Args, Command, Flags, ux} from '@oclif/core'
import {Args, Command, Flags, Interfaces, ux} from '@oclif/core'
import {got} from 'got'
import {basename} from 'node:path'
import {sort} from 'semver'
import TtyTable from 'tty-table'
Expand Down Expand Up @@ -37,9 +38,11 @@ export default class UpdateCommand extends Command {
available: Flags.boolean({
char: 'a',
description: 'See available versions.',
exclusive: ['version', 'interactive'],
}),
force: Flags.boolean({
description: 'Force a re-download of the requested version.',
exclusive: ['interactive', 'available'],
}),
interactive: Flags.boolean({
char: 'i',
Expand All @@ -49,26 +52,36 @@ export default class UpdateCommand extends Command {
version: Flags.string({
char: 'v',
description: 'Install a specific version.',
exclusive: ['interactive'],
exclusive: ['interactive', 'force'],
}),
}

async run(): Promise<void> {
const {args, flags} = await this.parse(UpdateCommand)
const updater = new Updater(this.config)
if (flags.available) {
const [index, localVersions] = await Promise.all([updater.fetchVersionIndex(), updater.findLocalVersions()])
const {distTags, index, localVersions} = await lookupVersions(updater, this.config)

const headers = [
{align: 'left', value: 'Location'},
{align: 'left', value: 'Version'},
]

if (distTags) {
headers.push({align: 'left', value: 'Channel'})
}

// eslint-disable-next-line new-cap
const t = TtyTable(
[
{align: 'left', value: 'Location'},
{align: 'left', value: 'Version'},
],
headers,
sort(Object.keys(index))
.reverse()
.map((version) => {
const location = localVersions.find((l) => basename(l).startsWith(version)) || index[version]
if (distTags) {
return [location, version, distTags[version] ?? '']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

distTags[version]

So in order for this to work, then one must create an NPM tag together with publish to S3?

A bit curious, if the requirement to keep two sources of truth as opposed to just S3. Or S3 doesn't have this kind of information?

Sorry if the question is trivial 🙏 Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it assumes that you're publishing to both npm and S3. But this assumption has been true for a long time here and can be turned off by setting disableNpmLookup to true in oclif.update in your package.json

Really the only way to get the information from S3 is to know ahead of time which channels you want (e.g. I want to show stable, stable-rc, and nightly). But that's hard to do in a plugin like this which is meant to be used by a wide variety of CLIs. For instance one CLI might make heavy use of nightly channel whereas another CLI might use a beta channel. Of course, this could be configured at the CLI level but I figured that since we were already assuming elsewhere that the CLI is published both to npm and S3 we could continue making that assumption.

}

return [location, version]
}),
{compact: true},
Expand All @@ -86,16 +99,52 @@ export default class UpdateCommand extends Command {
autoUpdate: flags.autoupdate,
channel: args.channel,
force: flags.force,
version: flags.interactive ? await promptForVersion(updater) : flags.version,
version: flags.interactive ? await promptForVersion(updater, this.config) : flags.version,
})
}
}

const promptForVersion = async (updater: Updater): Promise<string> =>
select({
choices: sort(Object.keys(await updater.fetchVersionIndex()))
const lookupVersions = async (updater: Updater, config: Interfaces.Config) => {
ux.action.start('Looking up versions')
const [index, localVersions, distTags] = await Promise.all([
updater.fetchVersionIndex(),
updater.findLocalVersions(),
fetchDistTags(config),
])

ux.action.stop(`Found ${Object.keys(index).length} versions`)
return {
distTags,
index,
localVersions,
}
}

const fetchDistTags = async (config: Interfaces.Config) => {
const distTags = config.pjson.oclif.update?.disableNpmLookup
? {}
: await got
.get(`${config.npmRegistry ?? 'https://registry.npmjs.org'}/${config.pjson.name}`)
.json<{
'dist-tags': Record<string, string>
}>()
.then((r) => r['dist-tags'])

// Invert the distTags object so we can look up the channel by version
return Object.fromEntries(Object.entries(distTags ?? {}).map(([k, v]) => [v, k]))
}

const displayName = (value: string, distTags: Record<string, string>) =>
`${value} ${distTags[value] ? `(${distTags[value]})` : ''}`

const promptForVersion = async (updater: Updater, config: Interfaces.Config): Promise<string> => {
const {distTags, index} = await lookupVersions(updater, config)
return select({
choices: sort(Object.keys(index))
.reverse()
.map((v) => ({value: v})),
.map((v) => ({name: displayName(v, distTags), value: v})),
loop: false,
message: 'Select a version to update to',
pageSize: 10,
})
}
2 changes: 1 addition & 1 deletion src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class Updater {
}

public async fetchVersionIndex(): Promise<VersionIndex> {
ux.action.status = 'fetching version index'
const newIndexUrl = this.config.s3Url(s3VersionIndexKey(this.config))
try {
const {body} = await got.get<VersionIndex>(newIndexUrl)
Expand Down Expand Up @@ -83,6 +82,7 @@ export class Updater {
if (localVersion) {
await this.updateToExistingVersion(current, localVersion)
} else {
ux.action.status = 'fetching version index'
const index = await this.fetchVersionIndex()
const url = index[version]
if (!url) {
Expand Down