Skip to content

Commit

Permalink
feat: add --interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Feb 1, 2022
1 parent c8e1045 commit e44820c
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 289 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"bugs": "https://github.com/oclif/plugin-update/issues",
"dependencies": {
"@oclif/color": "^1.0.0",
"@oclif/core": "^1.2.0",
"@types/semver": "^7.3.4",
"cross-spawn": "^7.0.3",
"@oclif/core": "^1.3.0",
"debug": "^4.3.1",
"filesize": "^6.1.0",
"fs-extra": "^9.0.1",
"http-call": "^5.3.0",
"inquirer": "^8.2.0",
"lodash": "^4.17.21",
"log-chopper": "^1.0.2",
"semver": "^7.3.5",
Expand All @@ -21,13 +20,14 @@
"@oclif/plugin-help": "^5.1.9",
"@oclif/test": "^2.0.2",
"@types/chai": "^4.2.15",
"@types/cross-spawn": "^6.0.2",
"@types/execa": "^0.9.0",
"@types/fs-extra": "^8.0.1",
"@types/glob": "^7.1.3",
"@types/inquirer": "^8.2.0",
"@types/lodash": "^4.14.168",
"@types/mocha": "^9",
"@types/node": "^14.14.31",
"@types/semver": "^7.3.4",
"@types/supports-color": "^7.2.0",
"@types/write-json-file": "^3.2.1",
"chai": "^4.3.4",
Expand All @@ -37,7 +37,7 @@
"globby": "^11.0.2",
"mocha": "^9",
"nock": "^13.2.1",
"oclif": "^2.3.0",
"oclif": "^2.4.2",
"qqjs": "^0.3.11",
"sinon": "^12.0.1",
"ts-node": "^9.1.1",
Expand Down
54 changes: 42 additions & 12 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,73 @@
import {Command, Flags, Config, CliUx} from '@oclif/core'
import * as path from 'path'
import {Command, Flags, Config} from '@oclif/core'
import {prompt} from 'inquirer'
import {sort} from 'semver'
import UpdateCli from '../update'

async function getPinToVersion(): Promise<string> {
return CliUx.ux.prompt('Enter a version to update to')
}

export default class UpdateCommand extends Command {
static description = 'update the <%= config.bin %> CLI'

static args = [{name: 'channel', optional: true}]

static flags = {
autoupdate: Flags.boolean({hidden: true}),
'from-local': Flags.boolean({description: 'Interactively choose an already installed version.'}),
'from-local': Flags.boolean({
description: 'Interactively choose an already installed version. This is ignored if a channel is provided.',
exclusive: ['version', 'interactive'],
}),
version: Flags.string({
description: 'Install a specific version.',
exclusive: ['from-local'],
exclusive: ['from-local', 'interactive'],
}),
interactive: Flags.boolean({
description: 'Interactively select version to install. This is ignored if a channel is provided.',
exclusive: ['from-local', 'version'],
}),
}

private readonly clientRoot = this.config.scopedEnvVar('OCLIF_CLIENT_HOME') || path.join(this.config.dataDir, 'client')

async run(): Promise<void> {
const {args, flags} = await this.parse(UpdateCommand)

if (args.channel && flags.version) {
this.error('You cannot specifiy both a version and a channel.')
}

let version = flags.version
if (flags['from-local']) {
version = await this.promptForLocalVersion()
} else if (flags.interactive) {
version = await this.promptForRemoteVersion()
}

const updateCli = new UpdateCli({
channel: args.channel,
autoUpdate: flags.autoupdate,
fromLocal: flags['from-local'],
version: flags.version,
version,
config: this.config as Config,
exit: this.exit,
getPinToVersion: getPinToVersion,
})
return updateCli.runUpdate()
}

private async promptForRemoteVersion(): Promise<string> {
const choices = sort(Object.keys(await UpdateCli.fetchVersionIndex(this.config))).reverse()
const {version} = await prompt<{version: string}>({
name: 'version',
message: 'Select a version to update to',
type: 'list',
choices,
})
return version
}

private async promptForLocalVersion(): Promise<string> {
const choices = sort(UpdateCli.findLocalVersions(this.config)).reverse()
const {version} = await prompt<{version: string}>({
name: 'version',
message: 'Select a version to update to',
type: 'list',
choices,
})
return version
}
}
2 changes: 1 addition & 1 deletion src/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ignore = (_: any, header: any) => {
}
}

export async function extract(stream: NodeJS.ReadableStream, basename: string, output: string, sha?: string) {
export async function extract(stream: NodeJS.ReadableStream, basename: string, output: string, sha?: string): Promise<void> {
const getTmp = () => `${output}.partial.${Math.random().toString().split('.')[1].slice(0, 5)}`
let tmp = getTmp()
if (fs.pathExistsSync(tmp)) tmp = getTmp()
Expand Down
Loading

0 comments on commit e44820c

Please sign in to comment.