Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use prettier and lint-staged #798

Merged
merged 11 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
}
11 changes: 0 additions & 11 deletions .editorconfig

This file was deleted.

3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": [
"oclif",
"oclif-typescript"
"oclif-typescript",
"prettier"
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see prettier's recommendations for how to set it up. The eslint plugin is not recommended. We don't do it that way in salesforcecli, either.

https://prettier.io/docs/en/integrating-with-linters.html

"rules": {
"sort-imports": "error",
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ name: automerge
on:
workflow_dispatch:
schedule:
- cron: '17 2,5,8,11 * * *'
- cron: "17 2,5,8,11 * * *"

jobs:
automerge:
uses: oclif/github-workflows/.github/workflows/automerge.yml@main
secrets: inherit
uses: salesforcecli/github-workflows/.github/workflows/automerge.yml@main
secrets:
SVC_CLI_BOT_GITHUB_TOKEN: ${{ secrets.SVC_CLI_BOT_GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged --concurrent false
5 changes: 5 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.json": ["prettier --write"],
"*.md": ["prettier --write"],
"+(src|test)/**/*.+(ts|js)": ["eslint --fix", "prettier --write"]
}
1 change: 0 additions & 1 deletion commitlint.config.js

This file was deleted.

22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
"wrap-ansi": "^7.0.0"
},
"devDependencies": {
"@commitlint/config-conventional": "^12.1.4",
"@commitlint/config-conventional": "^17.7.0",
"@oclif/plugin-help": "^5.2.8",
"@oclif/plugin-plugins": "^3.3.0",
"@oclif/prettier-config": "^0.1.1",
"@oclif/test": "^3.0.0-beta.1",
"@oclif/prettier-config": "^0.2.1",
"@oclif/test": "^3.0.1",
"@types/ansi-styles": "^3.2.1",
"@types/benchmark": "^2.1.2",
"@types/chai": "^4.3.4",
Expand All @@ -46,8 +46,7 @@
"@types/ejs": "^3.1.2",
"@types/indent-string": "^4.0.1",
"@types/js-yaml": "^3.12.7",
"@types/mocha": "^8.2.3",
"@types/nock": "^11.1.0",
"@types/mocha": "^10.0.2",
"@types/node": "^18",
"@types/node-notifier": "^8.0.2",
"@types/slice-ansi": "^4.0.0",
Expand All @@ -58,22 +57,24 @@
"benchmark": "^2.1.4",
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"commitlint": "^12.1.4",
"commitlint": "^17.7.2",
"cross-env": "^7.0.3",
"eslint": "^8.49.0",
"eslint-config-oclif": "^5.0.0",
"eslint-config-oclif-typescript": "^2.0.1",
"fancy-test": "^3.0.0-beta.2",
"eslint-config-prettier": "^9.0.0",
"fancy-test": "^3.0.1",
"globby": "^11.1.0",
"husky": "6",
"husky": "^8",
"lint-staged": "^14.0.1",
"madge": "^6.1.0",
"mocha": "^10.2.0",
"nock": "^13.3.0",
"nyc": "^15.1.0",
"prettier": "^3.0.3",
"shx": "^0.3.4",
"sinon": "^11.1.2",
"tsd": "^0.29.0",
"ts-node": "^10.9.1",
"tsd": "^0.29.0",
"tslib": "^2.5.0",
"typescript": "^5"
},
Expand Down Expand Up @@ -107,6 +108,7 @@
"build": "shx rm -rf lib && tsc",
"commitlint": "commitlint",
"compile": "tsc",
"format": "prettier --write \"+(src|test)/**/*.+(ts|js|json)\"",
"lint": "eslint . --ext .ts",
"posttest": "yarn lint && yarn test:circular-deps",
"prepack": "yarn run build",
Expand Down
8 changes: 3 additions & 5 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {Arg, ArgDefinition} from './interfaces/parser'
import {dirExists, fileExists, isNotFalsy} from './util'
import {Command} from './command'
Expand Down Expand Up @@ -33,13 +32,12 @@ export function custom<T, P = Record<string, unknown>>(defaults: Partial<Arg<T,
}

export const boolean = custom<boolean>({
parse: async b => Boolean(b) && isNotFalsy(b),
parse: async (b) => Boolean(b) && isNotFalsy(b),
})

export const integer = custom<number, {min?: number; max?: number;}>({
export const integer = custom<number, {min?: number; max?: number}>({
async parse(input, _, opts) {
if (!/^-?\d+$/.test(input))
throw new Error(`Expected an integer but received: ${input}`)
if (!/^-?\d+$/.test(input)) throw new Error(`Expected an integer but received: ${input}`)
const num = Number.parseInt(input, 10)
if (opts.min !== undefined && num < opts.min)
throw new Error(`Expected an integer greater than or equal to ${opts.min} but received: ${input}`)
Expand Down
32 changes: 16 additions & 16 deletions src/cli-ux/action/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {castArray} from '../../util'
import {inspect} from 'node:util'

export interface ITask {
action: string;
status: string | undefined;
active: boolean;
action: string
status: string | undefined
active: boolean
}

export type ActionType = 'spinner' | 'simple' | 'debug'
Expand Down Expand Up @@ -45,8 +45,8 @@ export class ActionBase {
this._stdout(false)
}

private get globals(): { action: { task?: ITask }; output: string | undefined } {
(global as any).ux = (global as any).ux || {}
private get globals(): {action: {task?: ITask}; output: string | undefined} {
;(global as any).ux = (global as any).ux || {}
const globals = (global as any).ux
globals.action = globals.action || {}
return globals
Expand Down Expand Up @@ -201,19 +201,19 @@ export class ActionBase {
// write to the real stdout/stderr
protected _write(std: 'stdout' | 'stderr', s: string | string[]): void {
switch (std) {
case 'stdout': {
this.stdmockOrigs.stdout.apply(stdout, castArray(s) as [string])
break
}
case 'stdout': {
this.stdmockOrigs.stdout.apply(stdout, castArray(s) as [string])
break
}

case 'stderr': {
this.stdmockOrigs.stderr.apply(stderr, castArray(s) as [string])
break
}
case 'stderr': {
this.stdmockOrigs.stderr.apply(stderr, castArray(s) as [string])
break
}

default: {
throw new Error(`invalid std: ${std}`)
}
default: {
throw new Error(`invalid std: ${std}`)
}
}
}
}
15 changes: 7 additions & 8 deletions src/cli-ux/action/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {errtermwidth} from '../../screen'
import spinners from './spinners'
import stripAnsi from 'strip-ansi'

const ansiEscapes = require('ansi-escapes')

function color(s: string): string {
if (!supportsColor) return s
const has256 = supportsColor.stdout ? supportsColor.stdout.has256 : (process.env.TERM || '').includes('256')
Expand Down Expand Up @@ -34,10 +36,10 @@ export default class SpinnerAction extends ActionBase {
this._reset()
if (this.spinner) clearInterval(this.spinner)
this._render()
this.spinner = setInterval(icon =>
this._render.bind(this)(icon),
process.platform === 'win32' ? 500 : 100,
'spinner',
this.spinner = setInterval(
(icon) => this._render.bind(this)(icon),
process.platform === 'win32' ? 500 : 100,
'spinner',
)
const interval = this.spinner
interval.unref()
Expand Down Expand Up @@ -82,15 +84,12 @@ export default class SpinnerAction extends ActionBase {

private _reset() {
if (!this.output) return
const ansiEscapes = require('ansi-escapes')
const lines = this._lines(this.output)
this._write(this.std, ansiEscapes.cursorLeft + ansiEscapes.cursorUp(lines) + ansiEscapes.eraseDown)
this.output = undefined
}

private _lines(s: string): number {
return (stripAnsi(s).split('\n') as any[])
.map(l => Math.ceil(l.length / errtermwidth))
.reduce((c, i) => c + i, 0)
return (stripAnsi(s).split('\n') as any[]).map((l) => Math.ceil(l.length / errtermwidth)).reduce((c, i) => c + i, 0)
}
}
2 changes: 1 addition & 1 deletion src/cli-ux/action/spinners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default {
},
flip: {
interval: 70,
frames: ['_', '_', '_', '-', '`', '`', '\'', '´', '-', '_', '_', '_'],
frames: ['_', '_', '_', '-', '`', '`', "'", '´', '-', '_', '_', '_'],
},
hamburger: {
interval: 100,
Expand Down
4 changes: 2 additions & 2 deletions src/cli-ux/action/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import spinners from './spinners'

export type Options = {
stdout?: boolean;
style?: keyof typeof spinners;
stdout?: boolean
style?: keyof typeof spinners
}
18 changes: 9 additions & 9 deletions src/cli-ux/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import spinner from './action/spinner'
export type Levels = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'

export interface ConfigMessage {
type: 'config';
prop: string;
value: any;
type: 'config'
prop: string
value: any
}

const g: any = global
const globals = g.ux || (g.ux = {})

const actionType = (
Boolean(process.stderr.isTTY)
&& !process.env.CI
&& !['dumb', 'emacs-color'].includes(process.env.TERM!)
&& 'spinner'
) || 'simple'
const actionType =
(Boolean(process.stderr.isTTY) &&
!process.env.CI &&
!['dumb', 'emacs-color'].includes(process.env.TERM!) &&
'spinner') ||
'simple'

const Action = actionType === 'spinner' ? spinner : simple

Expand Down
2 changes: 1 addition & 1 deletion src/cli-ux/exit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class ExitError extends Error {
public ux: {
exit: number;
exit: number
}

public code: 'EEXIT'
Expand Down
4 changes: 2 additions & 2 deletions src/cli-ux/flush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {stdout} from './stream'

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

async function _flush() {
const p = new Promise(resolve => {
const p = new Promise((resolve) => {
stdout.once('drain', () => resolve(null))
})
const flushed = stdout.write('')
Expand Down
2 changes: 1 addition & 1 deletion src/cli-ux/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare namespace NodeJS {
interface Global {
ux: any;
ux: any
}
}
3 changes: 1 addition & 2 deletions src/cli-ux/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import * as Errors from '../errors'
import * as styled from './styled'
import * as uxPrompt from './prompt'
Expand Down Expand Up @@ -169,7 +168,7 @@ const uxProcessExitHandler = async () => {

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

const maxLength = maxBy(items, item => item[0].length)?.[0].length ?? 0
const lines = items.map(i => {
const maxLength = maxBy(items, (item) => item[0].length)?.[0].length ?? 0
const lines = items.map((i) => {
let left = i[0]
let right = i[1]
if (!right) {
Expand Down
Loading
Loading