Skip to content

Commit 10aa604

Browse files
committed
👍 Add environment (e) to handle environment variables
1 parent 09f7898 commit 10aa604

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

denops_std/variable/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,23 @@ export async function main(denops: Denops): Promise<void> {
102102
console.log(await vim.get(denops, "version"));
103103
}
104104
```
105+
106+
### environment (alias e)
107+
108+
Use `environment` (or `e`) to access environment variables like:
109+
110+
```typescript
111+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
112+
import { environment } from "https://deno.land/x/denops_std/variable/mod.ts";
113+
114+
export async function main(denops: Denops): Promise<void> {
115+
// Set environment variable
116+
await environment.set(denops, "DENOPS_HELLO", "world");
117+
118+
// Get environment variable
119+
console.log(await environment.get(denops, "DENOPS_HELLO"));
120+
121+
// Remove environment variable
122+
await environment.remove(denops, "DENOPS_HELLO");
123+
}
124+
```

denops_std/variable/environment.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Denops } from "../deps.ts";
2+
import { Getter, Remover, Setter } from "./types.ts";
3+
4+
/**
5+
* Environment variables
6+
*/
7+
export const environment: Getter & Setter & Remover = {
8+
/**
9+
* Get environment variable
10+
*/
11+
async get<T = string>(
12+
denops: Denops,
13+
prop: string,
14+
defaultValue?: T,
15+
): Promise<T | null> {
16+
const name = `\$${prop}`;
17+
const result = await denops.eval(`exists(n) ? ${name} : v`, {
18+
n: name,
19+
v: defaultValue ?? null,
20+
});
21+
// deno-lint-ignore no-explicit-any
22+
return result as any;
23+
},
24+
25+
/**
26+
* Set environment variable
27+
*/
28+
async set<T = string>(
29+
denops: Denops,
30+
prop: string,
31+
value: T,
32+
): Promise<void> {
33+
const name = `\$${prop}`;
34+
await denops.cmd(`let ${name} = value`, {
35+
value,
36+
});
37+
},
38+
39+
/**
40+
* Remove environment variable
41+
*/
42+
async remove(
43+
denops: Denops,
44+
prop: string,
45+
): Promise<void> {
46+
const name = `\$${prop}`;
47+
await denops.cmd(`unlet ${name}`);
48+
},
49+
};
50+
export const e = environment;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { assertEquals, test } from "../deps_test.ts";
2+
import { environment } from "./environment.ts";
3+
4+
test({
5+
mode: "any",
6+
name: "environment.get() return the value of the environment variable",
7+
fn: async (denops) => {
8+
await denops.cmd("let $DENOPS_STD_VARIABLE_ENVIRONMENT = 'hello'");
9+
const result = await environment.get(
10+
denops,
11+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
12+
);
13+
assertEquals(result, "hello");
14+
},
15+
});
16+
test({
17+
mode: "any",
18+
name:
19+
"environment.get() return the defaultValue when the environment variable does not exist",
20+
fn: async (denops) => {
21+
const result = await environment.get(
22+
denops,
23+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
24+
);
25+
assertEquals(result, null);
26+
},
27+
});
28+
test({
29+
mode: "any",
30+
name: "environment.set() replace the value of the environment variable",
31+
fn: async (denops) => {
32+
await denops.cmd("let $DENOPS_STD_VARIABLE_ENVIRONMENT = 'hello'");
33+
await environment.set(denops, "DENOPS_STD_VARIABLE_ENVIRONMENT", "world");
34+
const result = await environment.get(
35+
denops,
36+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
37+
);
38+
assertEquals(result, "world");
39+
},
40+
});
41+
test({
42+
mode: "any",
43+
name: "environment.remove() remove the environment variable",
44+
fn: async (denops) => {
45+
await denops.cmd("let $DENOPS_STD_VARIABLE_ENVIRONMENT = 'hello'");
46+
await environment.remove(denops, "DENOPS_STD_VARIABLE_ENVIRONMENT");
47+
const result = await environment.get(
48+
denops,
49+
"DENOPS_STD_VARIABLE_ENVIRONMENT",
50+
);
51+
assertEquals(result, null);
52+
},
53+
});

denops_std/variable/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./variable.ts";
2+
export * from "./environment.ts";

0 commit comments

Comments
 (0)