From 643aea7957942b3a083052fba8ee8fd6c0ee41f1 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Tue, 30 Jan 2018 13:02:05 -0800 Subject: [PATCH] feat: added enum flag --- .circleci/config.yml | 15 +++------------ .circleci/release | 11 +++++++++++ .circleci/{yarn => test} | 7 +++++++ appveyor.yml | 2 +- package-scripts.js | 3 +-- package.json | 7 +++++++ src/flags.ts | 13 +++++++++++++ test/parse.test.ts | 21 +++++++++++++++++++++ yarn.lock | 2 +- 9 files changed, 65 insertions(+), 16 deletions(-) create mode 100755 .circleci/release rename .circleci/{yarn => test} (83%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 21dbfbb..8b71fd8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 @@ -31,10 +28,7 @@ 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 @@ -42,10 +36,7 @@ jobs: - 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 diff --git a/.circleci/release b/.circleci/release new file mode 100755 index 0000000..b79d957 --- /dev/null +++ b/.circleci/release @@ -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 diff --git a/.circleci/yarn b/.circleci/test similarity index 83% rename from .circleci/yarn rename to .circleci/test index e097b26..deb213c 100755 --- a/.circleci/yarn +++ b/.circleci/test @@ -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" @@ -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 diff --git a/appveyor.yml b/appveyor.yml index 4d7c1c2..d6e9ff4 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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: diff --git a/package-scripts.js b/package-scripts.js index 5cfe5d9..6751e89 100644 --- a/package-scripts.js +++ b/package-scripts.js @@ -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'), @@ -49,6 +49,5 @@ module.exports = { lint: concurrent(linters), test, mocha, - release: 'dxcli-semantic-release -e @dxcli/semantic-release', }, } diff --git a/package.json b/package.json index 06ed24c..f3c47f9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/flags.ts b/src/flags.ts index 80581c8..884f886 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -62,6 +62,19 @@ export const integer = build({ }, }) +export interface EnumFlagOptions extends Partial> { + options: string[] +} + +const _enum = (opts: EnumFlagOptions) => build({ + 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(options: {parse: IOptionFlag['parse']} & Partial>) { return build(options)() } diff --git a/test/parse.test.ts b/test/parse.test.ts index b0da276..a3a93ce 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -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') + }) + }) }) diff --git a/yarn.lock b/yarn.lock index b03bab0..755c278 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"