Skip to content

Commit 1164430

Browse files
committed
feat(core): will now coerce values to correct type
BREAKING CHANGE: execute function no longer returns a promise
1 parent ad7ebec commit 1164430

6 files changed

Lines changed: 53 additions & 5 deletions

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"typescript": "^3.0.3"
6565
},
6666
"dependencies": {
67+
"boolean": "^0.2.0",
6768
"chalk": "^2.4.1",
6869
"commander": "^2.18.0",
6970
"fs-extra": "^7.0.0",

src/coercion.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { coerceValue } from './coercion';
2+
3+
describe('src/coercion', () => {
4+
describe('coerceValue', () => {
5+
it('should coerce strings to strings', () => {
6+
expect(coerceValue('123', String)).toStrictEqual('123');
7+
});
8+
9+
it('should coerce strings to strings', () => {
10+
expect(coerceValue('123', Number)).toStrictEqual(123);
11+
expect(coerceValue('123', Number)).not.toStrictEqual('123');
12+
});
13+
14+
it('should coerce strings to strings', () => {
15+
expect(coerceValue('true', Boolean)).toStrictEqual(true);
16+
expect(coerceValue('123', Boolean)).not.toStrictEqual('123');
17+
expect(coerceValue('123', Boolean)).not.toStrictEqual(123);
18+
});
19+
});
20+
});

src/coercion.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as parseBoolean from 'boolean';
2+
3+
export function coerceValue(value: string, toType: typeof String): string;
4+
export function coerceValue(value: string, toType: typeof Number): number;
5+
export function coerceValue(value: string, toType: typeof Boolean): boolean;
6+
7+
export function coerceValue(
8+
value: string,
9+
toType: typeof String | typeof Number | typeof Boolean,
10+
): string | number | boolean {
11+
if (toType === Number) {
12+
return +value;
13+
} else if (toType === Boolean) {
14+
return parseBoolean(value);
15+
} else {
16+
return value;
17+
}
18+
}

src/commander.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import chalk from 'chalk';
22
import * as cli from 'commander';
3+
import { coerceValue } from './coercion';
34
import { errorToString } from './errors';
45
import { getCommandOptions, getCommandValues } from './metadata';
56
import { Command, CommandClass, CommandDefinition, CommandOptionDefinition, IocContainer } from './types';
@@ -39,11 +40,14 @@ function getParams(
3940
const values = getCommandValues(command.paramsClass.prototype);
4041
const options = getCommandOptions(command.paramsClass.prototype);
4142

42-
const params: { [paramName: string]: any } = new command.paramsClass();
43+
const params: { [paramName: string]: string | number | boolean } = new command.paramsClass();
4344
let paramIndex = 0;
4445

4546
for (const value of values) {
46-
params[value.name] = args[paramIndex++];
47+
params[value.name] = coerceValue(
48+
args[paramIndex++],
49+
value.type as any // Not sure why TS complains here without cast to any 🤔
50+
);
4751
}
4852

4953
const optionValues = args[paramIndex];
@@ -67,7 +71,7 @@ function registerCommandOption(
6771
option: CommandOptionDefinition
6872
) {
6973
const optionUsage = getOptionUsage(option);
70-
const coerceValue = !option.valueName ? undefined : ((value: string) => {
74+
const coercedValue = !option.valueName ? undefined : ((value: string) => {
7175
if (option.valueName && option.type === Number) {
7276
return +value;
7377
} else {
@@ -81,7 +85,7 @@ function registerCommandOption(
8185
cliCommand.option(
8286
optionUsage,
8387
option.description,
84-
coerceValue,
88+
coercedValue,
8589
defaultValue
8690
);
8791
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function commandsFromDirectory(directoryPath: string): Promise<Comm
2828
return commander.commandsFromDirectory(directoryPath);
2929
}
3030

31-
export async function execute(argv?: string[]): Promise<void> {
31+
export function execute(argv?: string[]) {
3232
return commander.execute(argv);
3333
}
3434

0 commit comments

Comments
 (0)