Skip to content

Commit

Permalink
feat(command): add value option for env vars (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Oct 23, 2021
1 parent 6201e28 commit e5b2b2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
5 changes: 5 additions & 0 deletions command/command.ts
Expand Up @@ -1117,12 +1117,17 @@ export class Command<
? env.names[0].replace(new RegExp(`^${env.prefix}`), "")
: env.names[0],
);

result[propertyName] = this.parseType({
label: "Environment variable",
type: env.type,
name,
value: Deno.env.get(name) ?? "",
});

if (env.value && typeof result[propertyName] !== "undefined") {
result[propertyName] = env.value(result[propertyName]);
}
} else if (env.required) {
throw new MissingRequiredEnvVar(env);
}
Expand Down
98 changes: 47 additions & 51 deletions command/test/command/env_var_test.ts
Expand Up @@ -91,7 +91,7 @@ function clearEnv() {
Deno.env.delete("baz_required");
}

Deno.test("command - env var - missing required env var", async () => {
Deno.test("[command] - env var - missing required env var", async () => {
await assertThrowsAsync(
async () => {
await new Command()
Expand All @@ -104,53 +104,49 @@ Deno.test("command - env var - missing required env var", async () => {
);
});

Deno.test("command - env var - expect valid values", async () => {
Deno.test("[command] - env var - should map the value", async () => {
setupEnv();
let called = 0;
await command()
.reset()
.action((options) => {
called++;
assertEquals(options, {
global: true,
globalPrefixed: "global",
globalHidden: "global",
globalRequired: 1,
foo: true,
fooPrefixed: "foo",
fooHidden: "foo",
fooRequired: 1,
});
})
const { options } = await new Command()
.throwErrors()
.env("foo", "...", { value: (value) => Number(value) })
.parse([]);
assertEquals(called, 1);
assertEquals(options, { foo: 1 });
clearEnv();
});

Deno.test("command - env var - override env vars with option", async () => {
Deno.test("[command] - env var - expect valid values", async () => {
setupEnv();
let called = 0;
await command()
.reset()
.action((options) => {
called++;
assertEquals(options, {
global: true,
globalPrefixed: "global2",
globalHidden: "global",
globalRequired: 1,
foo: true,
fooPrefixed: "foo",
fooHidden: "foo",
fooRequired: 1,
});
})
.parse(["--global-prefixed", "global2"]);
assertEquals(called, 1);
const { options } = await command().parse([]);
assertEquals(options, {
global: true,
globalPrefixed: "global",
globalHidden: "global",
globalRequired: 1,
foo: true,
fooPrefixed: "foo",
fooHidden: "foo",
fooRequired: 1,
});
clearEnv();
});

Deno.test("[command] - env var - override env vars with option", async () => {
setupEnv();
const { options } = await command().parse(["--global-prefixed", "global2"]);
assertEquals(options, {
global: true,
globalPrefixed: "global2",
globalHidden: "global",
globalRequired: 1,
foo: true,
fooPrefixed: "foo",
fooHidden: "foo",
fooRequired: 1,
});
clearEnv();
});

Deno.test("command - env var - expect option to throw if value is not a boolean", async () => {
Deno.test("[command] - env var - expect option to throw if value is not a boolean", async () => {
setupEnv();
Deno.env.set("global", "foo");
await assertThrowsAsync(
Expand All @@ -163,7 +159,7 @@ Deno.test("command - env var - expect option to throw if value is not a boolean"
clearEnv();
});

Deno.test("command - env var - expect option to throw if value is not a number", async () => {
Deno.test("[command] - env var - expect option to throw if value is not a number", async () => {
setupEnv();
Deno.env.set("global_required", "foo");
await assertThrowsAsync(
Expand All @@ -176,7 +172,7 @@ Deno.test("command - env var - expect option to throw if value is not a number",
clearEnv();
});

Deno.test("command - env var - expect global option to throw if value is not a boolean", async () => {
Deno.test("[command] - env var - expect global option to throw if value is not a boolean", async () => {
setupEnv();
Deno.env.set("global", "foo");
await assertThrowsAsync(
Expand All @@ -189,7 +185,7 @@ Deno.test("command - env var - expect global option to throw if value is not a b
clearEnv();
});

Deno.test("command - env var - expect global option to throw if value is not a number", async () => {
Deno.test("[command] - env var - expect global option to throw if value is not a number", async () => {
setupEnv();
Deno.env.set("global_required", "foo");
await assertThrowsAsync(
Expand All @@ -202,7 +198,7 @@ Deno.test("command - env var - expect global option to throw if value is not a n
clearEnv();
});

Deno.test("command - env var - env var properties", () => {
Deno.test("[command] - env var - env var properties", () => {
const cmd: Command = new Command()
.throwErrors()
.globalEnv("global, global2", "global ...")
Expand All @@ -228,15 +224,15 @@ Deno.test("command - env var - env var properties", () => {
assertEquals(fooEnvVar.hidden, true);
});

Deno.test("command - env var - has env vars", () => {
Deno.test("[command] - env var - has env vars", () => {
const cmd: Command = command();
assertEquals(cmd.hasEnvVars(), true);
assertEquals(cmd.hasEnvVars(true), true);
assertEquals(new Command().hasEnvVars(), false);
assertEquals(new Command().hasEnvVars(true), false);
});

Deno.test("command - env var - get env vars", () => {
Deno.test("[command] - env var - get env vars", () => {
const cmd: Command = command();
assertEquals(cmd.getEnvVars().map(env => env.name), [
"global",
Expand All @@ -258,7 +254,7 @@ Deno.test("command - env var - get env vars", () => {
]);
});

Deno.test("command - env var - get base env vars", () => {
Deno.test("[command] - env var - get base env vars", () => {
const cmd: Command = command();
assertEquals(cmd.getBaseEnvVars().map(env => env.name), [
"global",
Expand All @@ -280,7 +276,7 @@ Deno.test("command - env var - get base env vars", () => {
]);
});

Deno.test("command - env var - get global env vars", () => {
Deno.test("[command] - env var - get global env vars", () => {
const cmd: Command = command().getCommand("bar") as Command;
assertEquals(cmd.getGlobalEnvVars().map(env => env.name), [
"global",
Expand All @@ -295,7 +291,7 @@ Deno.test("command - env var - get global env vars", () => {
]);
});

Deno.test("command - env var - has env var", () => {
Deno.test("[command] - env var - has env var", () => {
const cmd: Command = command();
assertEquals(cmd.hasEnvVar("global"), true);
assertEquals(cmd.hasEnvVar("global_hidden"), false);
Expand All @@ -320,7 +316,7 @@ Deno.test("command - env var - has env var", () => {
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.hasEnvVar("unknown"), false);
});

Deno.test("command - env var - get env var", () => {
Deno.test("[command] - env var - get env var", () => {
const cmd: Command = command();
assertEquals(cmd.getEnvVar("global")?.name, "global");
assertEquals(cmd.getEnvVar("global_hidden")?.name, undefined);
Expand All @@ -345,7 +341,7 @@ Deno.test("command - env var - get env var", () => {
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getEnvVar("unknown")?.name, undefined);
});

Deno.test("command - env var - get base env var", () => {
Deno.test("[command] - env var - get base env var", () => {
const cmd: Command = command();
assertEquals(cmd.getBaseEnvVar("global")?.name, "global");
assertEquals(cmd.getBaseEnvVar("global_hidden")?.name, undefined);
Expand All @@ -370,7 +366,7 @@ Deno.test("command - env var - get base env var", () => {
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getBaseEnvVar("unknown")?.name, undefined);
});

Deno.test("command - env var - get global env var", () => {
Deno.test("[command] - env var - get global env var", () => {
const cmd: Command = command();
assertEquals(cmd.getGlobalEnvVar("global")?.name, undefined);
assertEquals(cmd.getGlobalEnvVar("global_hidden")?.name, undefined);
Expand Down
4 changes: 4 additions & 0 deletions command/types.ts
Expand Up @@ -129,12 +129,16 @@ export interface IOption<

/* ENV VARS TYPES */

// deno-lint-ignore no-explicit-any
export type IEnvVarValueHandler<T extends any = any> = (val: T) => unknown;

/** Environment variable options */
export interface IEnvVarOptions {
hidden?: boolean;
global?: boolean;
required?: boolean;
prefix?: string;
value?: IEnvVarValueHandler;
}

/** Environment variable settings. */
Expand Down

0 comments on commit e5b2b2b

Please sign in to comment.