Skip to content

Commit 2f09a72

Browse files
authored
fix: work with 4.8.3 (#493)
* fix: work with 4.8.3 * test: fix test compilation * chore: refine solution
1 parent 2038ad4 commit 2f09a72

File tree

13 files changed

+190
-157
lines changed

13 files changed

+190
-157
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
"shelljs": "^0.8.5",
7474
"shx": "^0.3.4",
7575
"sinon": "^11.1.2",
76-
"ts-node": "^9.1.1",
76+
"ts-node": "^10.9.1",
7777
"tsd": "^0.22.0",
78-
"typescript": "4.5.5"
78+
"typescript": "^4.8.3"
7979
},
8080
"engines": {
8181
"node": ">=14.0.0"
@@ -111,7 +111,7 @@
111111
"prepack": "yarn run build",
112112
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
113113
"test:e2e": "mocha \"test/**/*.e2e.ts\" --timeout 600000",
114-
"pretest": "yarn build --noEmit && tsc -p test --noEmit"
114+
"pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck"
115115
},
116116
"types": "lib/index.d.ts"
117117
}

src/command.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ export default abstract class Command {
107107
if (value === true) {
108108
this.globalFlags = jsonFlag
109109
} else {
110-
// @ts-expect-error because this.globalFlags is typed as a plain object
111110
delete this.globalFlags?.json
112111
this.flags = {} // force the flags setter to run
113-
// @ts-expect-error because this.flags is typed as a plain object
114112
delete this.flags?.json
115113
}
116114
}
@@ -250,7 +248,7 @@ export default abstract class Command {
250248
g['http-call']!.userAgent = this.config.userAgent
251249
}
252250

