Skip to content

Commit 4a77a70

Browse files
authored
refactor!: upgrade to oclif/core (#132)
* Upgrade to oclif/core * Fix test, update packages * Fix linting * Fix bin/run * Fix a bug in displaying diff need to await promise * Fix bug by awaiting promise * Update jsdiff * Remove manifest testing file
1 parent 5903f2e commit 4a77a70

File tree

19 files changed

+1782
-1400
lines changed

19 files changed

+1782
-1400
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
}
1212
},
1313
"rules": {
14+
"@typescript-eslint/no-explicit-any": "off",
1415
"camelcase":"off",
16+
"import/namespace": "off",
1517
"indent": ["error", 2, {"MemberExpression": 1}],
16-
"node/no-deprecated-api": "warn",
1718
"node/no-missing-import": "off",
19+
"n/shebang": "warn",
20+
"perfectionist/sort-objects": "warn",
1821
"unicorn/no-array-for-each": "off",
1922
"unicorn/no-useless-undefined": "warn",
2023
"unicorn/prefer-node-protocol": "warn",

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nodejs 16.20.2
1+
nodejs 20.18.1

bin/run

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/usr/bin/env node
22

3-
require('@oclif/command').run()
4-
.catch(require('@oclif/errors/handle'))
3+
const oclif = require('@oclif/core')
4+
5+
oclif.run()
6+
.then(require('@oclif/core/flush'))
7+
.catch(async error => require('@oclif/core/handle')(error))

package.json

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,43 @@
66
"bugs": "https://github.com/heroku/heroku-cli-addons-admin/issues",
77
"dependencies": {
88
"@heroku-cli/color": "^2.0.4",
9-
"@heroku-cli/command": "^8.5.0",
10-
"@heroku-cli/schema": "^1.0.3",
11-
"@oclif/command": "^1.8.36",
12-
"@oclif/config": "^1.18.17",
13-
"@oclif/plugin-help": "^3.3.1",
14-
"cli-ux": "^6.0.9",
15-
"diff": "^3.5.0",
9+
"@heroku-cli/command": "^11.4.0",
10+
"@heroku/http-call": "5.4.0",
11+
"@heroku-cli/schema": "^1.0.25",
12+
"@oclif/core": "^2.16.0",
13+
"diff": "^5.2.0",
1614
"fs-extra": "^9.1.0",
17-
"http-call": "5.3.0",
1815
"inquirer": "^8.2.6",
19-
"jsdiff": "^1.1.1",
2016
"lodash": "^4.17.21",
21-
"randomstring": "^1.3.0",
22-
"tslib": "2.6.3"
17+
"open": "^8.4.2",
18+
"randomstring": "^1.3.1",
19+
"tslib": "2.8.1"
2320
},
2421
"devDependencies": {
25-
"@heroku-cli/dev-cli": "^1.26.13",
22+
"@oclif/plugin-help": "^5",
2623
"@oclif/test": "^2.5.6",
2724
"@types/chai": "^4",
28-
"@types/diff": "^3.5.1",
29-
"@types/execa": "^0.9.0",
25+
"@types/diff": "^5.2.1",
3026
"@types/fs-extra": "^9.0.13",
31-
"@types/glob": "8.1.0",
3227
"@types/inquirer": "^8.2.10",
33-
"@types/mocha": "^10.0.7",
34-
"@types/nock": "^9.3.0",
28+
"@types/mocha": "^10.0.10",
3529
"@types/node": "^22.8.7",
3630
"@types/randomstring": "^1.3.0",
37-
"@types/supports-color": "^5.3.0",
38-
"@types/write-json-file": "^2.2.1",
31+
"@types/supports-color": "^8.1.3",
3932
"chai": "^4",
40-
"eslint": "^7",
41-
"eslint-config-oclif": "^4.0.0",
42-
"eslint-config-oclif-typescript": "^1.0.2",
43-
"globby": "^10",
33+
"eslint": "^8.57.0",
34+
"eslint-config-oclif": "^5.0.0",
35+
"eslint-config-oclif-typescript": "^3.0.26",
4436
"mocha": "^10.7.3",
45-
"nock": "^13.5.4",
37+
"nock": "^13.5.6",
4638
"nyc": "^17.1.0",
4739
"sinon": "^18.0.0",
4840
"ts-node": "^10.9.2",
4941
"tsheredoc": "^1.0.1",
5042
"typescript": "4.9.5"
5143
},
5244
"engines": {
53-
"node": ">= 16.0"
45+
"node": ">= 20.0"
5446
},
5547
"files": [
5648
"/lib",
@@ -82,11 +74,13 @@
8274
},
8375
"repository": "heroku/heroku-cli-addons-admin",
8476
"scripts": {
77+
"build": "rm -rf lib && tsc",
8578
"clean:branch": "./bin/clean-branch.sh",
8679
"lint": "eslint . --ext .ts --config .eslintrc",
8780
"postpack": "rm -f oclif.manifest.json",
8881
"posttest": "tsc -p test --noEmit",
89-
"prepack": "rm -rf lib && tsc && oclif-dev manifest && oclif-dev readme",
82+
"prepack": "yarn build && oclif-dev manifest && oclif-dev readme",
83+
"pretest": "yarn build",
9084
"test": "nyc mocha --forbid-only \"test/**/*.test.ts\"",
9185
"version": "oclif-dev readme && git add README.md"
9286
}

src/addon-client.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
import {HTTPError} from '@heroku/http-call'
12
import {APIClient} from '@heroku-cli/command'
2-
import cli from 'cli-ux'
3-
import * as url from 'url'
4-
import {HTTPError} from 'http-call'
3+
import {ux} from '@oclif/core'
4+
import {URL} from 'node:url'
55

66
export default class AddonClient {
77
private readonly client: APIClient
88

99
private readonly options: any = {
1010
headers: {
11+
Accept: 'application/json',
1112
'Content-Type': 'application/json',
1213
'User-Agent': 'kensa future',
13-
Accept: 'application/json',
1414
},
1515
}
1616

1717
constructor(config: any) {
1818
const client = new APIClient(config, {})
1919
const host = process.env.HEROKU_ADDONS_HOST
2020
const herokuHost = process.env.HEROKU_HOST || 'heroku.com'
21-
client.defaults.host = host ? url.parse(host).host : `addons.${herokuHost}`
21+
client.defaults.host = host ? new URL(host).host : `addons.${herokuHost}`
2222

2323
this.client = client
2424
}
@@ -27,7 +27,7 @@ export default class AddonClient {
2727
const response = await this.client.get(path, this.options).catch((error: HTTPError) => {
2828
const errorBody = error?.body?.error
2929
if (errorBody) {
30-
cli.error(errorBody)
30+
ux.error(errorBody)
3131
}
3232

3333
throw error
@@ -43,12 +43,12 @@ export default class AddonClient {
4343
const response = await this.client.post(path, opts).catch((error: HTTPError) => {
4444
const baseErrors = error?.body?.error?.base
4545
if (baseErrors) {
46-
cli.error(baseErrors.join(', '))
46+
ux.error(baseErrors.join(', '))
4747
}
4848

4949
const errorBody = error?.body?.error
5050
if (errorBody) {
51-
cli.error(errorBody)
51+
ux.error(errorBody)
5252
}
5353

5454
throw error

src/addon.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,73 @@
1-
import cli from 'cli-ux'
2-
1+
import {HTTPError} from '@heroku/http-call'
32
import color from '@heroku-cli/color'
4-
import {IConfig} from '@oclif/config'
5-
import {HTTPError} from 'http-call'
3+
import {ux} from '@oclif/core'
4+
import {Config} from '@oclif/core'
65

76
import AddonClient from './addon-client'
87
import {ManifestInterface, ManifestLocal, ManifestRemote} from './manifest'
98

109
export default class Addon {
1110
readonly argsSlug?: string
11+
private readonly _client: AddonClient
1212
private readonly _local: ManifestLocal
1313
private readonly _remote: ManifestRemote
14-
private readonly _client: AddonClient
1514

16-
constructor(config: IConfig, argsSlug?: string) {
15+
constructor(config: Config, argsSlug?: string) {
1716
this.argsSlug = argsSlug
1817
this._local = new ManifestLocal()
1918
this._remote = new ManifestRemote(this)
2019

2120
this._client = new AddonClient(config)
2221
}
2322

24-
local(): ManifestLocal {
25-
return this._local
23+
client(): AddonClient {
24+
return this._client
2625
}
2726

28-
remote(): ManifestRemote {
29-
return this._remote
27+
local(): ManifestLocal {
28+
return this._local
3029
}
3130

32-
async slug(): Promise<string> {
33-
// allows users to pull without declaring slug
34-
if (this.argsSlug) {
35-
return this.argsSlug
36-
}
37-
38-
const manifest = await this.local().get()
39-
if (!manifest.id) {
40-
cli.error('No slug found in manifest')
41-
}
31+
async manifest(uuid: string): Promise<ManifestInterface> {
32+
const slug = await this.slug()
33+
ux.action.start(`Fetching add-on manifest for ${color.addon(slug)}`)
34+
const body = await this._client.get(`/api/v3/addons/${encodeURIComponent(slug)}/manifests/${encodeURIComponent(uuid)}`)
35+
ux.action.stop()
4236

43-
return manifest.id
37+
return body.contents
4438
}
4539

4640
async manifests(): Promise<ManifestInterface[]> {
4741
const slug = await this.slug()
48-
cli.action.start(`Fetching add-on manifests for ${color.addon(slug)}`)
42+
ux.action.start(`Fetching add-on manifests for ${color.addon(slug)}`)
4943
const body = await this._client.get(`/api/v3/addons/${encodeURIComponent(slug)}/manifests`).catch((error: HTTPError) => {
5044
const errorBody = error?.body?.error
5145
if (errorBody) {
52-
cli.error(errorBody)
46+
ux.error(errorBody)
5347
}
5448

5549
throw error
5650
})
5751

58-
cli.action.stop()
52+
ux.action.stop()
5953
return body
6054
}
6155

62-
async manifest(uuid: string): Promise<ManifestInterface> {
63-
const slug = await this.slug()
64-
cli.action.start(`Fetching add-on manifest for ${color.addon(slug)}`)
65-
const body = await this._client.get(`/api/v3/addons/${encodeURIComponent(slug)}/manifests/${encodeURIComponent(uuid)}`)
66-
cli.action.stop()
67-
68-
return body.contents
56+
remote(): ManifestRemote {
57+
return this._remote
6958
}
7059

71-
client(): AddonClient {
72-
return this._client
60+
async slug(): Promise<string> {
61+
// allows users to pull without declaring slug
62+
if (this.argsSlug) {
63+
return this.argsSlug
64+
}
65+
66+
const manifest = await this.local().get()
67+
if (!manifest.id) {
68+
ux.error('No slug found in manifest')
69+
}
70+
71+
return manifest.id
7372
}
7473
}

src/commands/addons/admin/manifest/diff.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ export default class Diff extends Command {
88
static description = 'compares remote manifest to local manifest and finds differences'
99

1010
async run() {
11-
this.parse(Diff)
12-
1311
const addon = new Addon(this.config)
1412

1513
// reading current manifest
1614
const body = await addon.remote().get()
1715

1816
const fetchedManifest = JSON.stringify(body, null, 2)
19-
const localManifest = JSON.stringify(addon.local().get(), null, 2)
20-
21-
const diff = diffLines(fetchedManifest, localManifest, {newlineIsToken: true, ignoreCase: true})
17+
const localManifest = await addon.local().get()
18+
const localManifestString = JSON.stringify(localManifest, null, 2)
19+
const diff = diffLines(fetchedManifest, localManifestString, {ignoreCase: true, newlineIsToken: true})
2220
diff.forEach((substr: any) => {
23-
let outputColor: 'white' | 'green' | 'red' = 'white'
21+
let outputColor: 'green' | 'red' | 'white' = 'white'
2422
if (substr.added) {
2523
outputColor = 'green' // this is supposed to be a bold green (chalk.green.bold)
2624
} else if (substr.removed) {

0 commit comments

Comments
 (0)