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

Commit 643aea7

Browse files
committed
feat: added enum flag
1 parent 26a2e7c commit 643aea7

9 files changed

+65
-16
lines changed

.circleci/config.yml

+3-12
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ jobs:
1212
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
1313
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
1414
- v0-yarn-{{checksum ".circleci/config.yml"}}-master-
15-
- run: .circleci/setup_git
16-
- run: .circleci/yarn
17-
- run: yarn test
18-
- run: curl -s https://codecov.io/bash | bash
15+
- run: .circleci/test
1916
- store_test_results: &store_test_results
2017
path: ~/cli/reports
2118
- save_cache: &save_cache
@@ -31,21 +28,15 @@ jobs:
3128
steps:
3229
- checkout
3330
- restore_cache: *restore_cache
34-
- run: .circleci/setup_git
35-
- run: .circleci/yarn
36-
- run: yarn test
37-
- run: curl -s https://codecov.io/bash | bash
31+
- run: .circleci/test
3832
- store_test_results: *store_test_results
3933
release:
4034
<<: *test
4135
steps:
4236
- add_ssh_keys
4337
- checkout
4438
- restore_cache: *restore_cache
45-
- run: .circleci/setup_git
46-
- run: .circleci/yarn
47-
- run: yarn add -D @dxcli/semantic-release@1
48-
- run: yarn exec nps release
39+
- run: .circleci/release
4940

5041
workflows:
5142
version: 2

.circleci/release

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
.circleci/setup_git
6+
7+
PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH
8+
9+
yarn global add @dxcli/semantic-release@1 semantic-release@12
10+
yarn install --frozen-lockfile
11+
semantic-release -e @dxcli/semantic-release

.circleci/yarn .circleci/test

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -ex
44

5+
.circleci/setup_git
6+
57
PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH
68

79
CLI_ENGINE_UTIL_YARN_ARGS="--frozen-lockfile"
@@ -20,3 +22,8 @@ yarn install $CLI_ENGINE_UTIL_YARN_ARGS
2022
if [[ "$CLI_ENGINE_GREENKEEPER_BRANCH" == 1 ]]; then
2123
greenkeeper-lockfile-upload
2224
fi
25+
26+
yarn add -D nyc @dxcli/nyc-config
27+
yarn test
28+
29+
curl -s https://codecov.io/bash | bash

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ install:
1010
- git submodule update --init --recursive
1111
- git config --global user.email "[email protected]"
1212
- git config --global user.name "dxcli"
13-
- yarn
13+
- yarn add -D nyc @dxcli/nyc-config
1414
test_script:
1515
- yarn test
1616
after_test:

package-scripts.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ setColors(['dim'])
1313
const script = (script, description) => description ? {script, description} : {script}
1414

1515
const linters = {
16-
eslint: script('tsc && eslint .', 'lint js files'),
16+
eslint: script('eslint .', 'lint js files'),
1717
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
1818
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
1919
tslint: script('tslint -p test', 'lint ts files'),
@@ -49,6 +49,5 @@ module.exports = {
4949
lint: concurrent(linters),
5050
test,
5151
mocha,
52-
release: 'dxcli-semantic-release -e @dxcli/semantic-release',
5352
},
5453
}

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
"@dxcli/nyc-config": "^0.0.4",
1717
"@dxcli/tslint": "^0.1.3",
1818
"@heroku/linewrap": "^1.0.0",
19+
"@types/chai": "^4.1.2",
20+
"@types/lodash": "^4.14.98",
21+
"@types/mocha": "^2.2.47",
22+
"@types/nock": "^9.1.2",
23+
"@types/node": "^9.4.0",
24+
"@types/node-notifier": "^0.0.28",
25+
"@types/read-pkg": "^3.0.0",
1926
"chai": "^4.1.2",
2027
"eslint": "^4.16.0",
2128
"eslint-config-dxcli": "^1.2.1",

src/flags.ts

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ export const integer = build({
6262
},
6363
})
6464

65+
export interface EnumFlagOptions<T> extends Partial<IOptionFlag<T>> {
66+
options: string[]
67+
}
68+
69+
const _enum = <T = string>(opts: EnumFlagOptions<T>) => build<T>({
70+
parse(input) {
71+
if (!opts.options.includes(input)) throw new Error(`Expected --${this.name}=${input} to be one of: ${opts.options.join(', ')}`)
72+
return input
73+
},
74+
...opts as any,
75+
})
76+
export {_enum as enum}
77+
6578
export function option<T>(options: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>) {
6679
return build<T>(options)()
6780
}

test/parse.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,25 @@ See more help with --help`)
371371
expect(out.flags).to.deep.include({foo: 'bar'})
372372
})
373373
})
374+
375+
describe('enum', () => {
376+
it('accepts valid option', () => {
377+
const foo = flags.enum<'myopt' | 'myotheropt'>({options: ['myopt', 'myotheropt']})
378+
const out = parse({
379+
argv: ['--foo', 'myotheropt'],
380+
flags: {foo: foo()},
381+
})
382+
expect(out.flags.foo).to.equal('myotheropt')
383+
})
384+
385+
it('fails when invalid', () => {
386+
const foo = flags.enum({options: ['myopt', 'myotheropt']})
387+
expect(() => {
388+
parse({
389+
argv: ['--foo', 'bar'],
390+
flags: {foo: foo()},
391+
})
392+
}).to.throw('Expected --foo=bar to be one of: myopt, myotheropt')
393+
})
394+
})
374395
})

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
version "4.1.2"
155155
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.2.tgz#f1af664769cfb50af805431c407425ed619daa21"
156156

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

0 commit comments

Comments
 (0)