Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
chore: tslint -> eslint (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Nov 22, 2019
1 parent b083f9a commit 00ae12f
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 389 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"extends": "oclif"
"extends": [
"oclif",
"oclif-typescript"
],
"rules": {
"unicorn/no-abusive-eslint-disable": "off"
}
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"devDependencies": {
"@oclif/errors": "^1.2.2",
"@oclif/tslint": "^3.1.1",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"@types/nock": "^9.3.1",
Expand All @@ -20,11 +19,11 @@
"@types/read-pkg": "^4.0.0",
"chai": "^4.2.0",
"conventional-changelog-cli": "^2.0.17",
"eslint": "^5.16.0",
"eslint": "^6.6.0",
"eslint-config-oclif": "^3.1.0",
"eslint-config-oclif-typescript": "^0.1.0",
"mocha": "^6.1.4",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
},
"engines": {
Expand All @@ -42,11 +41,12 @@
"repository": "oclif/parser",
"scripts": {
"build": "rm -rf lib && tsc",
"lint": "yarn run build && eslint . && tsc -p test --noEmit && tslint -p test",
"posttest": "yarn run lint",
"lint": "tsc -p test --noEmit && eslint . --ext .ts --config .eslintrc",
"posttest": "yarn lint",
"prepublishOnly": "yarn run build",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
"pretest": "yarn run build"
},
"types": "lib/index.d.ts"
}
39 changes: 20 additions & 19 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
export type ParseFn<T> = (input: string) => T

// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IArg<T = string> {
name: string
description?: string
required?: boolean
hidden?: boolean
parse?: ParseFn<T>
default?: T | (() => T)
options?: string[]
name: string;
description?: string;
required?: boolean;
hidden?: boolean;
parse?: ParseFn<T>;
default?: T | (() => T);
options?: string[];
}

export interface ArgBase<T> {
name?: string
description?: string
hidden?: boolean
parse: ParseFn<T>
default?: T | (() => T)
input?: string
options?: string[]
name?: string;
description?: string;
hidden?: boolean;
parse: ParseFn<T>;
default?: T | (() => T);
input?: string;
options?: string[];
}

export type RequiredArg<T> = ArgBase<T> & {
required: true
value: T
required: true;
value: T;
}

export type OptionalArg<T> = ArgBase<T> & {
required: false
value?: T
required: false;
value?: T;
}

