Skip to content

Commit

Permalink
refactor: upgrade to oclif v2 (#98)
Browse files Browse the repository at this point in the history
* Refactor: upgrade to @oclif/core v2 and adapt to breaking changes to types.

* fix: finish handling breaking import changes

* chore: comment on footgun in tests.

* chore: maintain same import syntax in deps.ts
  • Loading branch information
ryandagg authored Jul 5, 2023
1 parent d888f2a commit 49f820a
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/heroku/heroku-cli-command/issues",
"dependencies": {
"@heroku-cli/color": "^1.1.14",
"@oclif/core": "^1.26.2",
"@oclif/core": "^2.8.11",
"cli-ux": "^6.0.9",
"debug": "^4.1.1",
"fs-extra": "^7.0.1",
Expand Down
33 changes: 17 additions & 16 deletions src/completions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Interfaces} from '@oclif/core'
import {CLIError} from '@oclif/core/lib/errors'
import {Completion} from '@oclif/core/lib/interfaces/parser'
import * as path from 'path'

import deps from './deps'
Expand All @@ -14,15 +15,15 @@ export const herokuGet = async (resource: string, ctx: {config: Interfaces.Confi
return resources.map((a: any) => a.name).sort()
}

export const AppCompletion: Interfaces.Completion = {
export const AppCompletion: Completion = {
cacheDuration: oneDay,
options: async ctx => {
const apps = await herokuGet('apps', ctx)
return apps
},
}

export const AppAddonCompletion: Interfaces.Completion = {
export const AppAddonCompletion: Completion = {
cacheDuration: oneDay,
cacheKey: async ctx => {
return ctx.flags && ctx.flags.app ? `${ctx.flags.app}_addons` : ''
Expand All @@ -33,7 +34,7 @@ export const AppAddonCompletion: Interfaces.Completion = {
},
}

export const AppDynoCompletion: Interfaces.Completion = {
export const AppDynoCompletion: Completion = {
cacheDuration: oneDay,
cacheKey: async ctx => {
return ctx.flags && ctx.flags.app ? `${ctx.flags.app}_dynos` : ''
Expand All @@ -44,7 +45,7 @@ export const AppDynoCompletion: Interfaces.Completion = {
},
}

export const BuildpackCompletion: Interfaces.Completion = {
export const BuildpackCompletion: Completion = {
skipCache: true,

options: async () => {
Expand All @@ -62,15 +63,15 @@ export const BuildpackCompletion: Interfaces.Completion = {
},
}

export const DynoSizeCompletion: Interfaces.Completion = {
export const DynoSizeCompletion: Completion = {
cacheDuration: oneDay * 90,
options: async ctx => {
const sizes = await herokuGet('dyno-sizes', ctx)
return sizes
},
}

export const FileCompletion: Interfaces.Completion = {
export const FileCompletion: Completion = {
skipCache: true,

options: async () => {
Expand All @@ -79,15 +80,15 @@ export const FileCompletion: Interfaces.Completion = {
},
}

export const PipelineCompletion: Interfaces.Completion = {
export const PipelineCompletion: Completion = {
cacheDuration: oneDay,
options: async ctx => {
const pipelines = await herokuGet('pipelines', ctx)
return pipelines
},
}

export const ProcessTypeCompletion: Interfaces.Completion = {
export const ProcessTypeCompletion: Completion = {
skipCache: true,

options: async () => {
Expand All @@ -112,15 +113,15 @@ export const ProcessTypeCompletion: Interfaces.Completion = {
},
}

export const RegionCompletion: Interfaces.Completion = {
export const RegionCompletion: Completion = {
cacheDuration: oneDay * 7,
options: async ctx => {
const regions = await herokuGet('regions', ctx)
return regions
},
}

export const RemoteCompletion: Interfaces.Completion = {
export const RemoteCompletion: Completion = {
skipCache: true,

options: async () => {
Expand All @@ -129,47 +130,47 @@ export const RemoteCompletion: Interfaces.Completion = {
},
}

export const RoleCompletion: Interfaces.Completion = {
export const RoleCompletion: Completion = {
skipCache: true,

options: async () => {
return ['admin', 'collaborator', 'member', 'owner']
},
}

export const ScopeCompletion: Interfaces.Completion = {
export const ScopeCompletion: Completion = {
skipCache: true,

options: async () => {
return ['global', 'identity', 'read', 'write', 'read-protected', 'write-protected']
},
}

export const SpaceCompletion: Interfaces.Completion = {
export const SpaceCompletion: Completion = {
cacheDuration: oneDay,
options: async ctx => {
const spaces = await herokuGet('spaces', ctx)
return spaces
},
}

export const StackCompletion: Interfaces.Completion = {
export const StackCompletion: Completion = {
cacheDuration: oneDay,
options: async ctx => {
const stacks = await herokuGet('stacks', ctx)
return stacks
},
}

export const StageCompletion: Interfaces.Completion = {
export const StageCompletion: Completion = {
skipCache: true,

options: async () => {
return ['test', 'review', 'development', 'staging', 'production']
},
}

export const TeamCompletion: Interfaces.Completion = {
export const TeamCompletion: Completion = {
cacheDuration: oneDay,
options: async ctx => {
const teams = await herokuGet('teams', ctx)
Expand Down
8 changes: 5 additions & 3 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// remote
import core = require('@oclif/core')
import oclif = require('@oclif/core')
import HTTP = require('http-call')
import netrc = require('netrc-parser')

Expand All @@ -10,10 +10,12 @@ import git = require('./git')
import mutex = require('./mutex')
import yubikey = require('./yubikey')

const {ux} = oclif

export const deps = {
// remote
get cli(): typeof core.CliUx.ux {
return fetch('@oclif/core').CliUx.ux
get cli(): typeof ux {
return fetch('@oclif/core').ux
},
get HTTP(): typeof HTTP {
return fetch('http-call')
Expand Down
4 changes: 1 addition & 3 deletions src/login.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import color from '@heroku-cli/color'
import * as Heroku from '@heroku-cli/schema'
import {CliUx, Interfaces} from '@oclif/core'
import {Interfaces, ux} from '@oclif/core'
import HTTP from 'http-call'
import Netrc from 'netrc-parser'
import open = require('open')
import * as os from 'os'

const {ux} = CliUx

import {APIClient, HerokuAPIError} from './api-client'
import {vars} from './vars'

Expand Down
9 changes: 4 additions & 5 deletions test/api-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CliUx, Config} from '@oclif/core'
import {Config, ux} from '@oclif/core'
import base, {expect} from 'fancy-test'
import nock from 'nock'
import {resolve} from 'path'
Expand All @@ -7,8 +7,6 @@ import * as sinon from 'sinon'
import {Command as CommandBase} from '../src/command'
import {RequestId, requestIdHeader} from '../src/request-id'

const cli = CliUx.ux

class Command extends CommandBase {
async run() {}
}
Expand Down Expand Up @@ -120,7 +118,8 @@ describe('api_client', () => {
_api.get('/apps').matchHeader('heroku-two-factor-code', '123456').reply(200, [{name: 'myapp'}])

const cmd = new Command([], ctx.config)
Object.defineProperty(cli, 'prompt', {
// todo: this should be stubbed, but other tests rely on it and break if it is done correctly
Object.defineProperty(ux, 'prompt', {
get: () => () => Promise.resolve('123456'),
})
const {body} = await cmd.heroku.get('/apps')
Expand All @@ -139,7 +138,7 @@ describe('api_client', () => {
api.get('/apps/myapp/dynos').reply(200, {web: 1})

const cmd = new Command([], ctx.config)
Object.defineProperty(cli, 'prompt', {
Object.defineProperty(ux, 'prompt', {
get: () => () => Promise.resolve('123456'),
})
const info = cmd.heroku.get('/apps/myapp')
Expand Down
10 changes: 4 additions & 6 deletions test/flags/org.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {CliUx, Command} from '@oclif/core'
import {Command, ux} from '@oclif/core'
import {expect, fancy} from 'fancy-test'

import * as flags from '../../src/flags'

const cli = CliUx.ux

describe('required', () => {
class OrgCommand extends Command {
static flags = {org: flags.org({required: true})}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.org)
ux.log(flags.org)
}
}

Expand Down Expand Up @@ -42,7 +40,7 @@ describe('optional', () => {
static flags = {org: flags.org()}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.org)
ux.log(flags.org)
}
}

Expand All @@ -68,7 +66,7 @@ describe('optional', () => {
static flags = {org: flags.org()}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.org)
ux.log(flags.org)
}
}

Expand Down
14 changes: 6 additions & 8 deletions test/flags/team.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {CliUx, Command} from '@oclif/core'
import {Command, ux} from '@oclif/core'
import {expect, fancy} from 'fancy-test'

import * as flags from '../../src/flags'

const cli = CliUx.ux

describe('required', () => {
class TeamCommand extends Command {
static flags = {team: flags.team({required: true})}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.team)
ux.log(flags.team)
}
}

Expand Down Expand Up @@ -42,7 +40,7 @@ describe('optional', () => {
static flags = {team: flags.team()}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.team)
ux.log(flags.team)
}
}

Expand All @@ -68,7 +66,7 @@ describe('optional', () => {
static flags = {team: flags.team()}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.team)
ux.log(flags.team)
}
}

Expand All @@ -84,7 +82,7 @@ describe('optional', () => {
static flags = {team: flags.team()}
async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.team)
ux.log(flags.team)
}
}

Expand All @@ -111,7 +109,7 @@ describe('with flag/env variable priorities', () => {

async run() {
const {flags} = await this.parse(this.constructor as any)
cli.log(flags.team)
ux.log(flags.team)
}
}

Expand Down
Loading

0 comments on commit 49f820a

Please sign in to comment.