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

Commit

Permalink
fix: clean up requires
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Apr 6, 2018
1 parent 3eac9b4 commit 9c5fb0f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 99 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/oclif/parser/issues",
"dependencies": {
"@heroku/linewrap": "^1.0.0"
"@heroku/linewrap": "^1.0.0",
"chalk": "^2.3.2"
},
"devDependencies": {
"@oclif/errors": "^1.0.2",
"@oclif/tslint": "^1.0.2",
"@oclif/errors": "^1.0.3",
"@oclif/tslint": "^1.1.0",
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
"@types/nock": "^9.1.2",
"@types/node": "^9.6.0",
"@types/node": "^9.6.2",
"@types/node-notifier": "^0.0.28",
"@types/read-pkg": "^3.0.0",
"chai": "^4.1.2",
"chalk": "^2.3.2",
"concurrently": "^3.5.1",
"eslint": "^4.19.1",
"eslint-config-oclif": "^1.3.8",
"eslint-config-oclif": "^1.4.0",
"mocha": "^5.0.5",
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"typescript": "^2.7.2"
"typescript": "^2.8.1"
},
"engines": {
"node": ">=8.0.0"
Expand Down
26 changes: 0 additions & 26 deletions src/deps.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {CLIError} from '@oclif/errors'

import {Arg} from './args'
import {deps} from './deps'
import * as flags from './flags'
import {flagUsages} from './help'
import {renderList} from './list'
import {ParserInput, ParserOutput} from './parse'

export interface ICLIParseErrorOptions {
Expand All @@ -30,7 +30,7 @@ export class RequiredArgsError extends CLIParseError {
let message = `Missing ${args.length} required arg${args.length === 1 ? '' : 's'}`
const namedArgs = args.filter(a => a.name)
if (namedArgs.length) {
const list = deps.renderList(namedArgs.map(a => [a.name, a.description] as [string, string]))
const list = renderList(namedArgs.map(a => [a.name, a.description] as [string, string]))
message += `:\n${list}`
}
super({parse, message})
Expand All @@ -42,7 +42,7 @@ export class RequiredFlagError extends CLIParseError {
public flags: flags.IFlag<any>[]

constructor({flags, parse}: ICLIParseErrorOptions & { flags: flags.IFlag<any>[] }) {
const usage = deps.renderList(flagUsages(flags, {displayRequired: false}))
const usage = renderList(flagUsages(flags, {displayRequired: false}))
const message = `Missing required flag${flags.length === 1 ? '' : 's'}:\n${usage}`
super({parse, message})
this.flags = flags
Expand Down
5 changes: 3 additions & 2 deletions src/help.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {deps} from './deps'
import chalk from 'chalk'

import {IFlag} from './flags'
import {sortBy} from './util'

function dim(s: string): string {
if (deps.chalk) return deps.chalk.dim(s)
if (chalk) return chalk.dim(s)
return s
}

Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// tslint:disable interface-over-type-literal

import * as args from './args'
import {OutputArgs, OutputFlags, ParserOutput as Output} from './parse'
import {OutputArgs, OutputFlags, Parser, ParserOutput as Output} from './parse'
export {args}
import * as flags from './flags'
import {validate} from './validate'
export {flags}
export {flagUsages} from './help'
import {deps} from './deps'

export type Input<TFlags extends flags.Output> = {
flags?: flags.Input<TFlags>
Expand All @@ -20,17 +20,17 @@ export function parse<TFlags, TArgs extends {[name: string]: string}>(argv: stri
const input = {
argv,
context: options.context,
args: (options.args || []).map((a: any) => deps.args.newArg(a as any)),
args: (options.args || []).map((a: any) => args.newArg(a as any)),
'--': options['--'],
flags: {
color: deps.flags.defaultFlags.color,
color: flags.defaultFlags.color,
...((options.flags || {})) as any,
},
strict: options.strict !== false,
}
const parser = new deps.parse.Parser(input)
const parser = new Parser(input)
const output = parser.parse()
deps.validate.validate({input, output})
validate({input, output})
return output as any
}

Expand Down
8 changes: 4 additions & 4 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {deps} from './deps'
import {RequiredArgsError, RequiredFlagError, UnexpectedArgsError} from './errors'
import {ParserInput, ParserOutput} from './parse'

export function validate(parse: { input: ParserInput; output: ParserOutput<any, any> }) {
function validateArgs() {
const maxArgs = parse.input.args.length
if (parse.input.strict && parse.output.argv.length > maxArgs) {
const extras = parse.output.argv.slice(maxArgs)
throw new deps.errors.UnexpectedArgsError({parse, args: extras})
throw new UnexpectedArgsError({parse, args: extras})
}
const requiredArgs = parse.input.args.filter(a => a.required)
const missingRequiredArgs = requiredArgs.slice(parse.output.argv.length)
if (missingRequiredArgs.length) {
throw new deps.errors.RequiredArgsError({parse, args: missingRequiredArgs})
throw new RequiredArgsError({parse, args: missingRequiredArgs})
}
}

function validateFlags() {
const flags = Object.keys(parse.input.flags)
.map(f => parse.input.flags[f])
.filter(f => f.required && !parse.output.flags[f.name])
if (flags.length) throw new deps.errors.RequiredFlagError({parse, flags})
if (flags.length) throw new RequiredFlagError({parse, flags})
}

validateArgs()
Expand Down
104 changes: 53 additions & 51 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
version "1.0.0"
resolved "https://registry.yarnpkg.com/@heroku/linewrap/-/linewrap-1.0.0.tgz#a9d4e99f0a3e423a899b775f5f3d6747a1ff15c6"

"@oclif/errors@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.0.2.tgz#0a3f773d673a3ccd8dc26bf2e3c49580afcbe30e"
"@oclif/errors@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@oclif/errors/-/errors-1.0.3.tgz#f23c024075855c7d116d041ee158f99bd51175af"
dependencies:
clean-stack "^1.3.0"
fs-extra "^5.0.0"
indent-string "^3.2.0"
strip-ansi "^4.0.0"
wrap-ansi "^3.0.1"

"@oclif/tslint@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@oclif/tslint/-/tslint-1.0.2.tgz#793d39758082f359469dba8ce5cfba041d7a7847"
"@oclif/tslint@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@oclif/tslint/-/tslint-1.1.0.tgz#a2d494a61afa882a685fe5f4d866dafd18990728"
dependencies:
tslint-xo "^0.6.0"
tslint-xo "^0.7.0"

"@types/chai@^4.1.2":
version "4.1.2"
Expand All @@ -46,9 +46,9 @@
version "9.4.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.0.tgz#b85a0bcf1e1cc84eb4901b7e96966aedc6f078d1"

"@types/node@^9.6.0":
version "9.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.0.tgz#d3480ee666df9784b1001a1872a2f6ccefb6c2d7"
"@types/node@^9.6.2":
version "9.6.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.2.tgz#e49ac1adb458835e95ca6487bc20f916b37aff23"

"@types/normalize-package-data@*":
version "2.4.0"
Expand Down Expand Up @@ -366,7 +366,7 @@ diff@^3.1.0, diff@^3.2.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c"

doctrine@^0.7.2:
doctrine@0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-0.7.2.tgz#7cb860359ba3be90e040b26b729ce4bfa654c523"
dependencies:
Expand All @@ -390,34 +390,34 @@ eslint-ast-utils@^1.0.0:
lodash.get "^4.4.2"
lodash.zip "^4.2.0"

eslint-config-oclif@^1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/eslint-config-oclif/-/eslint-config-oclif-1.3.8.tgz#901b2b9603b0e7c1b4f028fa48a7fb5098fe2a68"
eslint-config-oclif@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/eslint-config-oclif/-/eslint-config-oclif-1.4.0.tgz#a165329e015bc3841d23bd374d70c2f96dc1078c"
dependencies:
eslint-config-xo-space "^0.17.0"
eslint-plugin-mocha "^4.11.0"
eslint-plugin-node "^6.0.0"
eslint-config-xo-space "^0.18.0"
eslint-plugin-mocha "^4.12.1"
eslint-plugin-node "^6.0.1"
eslint-plugin-unicorn "^4.0.2"

eslint-config-xo-space@^0.17.0:
version "0.17.0"
resolved "https://registry.yarnpkg.com/eslint-config-xo-space/-/eslint-config-xo-space-0.17.0.tgz#b712feb4f4547e28001900cbeecc9c2f2f456af2"
eslint-config-xo-space@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/eslint-config-xo-space/-/eslint-config-xo-space-0.18.0.tgz#92c8130b1ebaad9162bb822fdc8bc8e9f4fa5b8a"
dependencies:
eslint-config-xo "^0.18.0"
eslint-config-xo "^0.20.0"

eslint-config-xo@^0.18.0:
version "0.18.2"
resolved "https://registry.yarnpkg.com/eslint-config-xo/-/eslint-config-xo-0.18.2.tgz#0a157120875619929e735ffd6b185c41e8a187af"
eslint-config-xo@^0.20.0:
version "0.20.1"
resolved "https://registry.yarnpkg.com/eslint-config-xo/-/eslint-config-xo-0.20.1.tgz#ad04db35e62bacedcf7b7e8a76388364a78d616d"

eslint-plugin-mocha@^4.11.0:
version "4.11.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-mocha/-/eslint-plugin-mocha-4.11.0.tgz#91193a2f55e20a5e35974054a0089d30198ee578"
eslint-plugin-mocha@^4.12.1:
version "4.12.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-mocha/-/eslint-plugin-mocha-4.12.1.tgz#dbacc543b178b4536ec5b19d7f8e8864d85404bf"
dependencies:
ramda "^0.24.1"
ramda "^0.25.0"

eslint-plugin-node@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-6.0.0.tgz#5ad5ee6b5346aec6cc9cde0b8619caed2c6d8f25"
eslint-plugin-node@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-node/-/eslint-plugin-node-6.0.1.tgz#bf19642298064379315d7a4b2a75937376fa05e4"
dependencies:
ignore "^3.3.6"
minimatch "^3.0.4"
Expand Down Expand Up @@ -944,9 +944,9 @@ pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

ramda@^0.24.1:
version "0.24.1"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.24.1.tgz#c3b7755197f35b8dc3502228262c4c91ddb6b857"
ramda@^0.25.0:
version "0.25.0"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.25.0.tgz#8fdf68231cffa90bc2f9460390a0cb74a29b29a9"

readable-stream@^2.2.2:
version "2.3.3"
Expand Down Expand Up @@ -1179,7 +1179,7 @@ ts-node@^5.0.1:
source-map-support "^0.5.3"
yn "^2.0.0"

tslib@^1.0.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1:
tslib@1.9.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1:
version "1.9.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8"

Expand All @@ -1190,26 +1190,26 @@ tslint-consistent-codestyle@^1.11.0:
tslib "^1.7.1"
tsutils "^2.12.2"

tslint-eslint-rules@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/tslint-eslint-rules/-/tslint-eslint-rules-4.1.1.tgz#7c30e7882f26bc276bff91d2384975c69daf88ba"
tslint-eslint-rules@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/tslint-eslint-rules/-/tslint-eslint-rules-5.1.0.tgz#3232b318da55dbb5a83e3f5d657c1ddbb27b9ff2"
dependencies:
doctrine "^0.7.2"
tslib "^1.0.0"
tsutils "^1.4.0"
doctrine "0.7.2"
tslib "1.9.0"
tsutils "2.8.0"

tslint-microsoft-contrib@^5.0.2:
version "5.0.2"
resolved "https://registry.yarnpkg.com/tslint-microsoft-contrib/-/tslint-microsoft-contrib-5.0.2.tgz#ecc2a797f777a12f0066944cec0c81a9e7c59ee9"
dependencies:
tsutils "^2.12.1"

tslint-xo@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tslint-xo/-/tslint-xo-0.6.0.tgz#95a05b8dcac7aaa1f4d6ca1397a3c4c45a8b848e"
tslint-xo@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/tslint-xo/-/tslint-xo-0.7.0.tgz#be5a1d67f8ade5d92aa8c0a21d9cfdcbaf06f3e0"
dependencies:
tslint-consistent-codestyle "^1.11.0"
tslint-eslint-rules "^4.1.1"
tslint-eslint-rules "^5.1.0"
tslint-microsoft-contrib "^5.0.2"

tslint@^5.9.1:
Expand All @@ -1229,9 +1229,11 @@ tslint@^5.9.1:
tslib "^1.8.0"
tsutils "^2.12.1"

tsutils@^1.4.0:
version "1.9.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.9.1.tgz#b9f9ab44e55af9681831d5f28d0aeeaf5c750cb0"
tsutils@2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.8.0.tgz#0160173729b3bf138628dd14a1537e00851d814a"
dependencies:
tslib "^1.7.1"

tsutils@^2.12.1, tsutils@^2.12.2:
version "2.19.1"
Expand All @@ -1253,9 +1255,9 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^2.7.2:
version "2.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"
typescript@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.1.tgz#6160e4f8f195d5ba81d4876f9c0cc1fbc0820624"

universalify@^0.1.0:
version "0.1.1"
Expand Down

0 comments on commit 9c5fb0f

Please sign in to comment.