Skip to content

Commit

Permalink
feat(switching): allows cli to switch versions with param
Browse files Browse the repository at this point in the history
  • Loading branch information
sumwatshade committed Mar 27, 2021
1 parent 0976499 commit 72d6602
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"http-call": "^5.3.0",
"lodash": "^4.17.21",
"log-chopper": "^1.0.2",
"semver": "^7.3.4",
"semver": "^7.3.5",
"tar-fs": "^2.1.1"
},
"devDependencies": {
Expand Down
30 changes: 24 additions & 6 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs from 'fs-extra'
import HTTP from 'http-call'
import * as _ from 'lodash'
import * as path from 'path'
import * as semver from 'semver'

import {extract} from '../tar'
import {ls, wait} from '../util'
Expand All @@ -19,6 +20,7 @@ export default class UpdateCommand extends Command {
static flags: flags.Input<any> = {
autoupdate: flags.boolean({hidden: true}),
'from-local': flags.boolean({description: 'interactively choose an already installed version'}),
'switch-to': flags.string({char: 'v', description: 'specify a specific version to switch to'}),
}

private autoupdate!: boolean
Expand All @@ -41,18 +43,19 @@ export default class UpdateCommand extends Command {

this.channel = args.channel || await this.determineChannel()

// Do not show known non-local version folder names, bin and current.
// const localVersions = fs.readdirSync(this.clientRoot).filter(dirOrFile => dirOrFile !== 'bin' && dirOrFile !== 'current')

if (flags['from-local']) {
await this.ensureClientDir()
this.debug(`Looking for locally installed versions at ${this.clientRoot}`)

// Do not show known non-local version folder names, bin and current.
const versions = fs.readdirSync(this.clientRoot).filter(dirOrFile => dirOrFile !== 'bin' && dirOrFile !== 'current')
if (versions.length === 0) throw new Error('No locally installed versions found.')
if (localVersions.length === 0) throw new Error('No locally installed versions found.')

this.log(`Found versions: \n${versions.map(version => ` ${version}`).join('\n')}\n`)
this.log(`Found versions: \n${localVersions.map(version => ` ${version}`).join('\n')}\n`)

const pinToVersion = await cli.prompt('Enter a version to update to')
if (!versions.includes(pinToVersion)) throw new Error(`Version ${pinToVersion} not found in the locally installed versions.`)
if (!localVersions.includes(pinToVersion)) throw new Error(`Version ${pinToVersion} not found in the locally installed versions.`)

if (!await fs.pathExists(path.join(this.clientRoot, pinToVersion))) {
throw new Error(`Version ${pinToVersion} is not already installed at ${this.clientRoot}.`)
Expand All @@ -64,11 +67,25 @@ export default class UpdateCommand extends Command {
this.log()
this.log(`Updating to an already installed version will not update the channel. If autoupdate is enabled, the CLI will eventually be updated back to ${this.channel}.`)
} else {
let targetVersion
if (flags['switch-to']) {
targetVersion = semver.clean(flags['switch-to'])
this.debug(`Flag overriden target version: ${targetVersion}`)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, channel] = targetVersion?.split('-') || ['', '']
if (channel) {
this.channel = channel.substr(0, channel.indexOf('.'))
}
this.debug(`Flag overriden target channel: ${this.channel}`)
}

cli.action.start(`${this.config.name}: Updating CLI`)
await this.config.runHook('preupdate', {channel: this.channel})
const manifest = await this.fetchManifest()
this.currentVersion = await this.determineCurrentVersion()
this.updatedVersion = (manifest as any).sha ? `${manifest.version}-${(manifest as any).sha}` : manifest.version

this.updatedVersion = (manifest as any).sha ? `${targetVersion || manifest.version}-${(manifest as any).sha}` : targetVersion || manifest.version
this.debug(`Updating to ${this.updatedVersion}`)
const reason = await this.skipUpdate()
if (reason) cli.action.stop(reason || 'done')
else await this.update(manifest)
Expand Down Expand Up @@ -179,6 +196,7 @@ export default class UpdateCommand extends Command {
private async update(manifest: IManifest, channel = 'stable') {
const {channel: manifestChannel} = manifest
if (manifestChannel) channel = manifestChannel

cli.action.start(`${this.config.name}: Updating CLI from ${color.green(this.currentVersion)} to ${color.green(this.updatedVersion)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)

await this.ensureClientDir()
Expand Down
11 changes: 9 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@

"@types/semver@^7.3.4":
version "7.3.4"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.4.tgz#43d7168fec6fa0988bb1a513a697b29296721afb"
resolved "https://registry.npmjs.org/@types/semver/-/semver-7.3.4.tgz#43d7168fec6fa0988bb1a513a697b29296721afb"
integrity sha512-+nVsLKlcUCeMzD2ufHEYuJ9a2ovstb6Dp52A5VsoKxDXgvE051XgHI/33I1EymwkRGQkwnA0LkhnUzituGs4EQ==

"@types/sinon@*":
Expand Down Expand Up @@ -2076,13 +2076,20 @@ semver@^6.0.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.2.1, semver@^7.3.2, semver@^7.3.4:
semver@^7.2.1, semver@^7.3.2:
version "7.3.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97"
integrity sha512-tCfb2WLjqFAtXn4KEdxIhalnRtoKFN7nAwj0B3ZXCbQloV2tq5eDbcTmT68JJD3nRJq24/XgxtQKFIpQdtvmVw==
dependencies:
lru-cache "^6.0.0"

semver@^7.3.5:
version "7.3.5"
resolved "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==
dependencies:
lru-cache "^6.0.0"

serialize-javascript@5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-5.0.1.tgz#7886ec848049a462467a97d3d918ebb2aaf934f4"
Expand Down

0 comments on commit 72d6602

Please sign in to comment.