export type Arg<T> = RequiredArg<T> | OptionalArg<T>
Expand All @@ -38,7 +39,7 @@ export function newArg(arg: IArg<any>): any {
return {
parse: (i: string) => i,
...arg,
required: !!arg.required,
required: Boolean(arg.required),
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export default () => {
add<T, K extends string, U>(this: T, name: K, fn: () => U): T & {[P in K]: U} {
Object.defineProperty(this, name, {
enumerable: true,
get: () => cache[name] || (cache[name] = fn()),
get: () => {
cache[name] = cache[name] || fn()
return cache[name]
},
})
return this as any
},
Expand Down
14 changes: 9 additions & 5 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import {ParserInput, ParserOutput} from './parse'

export {CLIError} from '@oclif/errors'

// eslint-disable-next-line new-cap
const m = Deps()
// eslint-disable-next-line node/no-missing-require
.add('help', () => require('./help') as typeof Help)
// eslint-disable-next-line node/no-missing-require
.add('list', () => require('./list') as typeof List)

// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface ICLIParseErrorOptions {
parse: {
input?: ParserInput
output?: ParserOutput<any, any>
}
input?: ParserInput;
output?: ParserOutput<any, any>;
};
}

export class CLIParseError extends CLIError {
Expand All @@ -36,7 +40,7 @@ export class InvalidArgsSpecError extends CLIParseError {
constructor({args, parse}: ICLIParseErrorOptions & { args: Arg<any>[] }) {
let message = 'Invalid argument spec'
const namedArgs = args.filter(a => a.name)
if (namedArgs.length) {
if (namedArgs.length > 0) {
const list = m.list.renderList(namedArgs.map(a => [`${a.name} (${a.required ? 'required' : 'optional'})`, a.description] as [string, string]))
message += `:\n${list}`
}
Expand All @@ -51,7 +55,7 @@ export class RequiredArgsError extends CLIParseError {
constructor({args, parse}: ICLIParseErrorOptions & { args: Arg<any>[] }) {
let message = `Missing ${args.length} required arg${args.length === 1 ? '' : 's'}`
const namedArgs = args.filter(a => a.name)
if (namedArgs.length) {
if (namedArgs.length > 0) {
const list = m.list.renderList(namedArgs.map(a => [a.name, a.description] as [string, string]))
message += `:\n${list}`
}
Expand Down
52 changes: 26 additions & 26 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ export type DefaultContext<T> = { options: IOptionFlag<T>; flags: { [k: string]:
export type Default<T> = T | ((context: DefaultContext<T>) => T)

export type IFlagBase<T, I> = {
name: string
char?: AlphabetLowercase | AlphabetUppercase
description?: string
helpLabel?: string
hidden?: boolean
required?: boolean
dependsOn?: string[],
exclusive?: string[],
name: string;
char?: AlphabetLowercase | AlphabetUppercase;
description?: string;
helpLabel?: string;
hidden?: boolean;
required?: boolean;
dependsOn?: string[];
exclusive?: string[];
/**
* also accept an environment variable as input
*/
env?: string
parse(input: I, context: any): T
env?: string;
parse(input: I, context: any): T;
}

export type IBooleanFlag<T> = IFlagBase<T, boolean> & {
type: 'boolean'
allowNo: boolean
type: 'boolean';
allowNo: boolean;
/**
* specifying a default of false is the same not specifying a default
*/
default?: Default<boolean>
default?: Default<boolean>;
}

export type IOptionFlag<T> = IFlagBase<T, string> & {
type: 'option'
helpValue?: string
default?: Default<T | undefined>
multiple: boolean
input: string[]
options?: string[]
type: 'option';
helpValue?: string;
default?: Default<T | undefined>;
multiple: boolean;
input: string[];
options?: string[];
}

export type Definition<T> = {
(options: {multiple: true} & Partial<IOptionFlag<T[]>>): IOptionFlag<T[]>
(options: ({required: true} | {default: Default<T>}) & Partial<IOptionFlag<T>>): IOptionFlag<T>
(options?: Partial<IOptionFlag<T>>): IOptionFlag<T | undefined>
(options: {multiple: true} & Partial<IOptionFlag<T[]>>): IOptionFlag<T[]>;
(options: ({required: true} | {default: Default<T>}) & Partial<IOptionFlag<T>>): IOptionFlag<T>;
(options?: Partial<IOptionFlag<T>>): IOptionFlag<T | undefined>;
}

export type EnumFlagOptions<T> = Partial<IOptionFlag<T>> & {
options: string[]
options: string[];
}

export type IFlag<T> = IBooleanFlag<T> | IOptionFlag<T>
Expand All @@ -61,7 +61,7 @@ export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
...defaults,
...options,
input: [] as string[],
multiple: !!options.multiple,
multiple: Boolean(options.multiple),
type: 'option',
} as any
}
Expand All @@ -71,14 +71,14 @@ export function boolean<T = boolean>(options: Partial<IBooleanFlag<T>> = {}): IB
return {
parse: (b, _) => b,
...options,
allowNo: !!options.allowNo,
allowNo: Boolean(options.allowNo),
type: 'boolean',
} as IBooleanFlag<T>
}

export const integer = build({
parse: input => {
if (!/^-?[0-9]+$/.test(input)) throw new Error(`Expected an integer but received: ${input}`)
if (!/^-?\d+$/.test(input)) throw new Error(`Expected an integer but received: ${input}`)
return parseInt(input, 10)
},
})
Expand Down
6 changes: 4 additions & 2 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import Deps from './deps'
import {IFlag} from './flags'
import * as Util from './util'

// eslint-disable-next-line new-cap
const m = Deps()
.add('chalk', () => require('chalk') as typeof Chalk)
// eslint-disable-next-line node/no-missing-require
.add('util', () => require('./util') as typeof Util)

export interface FlagUsageOptions { displayRequired?: boolean }
Expand All @@ -29,8 +31,8 @@ export function flagUsage(flag: IFlag<any>, options: FlagUsageOptions = {}): [st
}

export function flagUsages(flags: IFlag<any>[], options: FlagUsageOptions = {}): [string, string | undefined][] {
if (!flags.length) return []
if (flags.length === 0) return []
const {sortBy} = m.util
return sortBy(flags, f => [f.char ? -1 : 1, f.char, f.name])
.map(f => flagUsage(f, options))
.map(f => flagUsage(f, options))
}
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ export {args}
export {flags}
export {flagUsages} from './help'

// eslint-disable-next-line new-cap
const m = Deps()
// eslint-disable-next-line node/no-missing-require
.add('validate', () => require('./validate').validate as typeof Validate.validate)

export type Input<TFlags extends flags.Output> = {
flags?: flags.Input<TFlags>
args?: args.Input
strict?: boolean
context?: any
'--'?: boolean
flags?: flags.Input<TFlags>;
args?: args.Input;
strict?: boolean;
context?: any;
'--'?: boolean;
}

export function parse<TFlags, TArgs extends {[name: string]: string}>(argv: string[], options: Input<TFlags>): Output<TFlags, TArgs> {
Expand Down
4 changes: 2 additions & 2 deletions src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Metadata = {
flags: { [key: string]: MetadataFlag }
flags: { [key: string]: MetadataFlag };
}

type MetadataFlag = {
setFromDefault?: boolean
setFromDefault?: boolean;
}
Loading

0 comments on commit 00ae12f

Please sign in to comment.