Skip to content

Commit ee3ff6d

Browse files
authored
feat: Support Workflow level env: (#19)
* chore: Implement envJSON * chore: Implement removeUndefinedKeys function * feat: Support workflow level env
1 parent 5e2a4dc commit ee3ff6d

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

lib/package/env.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { envJSON } from "./env";
3+
4+
describe("envJSON", () => {
5+
test("should return undefined if env is undefined", () => {
6+
expect(envJSON()).toBeUndefined();
7+
});
8+
9+
test("should return env as a record", () => {
10+
expect(
11+
envJSON({
12+
HOGE: "hoge",
13+
FUGA: "fuga",
14+
}),
15+
).toEqual({
16+
HOGE: "hoge",
17+
FUGA: "fuga",
18+
});
19+
});
20+
});

lib/package/env.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type Env = {
2+
[key: string]: string;
3+
};
4+
5+
export function envJSON(env?: Env): Record<string, unknown> | undefined {
6+
if (env == null) return undefined;
7+
return env;
8+
}

lib/package/workflow.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ describe("Workflow", () => {
1212
"run-name": undefined,
1313
on: "push",
1414
permissions: undefined,
15+
env: undefined,
16+
jobs: {},
17+
});
18+
});
19+
20+
test("workflow with env", () => {
21+
const workflow = new Workflow("simple", {
22+
on: "push",
23+
env: {
24+
FOO: "foo",
25+
BAR: "bar",
26+
},
27+
});
28+
29+
expect(workflow.toJSON()).toEqual({
30+
name: "simple",
31+
"run-name": undefined,
32+
on: "push",
33+
env: {
34+
FOO: "foo",
35+
BAR: "bar",
36+
},
37+
permissions: undefined,
1538
jobs: {},
1639
});
1740
});

lib/package/workflow.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Concurrency, concurrencyJSON } from "./concurrency";
2+
import { type Env, envJSON } from "./env";
23
import { Job } from "./job";
34
import { type On, onJSON } from "./on";
45
import { type Permissions, permissionsJSON } from "./permission";
@@ -8,6 +9,7 @@ export type WorkflowConfig = {
89
concurrency?: Concurrency;
910
on: On;
1011
permissions?: Permissions;
12+
env?: Env;
1113
};
1214

1315
export class Workflow {
@@ -31,8 +33,7 @@ export class Workflow {
3133
"run-name": this._config.runName,
3234
on: onJSON(this._config.on),
3335
permissions: permissionsJSON(this._config.permissions),
34-
35-
// TODO: env
36+
env: envJSON(this._config.env),
3637

3738
// TODO: defaults
3839

lib/util/util.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from "bun:test";
2-
import { toUpperCamelCase } from "./util";
2+
import { removeUndefinedKeys, toUpperCamelCase } from "./util";
33

44
describe("toUpperCamelCase", () => {
55
test.each([
@@ -30,3 +30,12 @@ describe("toUpperCamelCase", () => {
3030
expect(toUpperCamelCase(input)).toBe(expected);
3131
});
3232
});
33+
34+
describe("removeUndefinedKeys", () => {
35+
test.each([
36+
[{ foo: "bar", baz: undefined }, ["foo"]],
37+
[{ foo: "bar", baz: undefined, hoge: "fuga" }, ["foo", "hoge"]],
38+
])("removeUndefinedKeys(%j) returns %j", (input, expected) => {
39+
expect(Object.keys(removeUndefinedKeys(input))).toEqual(expected);
40+
});
41+
});

lib/util/util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ export function toUpperCamelCase(str: string): string {
44
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
55
.join("");
66
}
7+
8+
export function removeUndefinedKeys<T extends Record<string, unknown>>(
9+
obj: T,
10+
): Partial<T> {
11+
return Object.fromEntries(
12+
Object.entries(obj).filter(([_, value]) => value !== undefined),
13+
) as Partial<T>;
14+
}

0 commit comments

Comments
 (0)