253-
protected async parse<F, G, A extends { [name: string]: any }>(options?: Interfaces.Input<F, G>, argv = this.argv): Promise<Interfaces.ParserOutput<F, G, A>> {
251+
protected async parse<F extends Interfaces.FlagOutput, G extends Interfaces.FlagOutput, A extends { [name: string]: any }>(options?: Interfaces.Input<F, G>, argv = this.argv): Promise<Interfaces.ParserOutput<F, G, A>> {
254252
if (!options) options = this.constructor as any
255253
const opts = {context: this, ...options}
256254
// the spread operator doesn't work with getters so we have to manually add it here

src/interfaces/parser.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,4 @@ export type CompletableOptionFlag<T> = OptionFlag<T> & {
246246

247247
export type CompletableFlag<T> = BooleanFlag<T> | CompletableOptionFlag<T>
248248

249-
// eslint-disable-next-line @typescript-eslint/ban-types
250-
export type FlagInput<T extends FlagOutput = object> = { [P in keyof T]: CompletableFlag<T[P]> }
249+
export type FlagInput<T extends FlagOutput = { [flag: string]: any }> = { [P in keyof T]: CompletableFlag<T[P]> }

src/parser/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as args from './args'
22
import Deps from './deps'
33
import * as flags from './flags'
44
import {Parser} from './parse'
5-
import {FlagInput, Input, ParserOutput} from '../interfaces'
5+
import {FlagInput, Input, ParserOutput, OutputFlags, FlagOutput} from '../interfaces'
66
import * as Validate from './validate'
77
export {args}
88
export {flags}
@@ -13,7 +13,7 @@ const m = Deps()
1313
// eslint-disable-next-line node/no-missing-require
1414
.add('validate', () => require('./validate').validate as typeof Validate.validate)
1515

16-
export async function parse<TFlags, GFlags, TArgs extends { [name: string]: string }>(argv: string[], options: Input<TFlags, GFlags>): Promise<ParserOutput<TFlags, GFlags, TArgs>> {
16+
export async function parse<TFlags extends OutputFlags<any>, GFlags extends FlagOutput, TArgs extends { [name: string]: string }>(argv: string[], options: Input<TFlags, GFlags>): Promise<ParserOutput<TFlags, GFlags, TArgs>> {
1717
const input = {
1818
argv,
1919
context: options.context,

src/parser/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function pickBy<T>(obj: T, fn: (i: T[keyof T]) => boolean): Partial<T> {
1+
export function pickBy<T extends { [s: string]: T[keyof T]; } | ArrayLike<T[keyof T]>>(obj: T, fn: (i: T[keyof T]) => boolean): Partial<T> {
22
return Object.entries(obj)
33
.reduce((o, [k, v]) => {
44
if (fn(v)) o[k] = v

test/help/format-command-with-options.test.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {expect, test as base} from '@oclif/test'
2-
import stripAnsi = require('strip-ansi')
32

4-
import {Command as Base, Flags as flags, Interfaces, toCached} from '../../src'
5-
import {Help, CommandHelp} from '../../src/help'
3+
import {Command as Base, Flags as flags} from '../../src'
4+
import {commandHelp, TestHelpWithOptions as TestHelp} from './help-test-utils'
65

76
const g: any = global
87
g.oclif.columns = 80
@@ -15,47 +14,10 @@ class Command extends Base {
1514
}
1615
}
1716

18-
// Allow overriding section headers
19-
class TestCommandHelp extends CommandHelp {
20-
protected sections() {
21-
const sections = super.sections()
22-
const flagSection = sections.find(section => section.header === 'FLAGS')
23-
if (flagSection) flagSection.header = 'OPTIONS'
24-
return sections
25-
}
26-
}
27-
28-
// extensions to expose method as public for testing
29-
class TestHelp extends Help {
30-
CommandHelpClass = TestCommandHelp
31-
32-
constructor(config: Interfaces.Config, opts: Partial<Interfaces.HelpOptions> = {}) {
33-
super(config, opts)
34-
this.opts.showFlagNameInTitle = true
35-
this.opts.showFlagOptionsInTitle = true
36-
this.opts.hideCommandSummaryInDescription = true
37-
}
38-
39-
public formatCommand(command: Interfaces.Command) {
40-
return super.formatCommand(command)
41-
}
42-
}
43-
4417
const test = base
4518
.loadConfig()
4619
.add('help', ctx => new TestHelp(ctx.config as any))
47-
.register('commandHelp', (command?: any) => ({
48-
async run(ctx: {help: TestHelp; commandHelp: string; expectation: string}) {
49-
const cached = await toCached(command!, {} as any)
50-
const help = ctx.help.formatCommand(cached)
51-
if (process.env.TEST_OUTPUT === '1') {
52-
console.log(help)
53-
}
54-
55-
ctx.commandHelp = stripAnsi(help).split('\n').map(s => s.trimEnd()).join('\n')
56-
ctx.expectation = 'has commandHelp'
57-
},
58-
}))
20+
.register('commandHelp', commandHelp)
5921

6022
describe('formatCommand', () => {
6123
test

test/help/format-command.test.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {expect, test as base} from '@oclif/test'
2-
import stripAnsi = require('strip-ansi')
32

4-
import {Command as Base, Flags as flags, Interfaces, toCached} from '../../src'
5-
import {Help} from '../../src/help'
3+
import {Command as Base, Flags as flags} from '../../src'
4+
import {commandHelp, TestHelp} from './help-test-utils'
65

76
const g: any = global
87
g.oclif.columns = 80
@@ -13,28 +12,10 @@ class Command extends Base {
1312
}
1413
}
1514

16-
// extensions to expose method as public for testing
17-
class TestHelp extends Help {
18-
public formatCommand(command: Interfaces.Command) {
19-
return super.formatCommand(command)
20-
}
21-
}
22-
2315
const test = base
2416
.loadConfig()
2517
.add('help', ctx => new TestHelp(ctx.config as any))
26-
.register('commandHelp', (command?: any) => ({
27-
async run(ctx: {help: TestHelp; commandHelp: string; expectation: string}) {
28-
const cached = await toCached(command!, {} as any)
29-
const help = ctx.help.formatCommand(cached)
30-
if (process.env.TEST_OUTPUT === '1') {
31-
console.log(help)
32-
}
33-
34-
ctx.commandHelp = stripAnsi(help).split('\n').map(s => s.trimEnd()).join('\n')
35-
ctx.expectation = 'has commandHelp'
36-
},
37-
}))
18+
.register('commandHelp', commandHelp)
3819

3920
describe('formatCommand', () => {
4021
test

test/help/format-commands.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ class TestHelp extends Help {
1414
}
1515
}
1616

17-
const test = base
18-
.loadConfig()
19-
.add('help', ctx => new TestHelp(ctx.config as any))
20-
.register('formatCommands', (commands: Interfaces.Command[] = []) => ({
17+
const formatCommands = (commands: Interfaces.Command[]) => ({
2118
run(ctx: {help: TestHelp; output: string}) {
2219
const help = ctx.help.formatCommands(commands)
2320
if (process.env.TEST_OUTPUT === '1') {
@@ -26,7 +23,12 @@ const test = base
2623

2724
ctx.output = stripAnsi(help).split('\n').map(s => s.trimEnd()).join('\n')
2825
},
29-
}))
26+
})
27+
28+
const test = base
29+
.loadConfig()
30+
.add('help', ctx => new TestHelp(ctx.config as any))
31+
.register('formatCommands', formatCommands)
3032

3133
describe('formatCommand', () => {
3234
test
@@ -47,8 +49,6 @@ describe('formatCommand', () => {
4749

4850
static description = 'This is a very long command description that should wrap after too many characters have been entered'
4951

50-
static flags = {}
51-
5252
static args = []
5353

5454
async run() {

test/help/format-root.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ class TestHelp extends Help {
1717
}
1818
}
1919

20-
const test = base
21-
.loadConfig()
22-
.register('rootHelp', (ctxOverride?: (config: Interfaces.Config) => Interfaces.Config) => ({
20+
const rootHelp = (ctxOverride?: (config: Interfaces.Config) => Interfaces.Config) => ({
2321
run(ctx: { config: Interfaces.Config; help: Help; commandHelp: string; expectation: string}) {
2422
const config = ctxOverride ? ctxOverride(ctx.config) : ctx.config
2523

@@ -31,7 +29,11 @@ const test = base
3129

3230
ctx.commandHelp = stripAnsi(root).split('\n').map(s => s.trimEnd()).join('\n')
3331
},
34-
}))
32+
})
33+
34+
const test = base
35+
.loadConfig()
36+
.register('rootHelp', rootHelp)
3537

3638
describe('formatRoot', () => {
3739
test

test/help/format-topic.test.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
import {expect, test as base} from '@oclif/test'
2-
import stripAnsi = require('strip-ansi')
3-
4-
import {Help} from '../../src/help'
5-
import {Interfaces} from '../../src'
2+
import {TestHelp, topicHelp} from './help-test-utils'
63

74
const g: any = global
85
g.oclif.columns = 80
96

10-
// extensions to expose method as public for testing
11-
class TestHelp extends Help {
12-
public formatTopic(topic: Interfaces.Topic) {
13-
return super.formatTopic(topic)
14-
}
15-
}
16-
177
const test = base
188
.loadConfig()
199
.add('help', ctx => {
2010
return new TestHelp(ctx.config as any)
2111
})
22-
.register('topicHelp', (topic: Interfaces.Topic) => ({
23-
run(ctx: {help: TestHelp; commandHelp: string; expectation: string}) {
24-
const topicHelpOutput = ctx.help.formatTopic(topic)
25-
if (process.env.TEST_OUTPUT === '1') {
26-
console.log(topicHelpOutput)
27-
}
28-
29-
ctx.commandHelp = stripAnsi(topicHelpOutput).split('\n').map(s => s.trimEnd()).join('\n')
30-
ctx.expectation = 'has topicHelp'
31-
},
32-
}))
12+
.register('topicHelp', topicHelp)
3313

3414
describe('formatHelp', () => {
3515
test

0 commit comments

Comments
 (0)