Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: bump deps and correct/suppress lint errors (#450)
Browse files Browse the repository at this point in the history
* fix: bump deps and correct/suppress lint errors

* chore: bump lodash to 4.17.21
  • Loading branch information
peternhale authored Dec 1, 2021
1 parent 0b21159 commit bc0935c
Show file tree
Hide file tree
Showing 24 changed files with 696 additions and 258 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"oclif-typescript"
],
"rules": {
"unicorn/no-abusive-eslint-disable": 0
"unicorn/no-abusive-eslint-disable": 1,
"unicorn/prefer-node-protocol":0,
"unicorn/import-style": 1,
"unicorn/no-array-reduce": 1
}
}
6 changes: 4 additions & 2 deletions examples/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import cli from '../src'
import SimpleAction from '../src/action/simple'

const wait = (ms = 400) => new Promise(resolve => setTimeout(resolve, ms))
const wait = (ms = 400) => new Promise(resolve => {
setTimeout(resolve, ms)
})

async function run() {
cli.action.start('x foo')
await wait()
cli.log('1 log')
await wait()
// eslint-disable-next-line require-atomic-updates
cli.action.status = 'a wild status appeared!'
await wait()
cli.log('2 log')
Expand Down Expand Up @@ -53,5 +54,6 @@ async function main() {
await wait()
cli.error('oh no')
}

main()
.catch(error => require('@oclif/core/handle')(error))
5 changes: 4 additions & 1 deletion examples/confirm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {cli} from '../src'

const wait = (ms = 400) => new Promise(resolve => setTimeout(resolve, ms))
const wait = (ms = 400) => new Promise(resolve => {
setTimeout(resolve, ms)
})

async function run() {
cli.action.start('doing a thing')
Expand All @@ -9,4 +11,5 @@ async function run() {
await wait()
cli.log(`you entered: ${input}`)
}

run().catch(error => cli.error(error))
5 changes: 4 additions & 1 deletion examples/pride-spinner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import cli from '../src'

function wait() {
return new Promise(resolve => setTimeout(resolve, 1000))
return new Promise(resolve => {
setTimeout(resolve, 1000)
})
}

async function run() {
cli.prideAction.start('running')
await wait()
cli.action.stop()
}

run().catch(error => cli.error(error))
1 change: 1 addition & 0 deletions examples/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@ const main = async () => {
await customSettingExample()
await payloadValueExample()
}

main()

5 changes: 4 additions & 1 deletion examples/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {cli} from '../src'

const wait = (ms = 100) => new Promise(resolve => setTimeout(resolve, ms))
const wait = (ms = 100) => new Promise(resolve => {
setTimeout(resolve, ms)
})

async function run() {
cli.action.start('doing a thing')
Expand All @@ -25,4 +27,5 @@ async function run() {
cli.action.stop()
cli.log(`you entered: ${input}`)
}

run().catch(error => cli.error(error))
5 changes: 4 additions & 1 deletion examples/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import cli from '../src'

function wait() {
return new Promise(resolve => setTimeout(resolve, 1000))
return new Promise(resolve => {
setTimeout(resolve, 1000)
})
}

async function run() {
cli.action.start('running')
await wait()
cli.action.stop()
}

run().catch(error => cli.error(error))
4 changes: 2 additions & 2 deletions examples/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Users extends Command {
...cli.table.flags(),
}

async run() {
async run(): Promise<void> {
const {flags} = await this.parse(Users)
const {data: users} = await axios.get('https://jsonplaceholder.typicode.com/users')

Expand All @@ -33,4 +33,4 @@ export default class Users extends Command {
}
}

Users.run().then(null, err => cli.error(err))
Users.run().then(null, error => cli.error(error))
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/oclif/cli-ux/issues",
"dependencies": {
"@oclif/core": "^0.5.39",
"@oclif/core": "^1.0.4",
"@oclif/linewrap": "^1.0.0",
"@oclif/screen": "^1.0.3",
"ansi-escapes": "^4.3.0",
Expand All @@ -20,7 +20,7 @@
"indent-string": "^4.0.0",
"is-wsl": "^2.2.0",
"js-yaml": "^3.13.1",
"lodash": "^4.17.11",
"lodash": "^4.17.21",
"natural-orderby": "^2.0.1",
"object-treeify": "^1.1.4",
"password-prompt": "^1.1.2",
Expand Down Expand Up @@ -48,8 +48,8 @@
"axios": "^0.21.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^7.5.0",
"eslint-config-oclif": "^3.1.0",
"eslint": "^7.32.0",
"eslint-config-oclif": "^4.0.0",
"eslint-config-oclif-typescript": "^1.0.2",
"fancy-test": "^1.4.1",
"mocha": "^8.2.1",
Expand Down
10 changes: 9 additions & 1 deletion src/action/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class ActionBase {
if (!task) {
return
}

this._stop(msg)
task.active = false
this.task = undefined
Expand Down Expand Up @@ -82,9 +83,11 @@ export class ActionBase {
if (!task) {
return
}

if (task.status === status) {
return
}

this._updateStatus(status, task.status)
task.status = status
}
Expand All @@ -97,10 +100,12 @@ export class ActionBase {
this._stdout(false)
task.active = false
}

const ret = await fn()
if (task && active) {
this._resume()
}

return ret
}

Expand All @@ -112,10 +117,12 @@ export class ActionBase {
this._stdout(false)
task.active = false
}

const ret = fn()
if (task && active) {
this._resume()
}

return ret
}

Expand Down Expand Up @@ -175,14 +182,15 @@ export class ActionBase {
try {
let output = ''
let std: 'stdout' | 'stderr' | undefined
while (this.stdmocks && this.stdmocks.length) {
while (this.stdmocks && this.stdmocks.length > 0) {
const cur = this.stdmocks.shift() as ['stdout' | 'stderr', string[]]
std = cur[0]
this._write(std, cur[1])
output += (cur[1][0] as any).toString('utf8')
}
// add newline if there isn't one already
// otherwise we'll just overwrite it when we render

if (output && std && output[output.length - 1] !== '\n') {
this._write(std, '\n')
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/pride-spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function color(s: string, frameIndex: number): string {
]

if (!supportsColor) return s
const has256 = supportsColor.stdout.has256 || (process.env.TERM || '').indexOf('256') !== -1
const has256 = supportsColor.stdout.has256 || (process.env.TERM || '').includes('256')
const prideColor = prideColors[frameIndex] || prideColors[0]
return has256 ? prideColor(s) : chalk.magenta(s)
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const spinners = require('./spinners')

function color(s: string): string {
if (!supportsColor) return s
const has256 = supportsColor.stdout.has256 || (process.env.TERM || '').indexOf('256') !== -1
const has256 = supportsColor.stdout.has256 || (process.env.TERM || '').includes('256')
return has256 ? `\u001B[38;5;104m${s}${deps.ansiStyles.reset.open}` : chalk.magenta(s)
}

Expand Down
39 changes: 21 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ import {ExitError} from './exit'
import {IPromptOptions} from './prompt'
import * as Table from './styled/table'

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

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

async function flush() {
const p = new Promise(resolve => {
process.stdout.once('drain', () => resolve(null))
})
process.stdout.write('')
return p
}

export const ux = {
config,

Expand Down Expand Up @@ -104,24 +123,7 @@ export const ux = {
},

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

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

async function flush() {
const p = new Promise(resolve => process.stdout.once('drain', () => resolve(null)))
process.stdout.write('')
return p
}

await timeout(flush(), 10000)
await timeout(flush(), 10_000)
},
}
export default ux
Expand All @@ -145,6 +147,7 @@ const cliuxProcessExitHandler = async () => {
process.exitCode = 1
}
}

// to avoid MaxListenersExceededWarning
// only attach named listener once
const cliuxListener = process.listeners('exit').find(fn => fn.name === cliuxProcessExitHandler.name)
Expand Down
2 changes: 2 additions & 0 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export function renderList(items: IListItem[]): string {
if (items.length === 0) {
return ''
}

const maxLength = (maxBy(items, '[0].length') as any)[0].length
const lines = items.map(i => {
let left = i[0]
let right = i[1]
if (!right) {
return left
}

left = left.padEnd(maxLength)
right = linewrap(maxLength + 2, right)
return `${left} ${right}`
Expand Down
14 changes: 4 additions & 10 deletions src/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,12 @@ export default function open(target: string, opts: open.Options = {}) {
}

if (appArgs.length > 0) {
args = args.concat(appArgs)
args = [...args, ...appArgs]
}
} else {
if (opts.app) {
cmd = opts.app
} else {
// try local xdg-open
cmd = 'xdg-open'
}

cmd = opts.app ? opts.app : 'xdg-open'
if (appArgs.length > 0) {
args = args.concat(appArgs)
args = [...args, ...appArgs]
}

// if (!opts.wait) {
Expand All @@ -73,7 +67,7 @@ export default function open(target: string, opts: open.Options = {}) {

if (process.platform === 'darwin' && appArgs.length > 0) {
args.push('--args')
args = args.concat(appArgs)
args = [...args, ...appArgs]
}

const cp = childProcess.spawn(cmd, args, cpOpts)
Expand Down
3 changes: 3 additions & 0 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
}, options.timeout)
timer.unref()
}

process.stdin.setEncoding('utf8')
process.stderr.write(options.prompt)
process.stdin.resume()
Expand Down Expand Up @@ -135,6 +136,7 @@ export function confirm(message: string): Promise<boolean> {
if (['y', 'yes'].includes(response)) return true
return confirm()
}

return confirm()
}, chalk.cyan('?'))
}
Expand All @@ -149,6 +151,7 @@ export async function anykey(message?: string): Promise<void> {
`Press any key to continue or ${chalk.yellow('q')} to exit` :
`Press enter to continue or ${chalk.yellow('q')} to exit`
}

const char = await prompt(message, {type: 'single', required: false})
if (tty) process.stderr.write('\n')
if (char === 'q') Errors.error('quit')
Expand Down
1 change: 1 addition & 0 deletions src/styled/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function styledJSON(obj: any) {
cli.info(json)
return
}

const cardinal = require('cardinal')
const theme = require('cardinal/themes/jq')
cli.info(cardinal.highlight(json, {json: true, theme}))
Expand Down
4 changes: 4 additions & 0 deletions src/styled/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export default function styledObject(obj: any, keys?: string[]): string {
.map(k => k + ': ' + util.inspect(obj[k]))
.join(', ')
}

return util.inspect(obj)
}

const logKeyValue = (key: string, value: any): string => {
return `${chalk.blue(key)}:` + ' '.repeat(maxKeyLength - key.length - 1) + pp(value)
}

for (const key of keys || Object.keys(obj).sort()) {
const value = obj[key]
if (Array.isArray(value)) {
Expand All @@ -32,5 +35,6 @@ export default function styledObject(obj: any, keys?: string[]): string {
output.push(logKeyValue(key, value))
}
}

return output.join('\n')
}
Loading

0 comments on commit bc0935c

Please sign in to comment.