-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
203 lines (176 loc) · 4.74 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { readFile, stat } from "node:fs/promises";
async function main(): Promise<void> {
const workspaceRoot = await getWorkspaceRoot();
const ciReport = await readCiReport(workspaceRoot);
if (!ciReport) {
console.log("CI report file does not exist. No CI tasks may have been executed.");
return;
}
for (const action of ciReport.actions) {
const taskInfo = taskInfoOf(action);
if (!taskInfo) {
continue;
}
const { stdout, stderr } = await readStatus({ workspaceRoot, taskInfo });
const { project, task, command, status } = taskInfo;
const target = `${project}:${task}`;
writeGroup(`${statusBadges[status]} ${bold(target)}`, ({ println }) => {
if (command) {
println(blue(`$ ${command}`));
}
const hasStdout = stdout.trim() !== "";
const hasStderr = stderr.trim() !== "";
if (hasStdout) {
println(stdoutBadge);
println(stdout);
}
if (hasStderr) {
println(stderrBadge);
println(stderr);
}
});
}
}
async function getWorkspaceRoot(): Promise<string> {
return process.cwd();
}
async function readCiReport(workspaceRoot: string): Promise<CiReport | undefined> {
const ciReportPath = `${workspaceRoot}/.moon/cache/ciReport.json`;
if (!(await fileExists(ciReportPath))) {
return;
}
const ciReportFile = await readFileContent(ciReportPath);
return JSON.parse(ciReportFile) as CiReport;
}
function taskInfoOf(action: Action): undefined | TaskInfo {
if (action.node.action !== "run-task") {
return undefined;
}
const { project, task } = parseTarget(action.node.params.target);
return {
project,
task,
command: commandOf(action),
status: action.status,
};
}
type TaskInfo = {
project: string;
task: string;
command: undefined | string;
status: "failed" | "passed" | "skipped";
};
function parseTarget(target: string): { project: string; task: string } {
const parts = target.split(":");
const project = parts[0] ?? "unknown";
const task = parts[1] ?? "unknown";
return { project, task };
}
function commandOf(action: Action): string | undefined {
for (const operation of action.operations) {
if (operation.meta.type === "task-execution") {
return operation.meta.command;
}
}
return undefined;
}
async function readStatus({
workspaceRoot,
taskInfo,
}: { workspaceRoot: string; taskInfo: TaskInfo }): Promise<{ stdout: string; stderr: string }> {
const { project, task } = taskInfo;
const statusDir = `${workspaceRoot}/.moon/cache/states/${project}/${task}`;
const stdoutPath = `${statusDir}/stdout.log`;
const stderrPath = `${statusDir}/stderr.log`;
const stdout = (await fileExists(stdoutPath)) ? await readFileContent(stdoutPath) : "";
const stderr = (await fileExists(stderrPath)) ? await readFileContent(stderrPath) : "";
return { stdout, stderr };
}
async function fileExists(path: string): Promise<boolean> {
try {
await stat(path);
return true;
} catch {
return false;
}
}
async function readFileContent(path: string): Promise<string> {
return await readFile(path, { encoding: "utf8" });
}
function writeGroup(title: string, inner: (params: { println: (output: string) => void }) => void): void {
console.log(`::group::${title}`);
inner({
println(output) {
console.log(output);
},
});
console.log("::endgroup::");
}
const statusBadges: Record<Action["status"], string> = {
passed: bgGreen(" PASS "),
failed: bgRed(" FAIL "),
skipped: bgBlue(" SKIP "),
};
function bgGreen(text: string): string {
return `\u001b[42m${text}\u001b[49m`;
}
function bgRed(text: string): string {
return `\u001b[41m${text}\u001b[49m`;
}
function bgBlue(text: string): string {
return `\u001b[44m${text}\u001b[49m`;
}
function bgDarkGray(text: string): string {
return `\u001b[48;5;236m${text}\u001b[49m`;
}
function bold(text: string): string {
return `\u001b[1m${text}\u001b[22m`;
}
function green(text: string): string {
return `\u001b[32m${text}\u001b[39m`;
}
function red(text: string): string {
return `\u001b[31m${text}\u001b[39m`;
}
function blue(text: string): string {
return `\u001b[34m${text}\u001b[39m`;
}
const stdoutBadge = bgDarkGray(` ${green("⏺")} STDOUT `);
const stderrBadge = bgDarkGray(` ${red("⏺")} STDERR `);
type CiReport = {
actions: Action[];
};
type Action = {
label: string;
nodeIndex: number;
status: "failed" | "passed" | "skipped";
node: Node;
operations: Operation[];
};
type Node =
| {
action: "run-task";
params: {
target: string;
};
}
| {
action: "sync-workspace" | "setup-tool" | "install-deps" | "sync-project" | "install-project-deps";
};
type Operation = {
meta: Meta;
};
type Meta =
| {
type: "task-execution";
command: string;
}
| {
type: "archive-creation" | "hash-generation" | "no-operation" | "output-hydration";
};
try {
await main();
} catch (error) {
console.error(error);
process.exit(0);
}