Skip to content

Commit

Permalink
feat: use oclif/core v2 (#497)
Browse files Browse the repository at this point in the history
* feat: use oclif/core v2

* chore: bump deps
  • Loading branch information
mdonnalley authored Jan 23, 2023
1 parent 4ecac2f commit fb9e48d
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 588 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bugs": "https://github.com/oclif/plugin-update/issues",
"dependencies": {
"@oclif/color": "^1.0.3",
"@oclif/core": "^1.25.0",
"@oclif/core": "^2.0.3",
"cross-spawn": "^7.0.3",
"debug": "^4.3.1",
"filesize": "^6.1.0",
Expand Down Expand Up @@ -39,7 +39,7 @@
"globby": "^11.0.2",
"mocha": "^9",
"nock": "^13.3.0",
"oclif": "^2.4.2",
"oclif": "^3.5.0",
"qqjs": "^0.3.11",
"sinon": "^12.0.1",
"ts-node": "^9.1.1",
Expand Down Expand Up @@ -81,4 +81,4 @@
"build": "rm -rf lib && tsc"
},
"main": "lib/index.js"
}
}
10 changes: 6 additions & 4 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Command, Flags, CliUx} from '@oclif/core'
import {Command, Flags, ux, Args} from '@oclif/core'
import {prompt, Separator} from 'inquirer'
import * as path from 'path'
import {sort} from 'semver'
Expand All @@ -7,7 +7,9 @@ import {Updater} from '../update'
export default class UpdateCommand extends Command {
static description = 'update the <%= config.bin %> CLI'

static args = [{name: 'channel', optional: true}]
static args = {
channel: Args.string({optional: true}),
}

static examples = [
{
Expand Down Expand Up @@ -62,12 +64,12 @@ export default class UpdateCommand extends Command {
return {version, location}
})

CliUx.ux.table(table, {version: {}, location: {}})
ux.table(table, {version: {}, location: {}})
return
}

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

return updater.runUpdate({
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliUx, Interfaces} from '@oclif/core'
import {ux, Interfaces} from '@oclif/core'
import spawn from 'cross-spawn'
import * as fs from 'fs-extra'
import * as path from 'path'
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') CliUx.ux.error(error.stack)
if (error.code !== 'ENOENT') ux.error(error.stack)
if ((global as any).testing) return false
debug('autoupdate ENOENT')
return true
Expand Down
54 changes: 27 additions & 27 deletions src/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable unicorn/prefer-module */
import color from '@oclif/color'
import {Config, CliUx, Interfaces} from '@oclif/core'
import {Config, ux, Interfaces} from '@oclif/core'

import * as fs from 'fs-extra'
import HTTP from 'http-call'
Expand Down Expand Up @@ -40,10 +40,10 @@ export class Updater {
const {autoUpdate, version, force = false} = options
if (autoUpdate) await this.debounce()

CliUx.ux.action.start(`${this.config.name}: Updating CLI`)
ux.action.start(`${this.config.name}: Updating CLI`)

if (this.notUpdatable()) {
CliUx.ux.action.stop('not updatable')
ux.action.stop('not updatable')
return
}

Expand All @@ -54,7 +54,7 @@ export class Updater {
const localVersion = force ? null : await this.findLocalVersion(version)

if (this.alreadyOnVersion(current, localVersion || null)) {
CliUx.ux.action.stop(this.config.scopedEnvVar('HIDE_UPDATED_MESSAGE') ? 'done' : `already on version ${current}`)
ux.action.stop(this.config.scopedEnvVar('HIDE_UPDATED_MESSAGE') ? 'done' : `already on version ${current}`)
return
}

Expand All @@ -75,27 +75,27 @@ export class Updater {
}

await this.config.runHook('update', {channel, version})
CliUx.ux.action.stop()
CliUx.ux.log()
CliUx.ux.log(`Updating to a specific version will not update the channel. If autoupdate is enabled, the CLI will eventually be updated back to ${channel}.`)
ux.action.stop()
ux.log()
ux.log(`Updating to a specific version will not update the channel. If autoupdate is enabled, the CLI will eventually be updated back to ${channel}.`)
} else {
const manifest = await this.fetchChannelManifest(channel)
const updated = manifest.sha ? `${manifest.version}-${manifest.sha}` : manifest.version

if (!force && this.alreadyOnVersion(current, updated)) {
CliUx.ux.action.stop(this.config.scopedEnvVar('HIDE_UPDATED_MESSAGE') ? 'done' : `already on version ${current}`)
ux.action.stop(this.config.scopedEnvVar('HIDE_UPDATED_MESSAGE') ? 'done' : `already on version ${current}`)
} else {
await this.config.runHook('preupdate', {channel, version: updated})
await this.update(manifest, current, updated, force, channel)
}

await this.config.runHook('update', {channel, version: updated})
CliUx.ux.action.stop()
ux.action.stop()
}

await this.touch()
await this.tidy()
CliUx.ux.debug('done')
ux.debug('done')
}

public async findLocalVersions(): Promise<string[]> {
Expand All @@ -107,7 +107,7 @@ export class Updater {
}

public async fetchVersionIndex(): Promise<Updater.VersionIndex> {
CliUx.ux.action.status = 'fetching version index'
ux.action.status = 'fetching version index'
const newIndexUrl = this.config.s3Url(this.s3VersionIndexKey())
try {
const {body} = await HTTP.get<Updater.VersionIndex>(newIndexUrl)
Expand Down Expand Up @@ -179,7 +179,7 @@ export class Updater {
}

private async fetchManifest(s3Key: string): Promise<Interfaces.S3Manifest> {
CliUx.ux.action.status = 'fetching manifest'
ux.action.status = 'fetching manifest'

const url = this.config.s3Url(s3Key)
const {body} = await HTTP.get<Interfaces.S3Manifest | string>(url)
Expand Down Expand Up @@ -213,12 +213,12 @@ export class Updater {
})
const extraction = extract(stream, baseDir, output, sha256gz)

if (CliUx.ux.action.type === 'spinner') {
if (ux.action.type === 'spinner') {
const total = Number.parseInt(stream.headers['content-length']!, 10)
let current = 0
const updateStatus = throttle(
(newStatus: string) => {
CliUx.ux.action.status = newStatus
ux.action.status = newStatus
},
250,
{leading: true, trailing: false},
Expand All @@ -235,7 +235,7 @@ export class Updater {

// eslint-disable-next-line max-params
private async update(manifest: Interfaces.S3Manifest, current: string, updated: string, force: boolean, channel: string) {
CliUx.ux.action.start(`${this.config.name}: Updating CLI from ${color.green(current)} to ${color.green(updated)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)
ux.action.start(`${this.config.name}: Updating CLI from ${color.green(current)} to ${color.green(updated)}${channel === 'stable' ? '' : ' (' + color.yellow(channel) + ')'}`)

await this.ensureClientDir()
const output = path.join(this.clientRoot, updated)
Expand All @@ -248,7 +248,7 @@ export class Updater {
}

private async updateToExistingVersion(current: string, updated: string): Promise<void> {
CliUx.ux.action.start(`${this.config.name}: Updating CLI from ${color.green(current)} to ${color.green(updated)}`)
ux.action.start(`${this.config.name}: Updating CLI from ${color.green(current)} to ${color.green(updated)}`)
await this.ensureClientDir()
await this.refreshConfig(updated)
await this.createBin(updated)
Expand All @@ -257,7 +257,7 @@ export class Updater {
private notUpdatable(): boolean {
if (!this.config.binPath) {
const instructions = this.config.scopedEnvVar('UPDATE_INSTRUCTIONS')
if (instructions) CliUx.ux.warn(instructions)
if (instructions) ux.warn(instructions)
return true
}

Expand All @@ -284,7 +284,7 @@ export class Updater {
const matches = currentVersion.match(/\.\.[/\\|](.+)[/\\|]bin/)
return matches ? matches[1] : this.config.version
} catch (error: any) {
CliUx.ux.debug(error)
ux.debug(error)
}

return this.config.version
Expand All @@ -304,11 +304,11 @@ export class Updater {

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

Expand All @@ -326,22 +326,22 @@ export class Updater {
if (m > new Date()) {
const msg = `waiting until ${m.toISOString()} to update`
if (output) {
CliUx.ux.debug(msg)
ux.debug(msg)
} else {
CliUx.ux.log(msg)
ux.log(msg)
output = true
}

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

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

// removes any unused CLIs
private async tidy(): Promise<void> {
CliUx.ux.debug('tidy')
ux.debug('tidy')
try {
const root = this.clientRoot
if (!await fs.pathExists(root)) return
Expand All @@ -357,19 +357,19 @@ export class Updater {
for (const p of promises) await p // eslint-disable-line no-await-in-loop
await this.logChop()
} catch (error: any) {
CliUx.ux.warn(error)
ux.warn(error)
}
}

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

Expand Down
10 changes: 5 additions & 5 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, CliUx} from '@oclif/core'
import {Config, ux} from '@oclif/core'
import {Config as IConfig} from '@oclif/core/lib/interfaces'
import {Updater} from '../src/update'
import * as zlib from 'zlib'
Expand Down Expand Up @@ -50,10 +50,10 @@ describe('update plugin', () => {
config.binPath = config.binPath || config.bin
collector = {stdout: [], stderr: []}
sandbox = sinon.createSandbox()
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 || ''))
sandbox.stub(ux, 'log').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(ux, 'warn').callsFake(line => collector.stderr.push(line ? `${line}` : ''))
sandbox.stub(ux.action, 'start').callsFake(line => collector.stdout.push(line || ''))
sandbox.stub(ux.action, 'stop').callsFake(line => collector.stdout.push(line || ''))
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
sandbox.stub(Updater.prototype, 'refreshConfig').resolves()
Expand Down
Loading

0 comments on commit fb9e48d

Please sign in to comment.