Skip to content

Commit

Permalink
feat: remove cli-ux (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jan 27, 2022
1 parent 4b5cbcc commit 980c612
Show file tree
Hide file tree
Showing 6 changed files with 1,594 additions and 1,547 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"bugs": "https://github.com/oclif/plugin-update/issues",
"dependencies": {
"@oclif/color": "^1.0.0",
"@oclif/core": "^1.1.1",
"@oclif/core": "^1.2.0",
"@types/semver": "^7.3.4",
"cli-ux": "^6.0.6",
"cross-spawn": "^7.0.3",
"debug": "^4.3.1",
"filesize": "^6.1.0",
Expand All @@ -19,7 +18,7 @@
"tar-fs": "^2.1.1"
},
"devDependencies": {
"@oclif/plugin-help": "5.1.9",
"@oclif/plugin-help": "^5.1.9",
"@oclif/test": "^2.0.2",
"@types/chai": "^4.2.15",
"@types/cross-spawn": "^6.0.2",
Expand All @@ -38,7 +37,7 @@
"globby": "^11.0.2",
"mocha": "^9",
"nock": "^13.2.1",
"oclif": "2.0.0-main.19",
"oclif": "^2.3.0",
"qqjs": "^0.3.11",
"sinon": "^12.0.1",
"ts-node": "^9.1.1",
Expand Down
7 changes: 3 additions & 4 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {Command, Flags, Config} from '@oclif/core'
import cli from 'cli-ux'
import {Command, Flags, Config, CliUx} from '@oclif/core'
import * as path from 'path'
import UpdateCli from '../update'

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

export default class UpdateCommand extends Command {
Expand All @@ -23,7 +22,7 @@ export default class UpdateCommand extends Command {

private readonly clientBin = path.join(this.clientRoot, 'bin', this.config.windows ? `${this.config.bin}.cmd` : this.config.bin)

async run() {
async run(): Promise<void> {
const {args, flags} = await this.parse(UpdateCommand)
const updateCli = new UpdateCli({channel: args.channel, autoUpdate: flags.autoupdate, fromLocal: flags['from-local'], config: this.config as Config, exit: this.exit, getPinToVersion: getPinToVersion})
return updateCli.runUpdate()
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/init.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Interfaces} from '@oclif/core'
import cli from 'cli-ux'
import {CliUx, Interfaces} from '@oclif/core'
import * as spawn from 'cross-spawn'
import * as fs from 'fs-extra'
import * as path from 'path'

import {touch} from '../util'

// eslint-disable-next-line unicorn/prefer-module
const debug = require('debug')('cli:updater')

function timestamp(msg: string): string {
Expand Down Expand Up @@ -40,7 +40,7 @@ export const init: Interfaces.Hook<'init'> = async function (opts) {
m.setHours(m.getHours() + (days * 24))
return m < new Date()
} catch (error: any) {
if (error.code !== 'ENOENT') cli.error(error.stack)
if (error.code !== 'ENOENT') CliUx.ux.error(error.stack)
if ((global as any).testing) return false
debug('autoupdate ENOENT')
return true
Expand Down
60 changes: 30 additions & 30 deletions src/update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable unicorn/prefer-module */
import color from '@oclif/color'
import {Config} from '@oclif/core'
import {Config, CliUx} from '@oclif/core'
import {IManifest} from 'oclif'

import cli from 'cli-ux'
import * as spawn from 'cross-spawn'
import * as fs from 'fs-extra'
import HTTP from 'http-call'
Expand Down Expand Up @@ -37,20 +37,20 @@ export default class UpdateCli {
this.clientBin = path.join(this.clientRoot, 'bin', this.options.config.windows ? `${this.options.config.bin}.cmd` : this.options.config.bin)
}

async runUpdate() {
async runUpdate(): Promise<void> {
if (this.options.autoUpdate) await this.debounce()

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

if (this.options.fromLocal) {
await this.ensureClientDir()
cli.debug(`Looking for locally installed versions at ${this.clientRoot}`)
CliUx.ux.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.')

cli.log(`Found versions: \n${versions.map(version => ` ${version}`).join('\n')}\n`)
CliUx.ux.log(`Found versions: \n${versions.map(version => ` ${version}`).join('\n')}\n`)

const pinToVersion = await this.options.getPinToVersion()
if (!versions.includes(pinToVersion)) throw new Error(`Version ${pinToVersion} not found in the locally installed versions.`)
Expand All @@ -59,34 +59,34 @@ export default class UpdateCli {
throw new Error(`Version ${pinToVersion} is not already installed at ${this.clientRoot}.`)
}

cli.action.start(`${this.options.config.name}: Updating CLI`)
cli.debug(`switching to existing version ${pinToVersion}`)
CliUx.ux.action.start(`${this.options.config.name}: Updating CLI`)
CliUx.ux.debug(`switching to existing version ${pinToVersion}`)
this.updateToExistingVersion(pinToVersion)

cli.log()
cli.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}.`)
CliUx.ux.log()
CliUx.ux.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 {
cli.action.start(`${this.options.config.name}: Updating CLI`)
CliUx.ux.action.start(`${this.options.config.name}: Updating CLI`)
await this.options.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
const reason = await this.skipUpdate()
if (reason) cli.action.stop(reason || 'done')
if (reason) CliUx.ux.action.stop(reason || 'done')
else await this.update(manifest)
cli.debug('tidy')
CliUx.ux.debug('tidy')
await this.tidy()
await this.options.config.runHook('update', {channel: this.channel})
}

cli.debug('done')
cli.action.stop()
CliUx.ux.debug('done')
CliUx.ux.action.stop()
}

private async fetchManifest(): Promise<IManifest> {
const http: typeof HTTP = require('http-call').HTTP

cli.action.status = 'fetching manifest'
CliUx.ux.action.status = 'fetching manifest'

try {
const url = this.options.config.s3Url(this.options.config.s3Key('manifest', {
Expand Down Expand Up @@ -139,13 +139,13 @@ export default class UpdateCli {
const extraction = extract(stream, baseDir, output, sha256gz)

// to-do: use cli.action.type
if ((cli.action as any).frames) {
if ((CliUx.ux.action as any).frames) {
// if spinner action
const total = Number.parseInt(stream.headers['content-length']!, 10)
let current = 0
const updateStatus = _.throttle(
(newStatus: string) => {
cli.action.status = newStatus
CliUx.ux.action.status = newStatus
},
250,
{leading: true, trailing: false},
Expand All @@ -161,7 +161,7 @@ export default class UpdateCli {
}

private async update(manifest: IManifest, channel = 'stable') {
cli.action.start(`${this.options.config.name}: Updating CLI from ${color.green(this.currentVersion)} to ${color.green(this.updatedVersion)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)
CliUx.ux.action.start(`${this.options.config.name}: Updating CLI from ${color.green(this.currentVersion)} to ${color.green(this.updatedVersion)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)

await this.ensureClientDir()
const output = path.join(this.clientRoot, this.updatedVersion)
Expand All @@ -184,7 +184,7 @@ export default class UpdateCli {
private async skipUpdate(): Promise<string | false> {
if (!this.options.config.binPath) {
const instructions = this.options.config.scopedEnvVar('UPDATE_INSTRUCTIONS')
if (instructions) cli.warn(instructions)
if (instructions) CliUx.ux.warn(instructions)
return 'not updatable'
}

Expand Down Expand Up @@ -212,7 +212,7 @@ export default class UpdateCli {
const matches = currentVersion.match(/\.\.[/\\|](.+)[/\\|]bin/)
return matches ? matches[1] : this.options.config.version
} catch (error: any) {
cli.debug(error)
CliUx.ux.debug(error)
}

return this.options.config.version
Expand All @@ -231,11 +231,11 @@ export default class UpdateCli {

private async logChop() {
try {
cli.debug('log chop')
CliUx.ux.debug('log chop')
const logChopper = require('log-chopper').default
await logChopper.chop(this.options.config.errlog)
} catch (error: any) {
cli.debug(error.message)
CliUx.ux.debug(error.message)
}
}

Expand All @@ -253,17 +253,17 @@ export default class UpdateCli {
if (m > new Date()) {
const msg = `waiting until ${m.toISOString()} to update`
if (output) {
cli.debug(msg)
CliUx.ux.debug(msg)
} else {
await cli.log(msg)
await CliUx.ux.log(msg)
output = true
}

await wait(60 * 1000) // wait 1 minute
return this.debounce()
}

cli.log('time to update')
CliUx.ux.log('time to update')
}

// removes any unused CLIs
Expand All @@ -283,26 +283,26 @@ export default class UpdateCli {
for (const p of promises) await p // eslint-disable-line no-await-in-loop
await this.logChop()
} catch (error: any) {
cli.warn(error)
CliUx.ux.warn(error)
}
}

private async touch() {
// touch the client so it won't be tidied up right away
try {
const p = path.join(this.clientRoot, this.options.config.version)
cli.debug('touching client at', p)
CliUx.ux.debug('touching client at', p)
if (!await fs.pathExists(p)) return
await fs.utimes(p, new Date(), new Date())
} catch (error: any) {
cli.warn(error)
CliUx.ux.warn(error)
}
}

private async reexec() {
cli.action.stop()
CliUx.ux.action.stop()
return new Promise((_, reject) => {
cli.debug('restarting CLI after update', this.clientBin)
CliUx.ux.debug('restarting CLI after update', this.clientBin)
spawn(this.clientBin, ['update'], {
stdio: 'inherit',
env: {...process.env, [this.options.config.scopedEnvVarKey('HIDE_UPDATED_MESSAGE')]: '1'},
Expand Down
11 changes: 5 additions & 6 deletions test/update.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import {Config} from '@oclif/core'
import {Config, CliUx} from '@oclif/core'
import {Config as IConfig} from '@oclif/core/lib/interfaces'
import UpdateCli, {UpdateCliOptions} from '../src/update'
import * as zlib from 'zlib'
Expand All @@ -9,7 +9,6 @@ import * as sinon from 'sinon'
import stripAnsi = require('strip-ansi')
import * as extract from '../src/tar'
import {expect} from 'chai'
import cli from 'cli-ux'

type OutputCollectors = {
stdout: string[];
Expand Down Expand Up @@ -57,10 +56,10 @@ describe('update plugin', () => {
config.binPath = config.binPath || config.bin
collector = {stdout: [], stderr: []}
sandbox = sinon.createSandbox()
sandbox.stub(cli, 'log').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(cli, 'warn').callsFake(line => collector.stderr.push(line ? `${line}` : ''))
sandbox.stub(cli.action, 'start').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(cli.action, 'stop').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(CliUx.ux, 'log').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(CliUx.ux, 'warn').callsFake(line => collector.stderr.push(line ? `${line}` : ''))
sandbox.stub(CliUx.ux.action, 'start').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(CliUx.ux.action, 'stop').callsFake(line => collector.stdout.push(line || ''))
})
afterEach(() => {
nock.cleanAll()
Expand Down
Loading

0 comments on commit 980c612

Please sign in to comment.