summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
authorValentin Anger <syrupthinker@gryphno.de>2020-04-29 20:48:19 +0200
committerGitHub <noreply@github.com>2020-04-29 14:48:19 -0400
commit721a4ad59d4a8bdd8470d6b98839137f14c84ba9 (patch)
treea8a7f7810a92c366224564b62b5fe7acab717466 /cli/js
parent17cf2ecdacea2254c06374866c4e7e83e282226d (diff)
BREAKING: Map-like interface for Deno.env (#4942)
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/lib.deno.ns.d.ts50
-rw-r--r--cli/js/ops/os.ts23
-rw-r--r--cli/js/tests/os_test.ts24
3 files changed, 46 insertions, 51 deletions
diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts
index edc9668e7..e58ec0b55 100644
--- a/cli/js/lib.deno.ns.d.ts
+++ b/cli/js/lib.deno.ns.d.ts
@@ -116,29 +116,35 @@ declare namespace Deno {
*/
export function exit(code?: number): never;
- /** Returns a snapshot of the environment variables at invocation. Changing a
- * property in the object will set that variable in the environment for the
- * process. The environment object will only accept `string`s as values.
- *
- * const myEnv = Deno.env();
- * console.log(myEnv.SHELL);
- * myEnv.TEST_VAR = "HELLO";
- * const newEnv = Deno.env();
- * console.log(myEnv.TEST_VAR === newEnv.TEST_VAR); // outputs "true"
- *
- * Requires `allow-env` permission. */
- export function env(): {
- [index: string]: string;
- };
+ export const env: {
+ /** Retrieve the value of an environment variable. Returns undefined if that
+ * key doesn't exist.
+ *
+ * console.log(Deno.env.get("HOME")); // e.g. outputs "/home/alice"
+ * console.log(Deno.env.get("MADE_UP_VAR")); // outputs "Undefined"
+ *
+ * Requires `allow-env` permission. */
+ get(key: string): string | undefined;
- /** Retrieve the value of an environment variable. Returns undefined if that
- * key doesn't exist.
- *
- * console.log(Deno.env("HOME")); // e.g. outputs "/home/alice"
- * console.log(Deno.env("MADE_UP_VAR")); // outputs "Undefined"
- *
- * Requires `allow-env` permission. */
- export function env(key: string): string | undefined;
+ /** Set the value of an environment variable.
+ *
+ * Deno.env.set("SOME_VAR", "Value"));
+ * Deno.env.get("SOME_VAR"); // outputs "Value"
+ *
+ * Requires `allow-env` permission. */
+ set(key: string, value: string): void;
+
+ /** Returns a snapshot of the environment variables at invocation.
+ *
+ * Deno.env.set("TEST_VAR", "A");
+ * const myEnv = Deno.env.toObject();
+ * console.log(myEnv.SHELL);
+ * Deno.env.set("TEST_VAR", "B");
+ * console.log(myEnv.TEST_VAR); // outputs "A"
+ *
+ * Requires `allow-env` permission. */
+ toObject(): { [index: string]: string };
+ };
/** **UNSTABLE** */
export type DirKind =
diff --git a/cli/js/ops/os.ts b/cli/js/ops/os.ts
index e01718c8c..30aa6d0d4 100644
--- a/cli/js/ops/os.ts
+++ b/cli/js/ops/os.ts
@@ -27,22 +27,13 @@ function getEnv(key: string): string | undefined {
return sendSync("op_get_env", { key })[0];
}
-export function env(): { [index: string]: string };
-export function env(key: string): string | undefined;
-export function env(
- key?: string
-): { [index: string]: string } | string | undefined {
- if (key) {
- return getEnv(key);
- }
- const env = sendSync("op_env");
- return new Proxy(env, {
- set(obj, prop: string, value: string): boolean {
- setEnv(prop, value);
- return Reflect.set(obj, prop, value);
- },
- });
-}
+export const env = {
+ get: getEnv,
+ toObject(): { [key: string]: string } {
+ return sendSync("op_env");
+ },
+ set: setEnv,
+};
type DirKind =
| "home"
diff --git a/cli/js/tests/os_test.ts b/cli/js/tests/os_test.ts
index 58dcd1bc5..a44b69e7d 100644
--- a/cli/js/tests/os_test.ts
+++ b/cli/js/tests/os_test.ts
@@ -8,24 +8,22 @@ import {
} from "./test_util.ts";
unitTest({ perms: { env: true } }, function envSuccess(): void {
- const env = Deno.env();
- assert(env !== null);
- // eslint-disable-next-line @typescript-eslint/camelcase
- env.test_var = "Hello World";
- const newEnv = Deno.env();
- assertEquals(env.test_var, newEnv.test_var);
- assertEquals(Deno.env("test_var"), env.test_var);
+ Deno.env.set("TEST_VAR", "A");
+ const env = Deno.env.toObject();
+ Deno.env.set("TEST_VAR", "B");
+ assertEquals(env["TEST_VAR"], "A");
+ assertNotEquals(Deno.env.get("TEST_VAR"), env["TEST_VAR"]);
});
unitTest({ perms: { env: true } }, function envNotFound(): void {
- const r = Deno.env("env_var_does_not_exist!");
+ const r = Deno.env.get("env_var_does_not_exist!");
assertEquals(r, undefined);
});
unitTest(function envPermissionDenied1(): void {
let err;
try {
- Deno.env();
+ Deno.env.toObject();
} catch (e) {
err = e;
}
@@ -37,7 +35,7 @@ unitTest(function envPermissionDenied1(): void {
unitTest(function envPermissionDenied2(): void {
let err;
try {
- Deno.env("PATH");
+ Deno.env.get("PATH");
} catch (e) {
err = e;
}
@@ -62,7 +60,7 @@ unitTest(
): Promise<void> => {
const src = `
console.log(
- ${JSON.stringify(Object.keys(expectedEnv))}.map(k => Deno.env(k))
+ ${JSON.stringify(Object.keys(expectedEnv))}.map(k => Deno.env.get(k))
)`;
const proc = Deno.run({
cmd: [Deno.execPath(), "eval", src],
@@ -79,8 +77,8 @@ unitTest(
proc.close();
};
- assertEquals(Deno.env("path"), Deno.env("PATH"));
- assertEquals(Deno.env("Path"), Deno.env("PATH"));
+ assertEquals(Deno.env.get("path"), Deno.env.get("PATH"));
+ assertEquals(Deno.env.get("Path"), Deno.env.get("PATH"));
// Check 'foo', 'Foo' and 'Foo' are case folded.
await checkChildEnv({ foo: "X" }, { foo: "X", Foo: "X", FOO: "X" });