generated from moontaiworks/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
432082d
commit 6d1b0ee
Showing
6 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { type TLiteral, type TUnion, Type } from "@sinclair/typebox"; | ||
import { expect, it } from "vitest"; | ||
import type { Options } from "yargs"; | ||
|
||
import { getChoiceOption } from "./choice"; | ||
|
||
it("should transform TUnion to yargs option", () => { | ||
const schema = Type.Union([Type.Literal("foo"), Type.Literal("bar")]); | ||
|
||
const result = getChoiceOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: true, | ||
choices: ["foo", "bar"], | ||
}); | ||
}); | ||
|
||
it("should transform TUnion with truthy default value to yargs option", () => { | ||
const schema = Type.Union([Type.Literal("foo"), Type.Literal("bar")], { | ||
default: "foo", | ||
}); | ||
|
||
const result = getChoiceOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
choices: ["foo", "bar"], | ||
default: "foo", | ||
}); | ||
}); | ||
|
||
it("should transform TUnion with falsy default value to yargs option", () => { | ||
const schema = Type.Union([Type.Literal("foo"), Type.Literal("bar")], { | ||
default: "", | ||
}); | ||
|
||
const result = getChoiceOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
choices: ["foo", "bar"], | ||
default: "", | ||
}); | ||
}); | ||
|
||
it("should transform TUnion with description to yargs option", () => { | ||
const schema = Type.Union([Type.Literal("foo"), Type.Literal("bar")], { | ||
description: "foo", | ||
}); | ||
|
||
const result = getChoiceOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: true, | ||
choices: ["foo", "bar"], | ||
description: "foo", | ||
}); | ||
}); | ||
|
||
it("should transform TUnion with override to yargs option", () => { | ||
const schema = Type.Union([Type.Literal("foo"), Type.Literal("bar")]); | ||
const overwrite: Options = { | ||
requiresArg: false, | ||
alias: "aliased", | ||
}; | ||
|
||
const result = getChoiceOption(schema, overwrite); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
choices: ["foo", "bar"], | ||
alias: "aliased", | ||
}); | ||
}); | ||
|
||
it("should detect if it is optional", () => { | ||
const schema = Type.Optional( | ||
Type.Union([Type.Literal("foo"), Type.Literal("bar")]), | ||
); | ||
|
||
const result = getChoiceOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
choices: ["foo", "bar"], | ||
}); | ||
}); | ||
|
||
it("should throw error when union contains non-literal", () => { | ||
const schema = Type.Union([Type.String(), Type.Number()]); | ||
|
||
expect(() => | ||
getChoiceOption(schema as unknown as TUnion<TLiteral[]>), | ||
).toThrowError("Choices must contain only literal values"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type TLiteral, type TUnion, TypeGuard } from "@sinclair/typebox"; | ||
import type { Options } from "yargs"; | ||
|
||
export function getChoiceOption( | ||
schema: TUnion<TLiteral[]>, | ||
override: Options = {}, | ||
): Options { | ||
const hasDefaultValue = schema.default !== undefined; | ||
const options = { | ||
type: "string" as const, | ||
requiresArg: !TypeGuard.IsOptional(schema) && !hasDefaultValue, | ||
choices: schema.anyOf.map(item => { | ||
if (!TypeGuard.IsLiteral(item)) { | ||
throw new Error("Choices must contain only literal values"); | ||
} | ||
|
||
return item.const.toString(); | ||
}), | ||
}; | ||
|
||
if (hasDefaultValue) | ||
Object.assign(options, { default: schema.default as string }); | ||
if (schema.description) | ||
Object.assign(options, { description: schema.description }); | ||
|
||
Object.assign(options, override); | ||
|
||
return options; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./array"; | ||
export * from "./boolean"; | ||
export * from "./choice"; | ||
export * from "./get-option"; | ||
export * from "./number"; | ||
export * from "./string"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters