Skip to content

Commit c62f584

Browse files
authored
chore(deps): use typescript 4+ (#254)
1 parent f6db8a3 commit c62f584

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+780
-351
lines changed

.eslintrc

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
],
66
"rules": {
77
"unicorn/no-abusive-eslint-disable": "off",
8-
"@typescript-eslint/no-empty-function": "off"
8+
"unicorn/prefer-spread": "off",
9+
"unicorn/prefer-module": "off",
10+
"unicorn/prefer-node-protocol": "off",
11+
"unicorn/import-style": "off",
12+
"unicorn/consistent-function-scoping": "off",
13+
"unicorn/no-array-reduce": "off",
14+
"unicorn/prefer-array-some": "off",
15+
"node/no-missing-import": "off",
16+
"@typescript-eslint/no-empty-function": "off",
17+
"@typescript-eslint/ban-ts-ignore": "off",
18+
"@typescript-eslint/ban-ts-comment": "off",
19+
"@typescript-eslint/explicit-module-boundary-types": "off"
920
}
1021
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"chai-as-promised": "^7.1.1",
5151
"commitlint": "^12.1.1",
5252
"eslint": "^7.3.1",
53-
"eslint-config-oclif": "^3.1.0",
54-
"eslint-config-oclif-typescript": "^0.2.0",
53+
"eslint-config-oclif": "^4.0.0",
54+
"eslint-config-oclif-typescript": "^1.0.2",
5555
"fancy-test": "^1.4.3",
5656
"globby": "^11.0.1",
5757
"husky": "6",
@@ -62,7 +62,7 @@
6262
"shx": "^0.3.3",
6363
"sinon": "^11.1.1",
6464
"ts-node": "^9.0.0",
65-
"typescript": "3.8.3"
65+
"typescript": "4.4.3"
6666
},
6767
"engines": {
6868
"node": ">=12.0.0"

src/command.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default abstract class Command {
107107
opts = fileURLToPath(opts)
108108
}
109109

110+
// to-do: update in node-14 to module.main
110111
const config = await Config.load(opts || (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname)
111112
const cmd = new this(argv, config)
112113
return cmd._run(argv)
@@ -134,7 +135,7 @@ export default abstract class Command {
134135

135136
protected debug: (...args: any[]) => void
136137

137-
constructor(public argv: string[], public config: Interfaces.Config) {
138+
constructor(public argv: string[], public config: Config) {
138139
this.id = this.ctor.id
139140
try {
140141
this.debug = require('debug')(this.id ? `${this.config.bin}:${this.id}` : this.config.bin)
@@ -155,7 +156,7 @@ export default abstract class Command {
155156
delete process.env[this.config.scopedEnvVarKey('REDIRECTED')]
156157
await this.init()
157158
result = await this.run()
158-
} catch (error) {
159+
} catch (error: any) {
159160
err = error
160161
await this.catch(error)
161162
} finally {
@@ -165,26 +166,27 @@ export default abstract class Command {
165166
if (result && this.jsonEnabled()) {
166167
cli.styledJSON(this.toSuccessJson(result))
167168
}
169+
168170
return result
169171
}
170172

171-
exit(code = 0) {
173+
exit(code = 0): void {
172174
return Errors.exit(code)
173175
}
174176

175-
warn(input: string | Error) {
177+
warn(input: string | Error): void {
176178
Errors.warn(input)
177179
}
178180

179181
error(input: string | Error, options: {code?: string; exit: false} & PrettyPrintableError): void
180182

181183
error(input: string | Error, options?: {code?: string; exit?: number} & PrettyPrintableError): never
182184

183-
error(input: string | Error, options: {code?: string; exit?: number | false} & PrettyPrintableError = {}) {
185+
error(input: string | Error, options: {code?: string; exit?: number | false} & PrettyPrintableError = {}): void {
184186
return Errors.error(input, options as any)
185187
}
186188

187-
log(message = '', ...args: any[]) {
189+
log(message = '', ...args: any[]): void {
188190
if (!this.jsonEnabled()) {
189191
// tslint:disable-next-line strict-type-predicates
190192
message = typeof message === 'string' ? message : inspect(message)
@@ -222,17 +224,18 @@ export default abstract class Command {
222224
return Parser.parse(argv, opts)
223225
}
224226

225-
protected async catch(err: any): Promise<any> {
227+
protected async catch(err: Record<string, any>): Promise<any> {
226228
process.exitCode = process.exitCode ?? err.exitCode ?? 1
227229
if (this.jsonEnabled()) {
228230
cli.styledJSON(this.toErrorJson(err))
229231
} else {
230232
if (!err.message) throw err
231233
try {
232234
const {cli} = require('cli-ux')
233-
const chalk = require('chalk') // eslint-disable-line node/no-extraneous-require
235+
const chalk = require('chalk')
234236
cli.action.stop(chalk.bold.red('!'))
235237
} catch {}
238+
236239
throw err
237240
}
238241
}
@@ -242,16 +245,16 @@ export default abstract class Command {
242245
const config = Errors.config
243246
if (config.errorLogger) await config.errorLogger.flush()
244247
// tslint:disable-next-line no-console
245-
} catch (error) {
248+
} catch (error: any) {
246249
console.error(error)
247250
}
248251
}
249252

250-
protected toSuccessJson(result: any): any {
253+
protected toSuccessJson(result: unknown): any {
251254
return result
252255
}
253256

254-
protected toErrorJson(err: any): any {
257+
protected toErrorJson(err: unknown): any {
255258
return {error: err}
256259
}
257260
}

src/config/config.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ export class Config implements IConfig {
9090
private _topics?: Topic[]
9191

9292
// eslint-disable-next-line no-useless-constructor
93-
constructor(public options: Options) {
94-
}
93+
constructor(public options: Options) {}
9594

9695
static async load(opts: LoadOptions = (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) {
9796
// Handle the case when a file URL string is passed in such as 'import.meta.url'; covert to file path.
@@ -181,7 +180,7 @@ export class Config implements IConfig {
181180
try {
182181
const devPlugins = this.pjson.oclif.devPlugins
183182
if (devPlugins) await this.loadPlugins(this.root, 'dev', devPlugins)
184-
} catch (error) {
183+
} catch (error: any) {
185184
process.emitWarning(error)
186185
}
187186
}
@@ -197,7 +196,7 @@ export class Config implements IConfig {
197196
if (!pjson.oclif.plugins) pjson.oclif.plugins = []
198197
await this.loadPlugins(userPJSONPath, 'user', pjson.oclif.plugins.filter((p: any) => p.type === 'user'))
199198
await this.loadPlugins(userPJSONPath, 'link', pjson.oclif.plugins.filter((p: any) => p.type === 'link'))
200-
} catch (error) {
199+
} catch (error: any) {
201200
if (error.code !== 'ENOENT') process.emitWarning(error)
202201
}
203202
}
@@ -263,7 +262,7 @@ export class Config implements IConfig {
263262
final.successes.push({plugin: p, result})
264263

265264
debug('done')
266-
} catch (error) {
265+
} catch (error: any) {
267266
final.failures.push({plugin: p, error: error as Error})
268267
debug(error)
269268
}
@@ -277,13 +276,15 @@ export class Config implements IConfig {
277276
return final
278277
}
279278

279+
// eslint-disable-next-line default-param-last
280280
async runCommand<T = unknown>(id: string, argv: string[] = [], cachedCommand?: Command.Plugin): Promise<T> {
281281
debug('runCommand %s %o', id, argv)
282282
const c = cachedCommand || this.findCommand(id)
283283
if (!c) {
284284
await this.runHook('command_not_found', {id, argv})
285285
throw new CLIError(`command ${id} not found`)
286286
}
287+
287288
const command = await c.load()
288289
await this.runHook('prerun', {Command: command, argv})
289290
const result = (await command.run(argv, this)) as T
@@ -302,8 +303,7 @@ export class Config implements IConfig {
302303

303304
scopedEnvVarKey(k: string) {
304305
return [this.bin, k]
305-
// eslint-disable-next-line no-useless-escape
306-
.map(p => p.replace(/@/g, '').replace(/[-\/]/g, '_'))
306+
.map(p => p.replace(/@/g, '').replace(/[/-]/g, '_'))
307307
.join('_')
308308
.toUpperCase()
309309
}
@@ -348,14 +348,17 @@ export class Config implements IConfig {
348348
// If b appears first in the pjson.plugins sort it first
349349
return aIndex - bIndex
350350
}
351+
351352
// if b is a core plugin and a is not sort b first
352353
if (b.pluginType === 'core' && a.pluginType !== 'core') {
353354
return 1
354355
}
356+
355357
// if a is a core plugin and b is not sort a first
356358
if (a.pluginType === 'core' && b.pluginType !== 'core') {
357359
return -1
358360
}
361+
359362
// neither plugin is core, so do not change the order
360363
return 0
361364
})
@@ -397,17 +400,20 @@ export class Config implements IConfig {
397400
} else topics.push(topic)
398401
}
399402
}
403+
400404
// add missing topics
401405
for (const c of this.commands.filter(c => !c.hidden)) {
402406
const parts = c.id.split(':')
403-
while (parts.length) {
407+
while (parts.length > 0) {
404408
const name = parts.join(':')
405409
if (name && !topics.find(t => t.name === name)) {
406410
topics.push({name, description: c.summary || c.description})
407411
}
412+
408413
parts.pop()
409414
}
410415
}
416+
411417
this._topics = topics
412418
return this._topics
413419
}
@@ -460,6 +466,7 @@ export class Config implements IConfig {
460466
} else {
461467
shellPath = ['unknown']
462468
}
469+
463470
return shellPath[shellPath.length - 1]
464471
}
465472

@@ -468,8 +475,8 @@ export class Config implements IConfig {
468475
try {
469476
const {enabled} = require('debug')(this.bin)
470477
if (enabled) return 1
471-
} catch {
472-
}
478+
} catch {}
479+
473480
return 0
474481
}
475482

@@ -486,18 +493,19 @@ export class Config implements IConfig {
486493
opts.tag = plugin.tag || opts.tag
487494
opts.root = plugin.root || opts.root
488495
}
496+
489497
const instance = new Plugin.Plugin(opts)
490498
await instance.load()
491499
if (this.plugins.find(p => p.name === instance.name)) return
492500
this.plugins.push(instance)
493501
if (parent) {
494-
// eslint-disable-next-line require-atomic-updates
495502
instance.parent = parent
496503
if (!parent.children) parent.children = []
497504
parent.children.push(instance)
498505
}
506+
499507
await this.loadPlugins(instance.root, type, instance.pjson.oclif.plugins || [], instance)
500-
} catch (error) {
508+
} catch (error: any) {
501509
this.warn(error, 'loadPlugins')
502510
}
503511
}))
@@ -581,7 +589,6 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
581589
options: flag.options,
582590
dependsOn: flag.dependsOn,
583591
exclusive: flag.exclusive,
584-
// eslint-disable-next-line no-await-in-loop
585592
default: typeof flag.default === 'function' ? await flag.default({options: {}, flags: {}}) : flag.default,
586593
}
587594
}
@@ -620,9 +627,9 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
620627
const stdKeys = Object.keys(stdProperties)
621628
const keysToAdd = Object.keys(c).filter(property => ![...stdKeys, ...ignoreCommandProperties].includes(property))
622629
const additionalProperties: any = {}
623-
keysToAdd.forEach(key => {
630+
for (const key of keysToAdd) {
624631
additionalProperties[key] = (c as any)[key]
625-
})
632+
}
626633

627634
return {...stdProperties, ...additionalProperties}
628635
}

src/config/plugin.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function topicsToArray(input: any, base?: string): Topic[] {
2323
if (Array.isArray(input)) {
2424
return input.concat(flatMap(input, t => topicsToArray(t.subtopics, `${base}${t.name}`)))
2525
}
26+
2627
return flatMap(Object.keys(input), k => {
2728
input[k].name = k
2829
return [{...input[k], name: `${base}${k}`}].concat(topicsToArray(input[k].subtopics, `${base}${input[k].name}`))
@@ -44,8 +45,10 @@ async function findRoot(name: string | undefined, root: string) {
4445
yield from
4546
from = path.dirname(from)
4647
}
48+
4749
yield from
4850
}
51+
4952
for (const next of up(root)) {
5053
let cur
5154
if (name) {
@@ -56,7 +59,7 @@ async function findRoot(name: string | undefined, root: string) {
5659
// eslint-disable-next-line no-await-in-loop
5760
const pkg = await loadJSON(path.join(next, 'package.json'))
5861
if (pkg.name === name) return next
59-
} catch { }
62+
} catch {}
6063
} else {
6164
cur = path.join(next, 'package.json')
6265
// eslint-disable-next-line no-await-in-loop
@@ -153,10 +156,11 @@ export class Plugin implements IPlugin {
153156
try {
154157
const globbyPath = require.resolve('globby', {paths: [this.root, __dirname]})
155158
globby = require(globbyPath)
156-
} catch (error) {
159+
} catch (error: any) {
157160
this.warn(error, 'not loading commands, globby not found')
158161
return []
159162
}
163+
160164
this._debug(`loading IDs from ${this.commandsDir}`)
161165
const patterns = [
162166
'**/*.+(js|cjs|mjs|ts|tsx)',
@@ -185,22 +189,25 @@ export class Plugin implements IPlugin {
185189
if (cmd.default && cmd.default.run) return cmd.default
186190
return Object.values(cmd).find((cmd: any) => typeof cmd.run === 'function')
187191
}
192+
188193
let m
189194
try {
190195
const p = path.join(this.pjson.oclif.commands as string, ...id.split(':'))
191196
const {isESM, module, filePath} = await ModuleLoader.loadWithData(this, p)
192197
this._debug(isESM ? '(import)' : '(require)', filePath)
193198
m = module
194-
} catch (error) {
199+
} catch (error: any) {
195200
if (!opts.must && error.code === 'MODULE_NOT_FOUND') return
196201
throw error
197202
}
203+
198204
const cmd = search(m)
199205
if (!cmd) return
200206
cmd.id = id
201207
cmd.plugin = this
202208
return cmd
203209
}
210+
204211
const cmd = await fetch()
205212
if (!cmd && opts.must) error(`command ${id} not found`)
206213
return cmd
@@ -217,26 +224,26 @@ export class Plugin implements IPlugin {
217224
this._debug('using manifest from', p)
218225
return manifest
219226
}
220-
} catch (error) {
227+
} catch (error: any) {
221228
if (error.code === 'ENOENT') {
222229
if (!dotfile) return readManifest(true)
223230
} else {
224231
this.warn(error, 'readManifest')
225232
}
226233
}
227234
}
235+
228236
if (!ignoreManifest) {
229237
const manifest = await readManifest()
230238
if (manifest) return manifest
231239
}
232240

233241
return {
234242
version: this.version,
235-
// eslint-disable-next-line array-callback-return
236243
commands: (await Promise.all(this.commandIDs.map(async id => {
237244
try {
238245
return [id, await toCached(await this.findCommand(id, {must: true}), this)]
239-
} catch (error) {
246+
} catch (error: any) {
240247
const scope = 'toCached'
241248
if (Boolean(errorOnManifestCreate) === false) this.warn(error, scope)
242249
else throw this.addErrorScope(error, scope)

0 commit comments

Comments
 (0)