Skip to content

Commit dddd237

Browse files
committed
ci: refactor actions
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent e4e3323 commit dddd237

File tree

26 files changed

+1653
-601
lines changed

26 files changed

+1653
-601
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const assert = require("node:assert/strict");
2+
const { describe, it, beforeEach, afterEach } = require("node:test");
3+
const fs = require("fs");
4+
const path = require("path");
5+
const mock = require("mock-fs");
6+
7+
const { run } = require("../index.js");
8+
9+
describe("generate-brand-content run", () => {
10+
let messages;
11+
let core;
12+
let io;
13+
let outputDir;
14+
let defaultInputs;
15+
16+
beforeEach(() => {
17+
messages = [];
18+
core = {
19+
info: (message) => {
20+
messages.push(message);
21+
},
22+
};
23+
24+
io = {
25+
mkdirP: async (dir) => fs.promises.mkdir(dir, { recursive: true }),
26+
};
27+
28+
outputDir = path.join("tmp", "brand");
29+
30+
defaultInputs = {
31+
version: "1.2.3",
32+
commit: "deadbeef",
33+
colors: JSON.stringify({
34+
items: [
35+
{ id: "primary", value: "#000000" },
36+
{ id: "secondary", value: "#ffffff" },
37+
],
38+
}),
39+
brandMission: JSON.stringify({ statement: "Empower builders" }),
40+
logos: JSON.stringify({ items: [{ id: "primary", format: "svg" }] }),
41+
typography: JSON.stringify({
42+
items: [{ id: "heading", family: "HK Grotesk" }],
43+
}),
44+
};
45+
});
46+
47+
afterEach(() => {
48+
mock.restore();
49+
});
50+
51+
it("writes generated files and logs progress", async () => {
52+
mock({});
53+
54+
await run({
55+
...defaultInputs,
56+
core,
57+
io,
58+
outputDir,
59+
});
60+
61+
const colorsFile = fs.readFileSync(
62+
path.join(outputDir, "generated-colors.ts"),
63+
"utf8",
64+
);
65+
assert.match(colorsFile, /export const brandColors: ColorCollection =/);
66+
assert.match(colorsFile, /Version: 1\.2\.3/);
67+
assert.match(colorsFile, /Commit: deadbeef/);
68+
69+
const missionFile = fs.readFileSync(
70+
path.join(outputDir, "generated-mission.ts"),
71+
"utf8",
72+
);
73+
assert.match(missionFile, /export const brandMission: BrandMission =/);
74+
75+
const typographyFile = fs.readFileSync(
76+
path.join(outputDir, "generated-typography.ts"),
77+
"utf8",
78+
);
79+
assert.match(
80+
typographyFile,
81+
/export const typography: TypographyCollection =/,
82+
);
83+
84+
assert.deepEqual(messages, [
85+
"\u2713 Generated generated-colors.ts (2 items)",
86+
"\u2713 Generated generated-mission.ts",
87+
"\u2713 Generated generated-typography.ts (1 items)",
88+
"\u2713 Generated generated-logos.ts (1 items)",
89+
"\u2705 All brand content files generated successfully",
90+
]);
91+
});
92+
93+
it("throws when required version is missing", async () => {
94+
await assert.rejects(
95+
() =>
96+
run({
97+
...defaultInputs,
98+
version: " ",
99+
core,
100+
io,
101+
outputDir,
102+
}),
103+
/version input is required/,
104+
);
105+
});
106+
107+
it("throws when colors payload is not valid JSON", async () => {
108+
await assert.rejects(
109+
() =>
110+
run({
111+
...defaultInputs,
112+
colors: "not-json",
113+
core,
114+
io,
115+
outputDir,
116+
}),
117+
/Failed to parse colors input/,
118+
);
119+
});
120+
121+
it("throws when core toolkit is missing", async () => {
122+
await assert.rejects(
123+
() =>
124+
run({
125+
...defaultInputs,
126+
core: null,
127+
io,
128+
outputDir,
129+
}),
130+
/@actions\/core instance must be provided/,
131+
);
132+
});
133+
});

.github/actions/generate-brand-content/action.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,33 @@ inputs:
1818
required: true
1919
typography:
2020
description: "JSON-stringified typography collection"
21-
required: false
21+
required: true
2222
output-dir:
2323
description: "Output directory for generated files"
2424
required: true
2525
runs:
26-
using: "node20"
27-
main: "index.js"
26+
using: "composite"
27+
steps:
28+
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
29+
env:
30+
VERSION_INPUT: ${{ inputs.version }}
31+
COMMIT_INPUT: ${{ inputs.commit }}
32+
COLORS_INPUT: ${{ inputs.colors }}
33+
BRAND_MISSION_INPUT: ${{ inputs['brand-mission'] }}
34+
LOGOS_INPUT: ${{ inputs.logos }}
35+
TYPOGRAPHY_INPUT: ${{ inputs.typography }}
36+
OUTPUT_DIR_INPUT: ${{ inputs['output-dir'] }}
37+
with:
38+
script: |
39+
const { run } = require('${{ github.action_path }}/index.js');
40+
await run({
41+
core,
42+
io,
43+
version: process.env.VERSION_INPUT,
44+
commit: process.env.COMMIT_INPUT,
45+
colors: process.env.COLORS_INPUT,
46+
brandMission: process.env.BRAND_MISSION_INPUT,
47+
logos: process.env.LOGOS_INPUT,
48+
typography: process.env.TYPOGRAPHY_INPUT,
49+
outputDir: process.env.OUTPUT_DIR_INPUT,
50+
});

0 commit comments

Comments
 (0)