Add valid values for --log

This commit is contained in:
Asher 2020-02-19 11:06:32 -06:00
parent 0263188431
commit 0e2eaa9b34
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 23 additions and 11 deletions

View File

@ -8,6 +8,14 @@ export class Optional<T> {
public constructor(public readonly value?: T) {} public constructor(public readonly value?: T) {}
} }
export enum LogLevel {
Trace = "trace",
Debug = "debug",
Info = "info",
Warn = "warn",
Error = "error",
}
export class OptionalString extends Optional<string> {} export class OptionalString extends Optional<string> {}
export interface Args extends VsArgs { export interface Args extends VsArgs {
@ -19,6 +27,7 @@ export interface Args extends VsArgs {
readonly help?: boolean readonly help?: boolean
readonly host?: string readonly host?: string
readonly json?: boolean readonly json?: boolean
log?: LogLevel
readonly open?: boolean readonly open?: boolean
readonly port?: number readonly port?: number
readonly socket?: string readonly socket?: string
@ -49,6 +58,8 @@ type OptionType<T> = T extends boolean
? "boolean" ? "boolean"
: T extends OptionalString : T extends OptionalString
? typeof OptionalString ? typeof OptionalString
: T extends LogLevel
? typeof LogLevel
: T extends AuthType : T extends AuthType
? typeof AuthType ? typeof AuthType
: T extends number : T extends number
@ -76,7 +87,7 @@ const options: Options<Required<Args>> = {
host: { type: "string", description: "Host for the HTTP server." }, host: { type: "string", description: "Host for the HTTP server." },
help: { type: "boolean", short: "h", description: "Show this output." }, help: { type: "boolean", short: "h", description: "Show this output." },
json: { type: "boolean" }, json: { type: "boolean" },
open: { type: "boolean", description: "Open in the browser on startup. Does not work remotely." }, open: { type: "boolean", description: "Open in browser on startup. Does not work remotely." },
port: { type: "number", description: "Port for the HTTP server." }, port: { type: "number", description: "Port for the HTTP server." },
socket: { type: "string", path: true, description: "Path to a socket (host and port will be ignored)." }, socket: { type: "string", path: true, description: "Path to a socket (host and port will be ignored)." },
version: { type: "boolean", short: "v", description: "Display version information." }, version: { type: "boolean", short: "v", description: "Display version information." },
@ -91,7 +102,7 @@ const options: Options<Required<Args>> = {
"install-extension": { type: "string[]" }, "install-extension": { type: "string[]" },
"uninstall-extension": { type: "string[]" }, "uninstall-extension": { type: "string[]" },
log: { type: "string" }, log: { type: LogLevel },
verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." }, verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." },
} }
@ -207,29 +218,29 @@ export const parse = (argv: string[]): Args => {
// Ensure the environment variable and the flag are synced up. The flag takes // Ensure the environment variable and the flag are synced up. The flag takes
// priority over the environment variable. // priority over the environment variable.
if (args.log === "trace" || process.env.LOG_LEVEL === "trace" || args.verbose) { if (args.log === LogLevel.Trace || process.env.LOG_LEVEL === LogLevel.Trace || args.verbose) {
args.log = process.env.LOG_LEVEL = "trace" args.log = process.env.LOG_LEVEL = LogLevel.Trace
args.verbose = true args.verbose = true
} else if (!args.log && process.env.LOG_LEVEL) { } else if (!args.log && process.env.LOG_LEVEL) {
args.log = process.env.LOG_LEVEL args.log = process.env.LOG_LEVEL as LogLevel
} else if (args.log) { } else if (args.log) {
process.env.LOG_LEVEL = args.log process.env.LOG_LEVEL = args.log
} }
switch (args.log) { switch (args.log) {
case "trace": case LogLevel.Trace:
logger.level = Level.Trace logger.level = Level.Trace
break break
case "debug": case LogLevel.Debug:
logger.level = Level.Debug logger.level = Level.Debug
break break
case "info": case LogLevel.Info:
logger.level = Level.Info logger.level = Level.Info
break break
case "warning": case LogLevel.Warn:
logger.level = Level.Warning logger.level = Level.Warning
break break
case "error": case LogLevel.Error:
logger.level = Level.Error logger.level = Level.Error
break break
} }

View File

@ -119,9 +119,10 @@ describe("cli", () => {
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/) assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
}) })
it("should error if number option is invalid", () => { it("should error if value is invalid", () => {
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/) assert.throws(() => parse(["--port", "foo"]), /--port must be a number/)
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/) assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
assert.throws(() => parse(["--log", "invalid"]), /--log valid values: \[trace, debug, info, warn, error\]/)
}) })
it("should error if the option doesn't exist", () => { it("should error if the option doesn't exist", () => {