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

Commit

Permalink
feat: added enum flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 30, 2018
1 parent 26a2e7c commit 643aea7
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 16 deletions.
15 changes: 3 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ jobs:
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
- v0-yarn-{{checksum ".circleci/config.yml"}}-master-
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn test
- run: curl -s https://codecov.io/bash | bash
- run: .circleci/test
- store_test_results: &store_test_results
path: ~/cli/reports
- save_cache: &save_cache
Expand All @@ -31,21 +28,15 @@ jobs:
steps:
- checkout
- restore_cache: *restore_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn test
- run: curl -s https://codecov.io/bash | bash
- run: .circleci/test
- store_test_results: *store_test_results
release:
<<: *test
steps:
- add_ssh_keys
- checkout
- restore_cache: *restore_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn add -D @dxcli/semantic-release@1
- run: yarn exec nps release
- run: .circleci/release

workflows:
version: 2
Expand Down
11 changes: 11 additions & 0 deletions .circleci/release
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -ex

.circleci/setup_git

PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH

yarn global add @dxcli/semantic-release@1 semantic-release@12
yarn install --frozen-lockfile
semantic-release -e @dxcli/semantic-release
7 changes: 7 additions & 0 deletions .circleci/yarn → .circleci/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -ex

.circleci/setup_git

PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH

CLI_ENGINE_UTIL_YARN_ARGS="--frozen-lockfile"
Expand All @@ -20,3 +22,8 @@ yarn install $CLI_ENGINE_UTIL_YARN_ARGS
if [[ "$CLI_ENGINE_GREENKEEPER_BRANCH" == 1 ]]; then
greenkeeper-lockfile-upload
fi

yarn add -D nyc @dxcli/nyc-config
yarn test

curl -s https://codecov.io/bash | bash
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
- git submodule update --init --recursive
- git config --global user.email "dxcli@example.com"
- git config --global user.name "dxcli"
- yarn
- yarn add -D nyc @dxcli/nyc-config
test_script:
- yarn test
after_test:
Expand Down
3 changes: 1 addition & 2 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setColors(['dim'])
const script = (script, description) => description ? {script, description} : {script}

const linters = {
eslint: script('tsc && eslint .', 'lint js files'),
eslint: script('eslint .', 'lint js files'),
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
tslint: script('tslint -p test', 'lint ts files'),
Expand Down Expand Up @@ -49,6 +49,5 @@ module.exports = {
lint: concurrent(linters),
test,
mocha,
release: 'dxcli-semantic-release -e @dxcli/semantic-release',
},
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"@dxcli/nyc-config": "^0.0.4",
"@dxcli/tslint": "^0.1.3",
"@heroku/linewrap": "^1.0.0",
"@types/chai": "^4.1.2",
"@types/lodash": "^4.14.98",
"@types/mocha": "^2.2.47",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/node-notifier": "^0.0.28",
"@types/read-pkg": "^3.0.0",
"chai": "^4.1.2",
"eslint": "^4.16.0",
"eslint-config-dxcli": "^1.2.1",
Expand Down
13 changes: 13 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ export const integer = build({
},
})

export interface EnumFlagOptions<T> extends Partial<IOptionFlag<T>> {
options: string[]
}

const _enum = <T = string>(opts: EnumFlagOptions<T>) => build<T>({
parse(input) {
if (!opts.options.includes(input)) throw new Error(`Expected --${this.name}=${input} to be one of: ${opts.options.join(', ')}`)
return input
},
...opts as any,
})
export {_enum as enum}

export function option<T>(options: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>) {
return build<T>(options)()
}
Expand Down
21 changes: 21 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,25 @@ See more help with --help`)
expect(out.flags).to.deep.include({foo: 'bar'})
})
})

describe('enum', () => {
it('accepts valid option', () => {
const foo = flags.enum<'myopt' | 'myotheropt'>({options: ['myopt', 'myotheropt']})
const out = parse({
argv: ['--foo', 'myotheropt'],
flags: {foo: foo()},
})
expect(out.flags.foo).to.equal('myotheropt')
})

it('fails when invalid', () => {
const foo = flags.enum({options: ['myopt', 'myotheropt']})
expect(() => {
parse({
argv: ['--foo', 'bar'],
flags: {foo: foo()},
})
}).to.throw('Expected --foo=bar to be one of: myopt, myotheropt')
})
})
})
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.2.tgz#f1af664769cfb50af805431c407425ed619daa21"

"@types/lodash@^4.14.97":
"@types/lodash@^4.14.97", "@types/lodash@^4.14.98":
version "4.14.98"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.98.tgz#aaf012ae443e657e7885e605a4c1b340db160609"

Expand Down

0 comments on commit 643aea7

Please sign in to comment.