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
36c72e9
commit 68f49fa
Showing
2 changed files
with
101 additions
and
0 deletions.
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,79 @@ | ||
import { Type } from "@sinclair/typebox"; | ||
import { expect, it } from "vitest"; | ||
import type { Options } from "yargs"; | ||
|
||
import { getStringOption } from "./string"; | ||
|
||
it("should transform TString to yargs option", () => { | ||
const schema = Type.String(); | ||
|
||
const result = getStringOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: true, | ||
}); | ||
}); | ||
|
||
it("should transform TString with truthy default value to yargs option", () => { | ||
const schema = Type.String({ default: "foo" }); | ||
|
||
const result = getStringOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
default: "foo", | ||
}); | ||
}); | ||
|
||
it("should transform TString with falsy default value to yargs option", () => { | ||
const schema = Type.String({ default: "" }); | ||
|
||
const result = getStringOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
default: "", | ||
}); | ||
}); | ||
|
||
it("should transform TString with description to yargs option", () => { | ||
const schema = Type.String({ description: "foo" }); | ||
|
||
const result = getStringOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: true, | ||
description: "foo", | ||
}); | ||
}); | ||
|
||
it("should transform TString with override to yargs option", () => { | ||
const schema = Type.String(); | ||
const overwrite: Options = { | ||
requiresArg: false, | ||
alias: "aliased", | ||
}; | ||
|
||
const result = getStringOption(schema, overwrite); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
alias: "aliased", | ||
}); | ||
}); | ||
|
||
it("should detect if it is optional", () => { | ||
const schema = Type.Optional(Type.String()); | ||
|
||
const result = getStringOption(schema); | ||
|
||
expect(result).toEqual({ | ||
type: "string", | ||
requiresArg: false, | ||
}); | ||
}); |
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,22 @@ | ||
import { type TString, TypeGuard } from "@sinclair/typebox"; | ||
import type { Options } from "yargs"; | ||
|
||
export function getStringOption( | ||
schema: TString, | ||
override: Options = {}, | ||
): Options { | ||
const hasDefaultValue = schema.default !== undefined; | ||
const options = { | ||
type: "string" as const, | ||
requiresArg: !TypeGuard.IsOptional(schema) && !hasDefaultValue, | ||
}; | ||
|
||
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; | ||
} |