Skip to content

Commit

Permalink
fix: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Aug 21, 2024
1 parent 4a43b08 commit 360e521
Show file tree
Hide file tree
Showing 22 changed files with 597 additions and 72 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"@types/wordwrap": "^1.0.3",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"eslint": "^8.57.0",
"eslint-config-oclif": "^5.2.1",
"eslint-config-oclif-typescript": "^3.1.9",
"eslint-config-prettier": "^9.1.0",
"jest": "^29.7.0",
"oclif": "^4.14.22",
"semantic-release": "^24.1.0",
Expand Down
505 changes: 504 additions & 1 deletion pnpm-lock.yaml

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions src/action/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/non-nullable-type-assertion-style */
/* eslint-disable @typescript-eslint/unbound-method */
import { inspect } from 'node:util'

import { castArray } from '../util'
import { Options } from './types'
import type { Options } from './types'

export interface ITask {
action: string
Expand All @@ -15,7 +18,7 @@ export type ActionType = 'debug' | 'simple' | 'spinner'
export class ActionBase {
std: 'stderr' | 'stdout' = 'stderr'

protected stdmocks?: ['stderr' | 'stdout', string[]][]
protected stdmocks?: Array<['stderr' | 'stdout', string[]]>

type!: ActionType

Expand Down Expand Up @@ -62,9 +65,9 @@ export class ActionBase {
this.globals.action.task = task
}

public pause(fn: () => any, icon?: string): Promise<any> {
public async pause(fn: () => any, icon?: string): Promise<any> {
const { task } = this
const active = task && task.active
const active = task?.active
if (task && active) {
this._pause(icon)
this._stdout(false)
Expand All @@ -81,7 +84,7 @@ export class ActionBase {

public async pauseAsync<T>(fn: () => Promise<T>, icon?: string): Promise<T> {
const { task } = this
const active = task && task.active
const active = task?.active
if (task && active) {
this._pause(icon)
this._stdout(false)
Expand All @@ -98,7 +101,7 @@ export class ActionBase {

public start(action: string, status?: string, opts: Options = {}): void {
this.std = opts.stdout ? 'stdout' : 'stderr'
const task = { action, active: Boolean(this.task && this.task.active), status }
const task = { action, active: Boolean(this.task?.active), status }
this.task = task

this._start(opts)
Expand Down
6 changes: 3 additions & 3 deletions src/action/simple.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ActionBase, ActionType } from './base'
import { ActionBase, type ActionType } from './base'

export default class SimpleAction extends ActionBase {
public type: ActionType = 'simple'

private _flush() {
private _flush(): void {
this._write(this.std, '\n')
this._flushStdout()
}
Expand All @@ -13,7 +13,7 @@ export default class SimpleAction extends ActionBase {
else this._flush()
}

private _render(action: string, status?: string) {
private _render(action: string, status?: string): void {
if (!this.task) return
if (this.task.active) this._flush()
this._write(this.std, status ? `${action}... ${status}` : `${action}...`)
Expand Down
13 changes: 7 additions & 6 deletions src/action/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import ansiStyles from 'ansi-styles'
import chalk from 'chalk'
import stripAnsi from 'strip-ansi'
import * as supportsColor from 'supports-color'

import { errtermwidth } from '../screen'
import { ActionBase, ActionType } from './base'
import { ActionBase, type ActionType } from './base'
import spinners from './spinners'
import { Options } from './types'
import type { Options } from './types'

const ansiEscapes = require('ansi-escapes')

Expand Down Expand Up @@ -48,7 +49,7 @@ export default class SpinnerAction extends ActionBase {
this.output = undefined
}

private _render(icon?: string) {
private _render(icon?: string): void {
if (!this.task) return
this._reset()
this._flushStdout()
Expand All @@ -59,7 +60,7 @@ export default class SpinnerAction extends ActionBase {
this._write(this.std, this.output)
}

private _reset() {
private _reset(): void {
if (!this.output) return
const lines = this._lines(this.output)
this._write(this.std, ansiEscapes.cursorLeft + ansiEscapes.cursorUp(lines) + ansiEscapes.eraseDown)
Expand All @@ -73,7 +74,7 @@ export default class SpinnerAction extends ActionBase {
if (this.spinner) clearInterval(this.spinner)
this._render()
this.spinner = setInterval(
(icon) => this._render.bind(this)(icon),
(icon) => { this._render.bind(this)(icon); },
process.platform === 'win32' ? 500 : 100,
'spinner',
)
Expand All @@ -88,7 +89,7 @@ export default class SpinnerAction extends ActionBase {
this.output = undefined
}

private getFrames(opts?: Options) {
private getFrames(opts?: Options): string[] {
if (opts?.style) return spinners[opts.style].frames

return spinners[process.platform === 'win32' ? 'line' : 'dots2'].frames
Expand Down
2 changes: 1 addition & 1 deletion src/action/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import spinners from './spinners'
import type spinners from './spinners'

export type Options = {
stdout?: boolean
Expand Down
5 changes: 3 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Cache from '@oclif/core/lib/cache'
import { ActionBase } from './action/base'
import type { ActionBase } from './action/base'
import simple from './action/simple'
import spinner from './action/spinner'

Expand All @@ -17,6 +17,7 @@ const globals = g.ux || (g.ux = {})
const actionType =
(Boolean(process.stderr.isTTY) &&
!process.env.CI &&
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
!['dumb', 'emacs-color'].includes(process.env.TERM!) &&
'spinner') ||
'simple'
Expand Down Expand Up @@ -50,7 +51,7 @@ export class Config {
}
}

function fetch() {
function fetch(): any {
const core = Cache.getInstance().get('@oclif/core')
const major = core?.version.split('.')[0] || 'unknown'
if (globals[major]) return globals[major]
Expand Down
10 changes: 5 additions & 5 deletions src/flush.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { error } from "@oclif/core/lib/errors"


function timeout(p: Promise<any>, ms: number) {
function wait(ms: number, unref = false) {
async function timeout(p: Promise<any>, ms: number): Promise<any> {
async function wait(ms: number, unref = false): Promise<any> {
return new Promise((resolve) => {
const t: any = setTimeout(() => resolve(null), ms)
const t: any = setTimeout(() => { resolve(null); }, ms)
if (unref) t.unref()
})
}

return Promise.race([p, wait(ms, true).then(() => error('timed out'))])
}

async function _flush() {
async function _flush(): Promise<any> {
const p = new Promise((resolve) => {
process.stdout.once('drain', () => resolve(null))
process.stdout.once('drain', () => { resolve(null); })
})
const flushed = process.stdout.write('')

Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import chalk from 'chalk'
import { format as utilFormat } from 'node:util'

import { ActionBase } from './action/base'
import type { ActionBase } from './action/base'
import { config } from './config'
import { flush as _flush } from './flush'
import * as uxPrompt from './prompt'
Expand All @@ -11,6 +13,7 @@ import write from './write'
import { Errors } from '@oclif/core'
const hyperlinker = require('hyperlinker')

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class ux {
public static config = config

Expand Down Expand Up @@ -172,7 +175,7 @@ export {
warn
}

const uxProcessExitHandler = async () => {
const uxProcessExitHandler = async (): Promise<void> => {
try {
await ux.done()
} catch (error) {
Expand All @@ -185,13 +188,14 @@ const uxProcessExitHandler = async () => {
// only attach named listener once
const uxListener = process.listeners('exit').find((fn) => fn.name === uxProcessExitHandler.name)
if (!uxListener) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
process.once('exit', uxProcessExitHandler)
}

export { ActionBase } from './action/base'
export { Config, config } from './config'
export { ExitError } from './exit'
export { IPromptOptions } from './prompt'
export type { IPromptOptions } from './prompt'
export { Table } from './styled'

export { colorize } from './theme'
Expand Down
12 changes: 7 additions & 5 deletions src/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/unbound-method */
import chalk from 'chalk'

import { config } from './config'
Expand Down Expand Up @@ -25,7 +26,7 @@ interface IPromptConfig {
type: 'hide' | 'mask' | 'normal' | 'single'
}

function normal(options: IPromptConfig, retries = 100): Promise<string> {
async function normal(options: IPromptConfig, retries = 100): Promise<string> {
if (retries < 0) throw new Error('no input')
return new Promise((resolve, reject) => {
let timer: NodeJS.Timeout
Expand All @@ -47,13 +48,14 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
if (!options.default && options.required && data === '') {
resolve(normal(options, retries - 1))
} else {
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
resolve(data || (options.default as string))
}
})
})
}

function getPrompt(name: string, type?: string, defaultValue?: string) {
function getPrompt(name: string, type?: string, defaultValue?: string): string {
let prompt = '> '

if (defaultValue && type === 'hide') {
Expand All @@ -75,7 +77,7 @@ async function single(options: IPromptConfig): Promise<string> {
return response
}

function replacePrompt(prompt: string) {
function replacePrompt(prompt: string): void {
const ansiEscapes = require('ansi-escapes')
process.stderr.write(
ansiEscapes.cursorHide +
Expand Down Expand Up @@ -142,15 +144,15 @@ async function _prompt(name: string, inputOptions: Partial<IPromptOptions> = {})
* @returns Promise<string>
*/
export async function prompt(name: string, options: IPromptOptions = {}): Promise<string> {
return config.action.pauseAsync(() => _prompt(name, options), chalk.cyan('?'))
return config.action.pauseAsync(async () => _prompt(name, options), chalk.cyan('?'))
}

/**
* confirmation prompt (yes/no)
* @param message - confirmation text
* @returns Promise<boolean>
*/
export function confirm(message: string): Promise<boolean> {
export async function confirm(message: string): Promise<boolean> {
return config.action.pauseAsync(async () => {
const confirm = async (): Promise<boolean> => {
const raw = await _prompt(message)
Expand Down
1 change: 1 addition & 0 deletions src/screen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */

function termwidth(stream: any): number {
if (!stream.isTTY) {
Expand Down
5 changes: 3 additions & 2 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ class Stream {
}

public emit(event: string, ...args: any[]): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return process[this.channel].emit(event, ...args)
}

public getWindowSize(): number[] {
return process[this.channel].getWindowSize()
}

public on(event: string, listener: (...args: any[]) => void): Stream {
public on(event: string, listener: (...args: any[]) => void): this {
process[this.channel].on(event, listener)
return this
}

public once(event: string, listener: (...args: any[]) => void): Stream {
public once(event: string, listener: (...args: any[]) => void): this {
process[this.channel].once(event, listener)
return this
}
Expand Down
8 changes: 4 additions & 4 deletions src/styled/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { inspect } from 'node:util'

export default function styledObject(obj: any, keys?: string[]): string {
const output: string[] = []
const keyLengths = Object.keys(obj).map((key) => key.toString().length)
const keyLengths = Object.keys(obj as object).map((key) => key.toString().length)
const maxKeyLength = Math.max(...keyLengths) + 2
function pp(obj: any) {
function pp(obj: any): any {
if (typeof obj === 'string' || typeof obj === 'number') return obj
if (typeof obj === 'object') {
return Object.keys(obj)
return Object.keys(obj as object)
.map((k) => k + ': ' + inspect(obj[k]))
.join(', ')
}
Expand All @@ -20,7 +20,7 @@ export default function styledObject(obj: any, keys?: string[]): string {
const logKeyValue = (key: string, value: any): string =>
`${chalk.blue(key)}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value)

for (const key of keys || Object.keys(obj).sort()) {
for (const key of keys || Object.keys(obj as object).sort()) {
const value = obj[key]
if (Array.isArray(value)) {
if (value.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/styled/progress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 3pp
import { Options, SingleBar } from 'cli-progress'
import { type Options, SingleBar } from 'cli-progress'


export default function progress(options: Options = {}): SingleBar {
Expand Down
Loading

0 comments on commit 360e521

Please sign in to comment.