Skip to content

Commit 61046a8

Browse files
committed
Save report before throwing errors
1 parent fc2a590 commit 61046a8

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

src/execute-command.ts

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { installPrettier } from "./install-prettier.ts";
55
import { parseCommand } from "./parse-command.ts";
66
import { clearDirectory, createTemporaryDirectory } from "./utilities.ts";
77
import { writeFile } from "./utilities.ts";
8-
import { reportsDirectory, THROW_EXECUTE_ERROR } from "./constants.ts";
8+
import { reportsDirectory } from "./constants.ts";
99
import path from "node:path";
1010

1111
export type ExecuteCommandResult = Awaited<ReturnType<typeof executeCommand>>;
@@ -26,7 +26,7 @@ export async function executeCommand(commandString: string) {
2626
);
2727

2828
let finished = 0;
29-
const results = await Promise.allSettled(
29+
const rawResults = await Promise.allSettled(
3030
repositories.map(async (repository) => {
3131
try {
3232
return await runPrettier(repository, {
@@ -46,55 +46,49 @@ export async function executeCommand(commandString: string) {
4646
}),
4747
);
4848

49-
const errors = results.filter((result) => result.status === "rejected");
50-
if (THROW_EXECUTE_ERROR) {
51-
throw new AggregateError(
52-
errors.map(({ reason }) => reason),
53-
"Command exclude failure.",
54-
);
55-
}
49+
await clearDirectory(reportsDirectory);
50+
const results = await Promise.all(
51+
rawResults.map(async (result, index) => {
52+
const repository = repositories[index];
53+
if (result.status === "rejected") {
54+
const error = result.reason as Error;
55+
const file = path.join(
56+
reportsDirectory,
57+
`error-${repository.directoryName}.txt`,
58+
);
59+
const stringifiedError = inspect(error);
60+
await writeFile(file, stringifiedError);
61+
return {
62+
repository,
63+
fail: true as const,
64+
error,
65+
stringifiedError: stringifiedError,
66+
};
67+
}
68+
69+
const diff = result.value;
70+
const file = path.join(
71+
reportsDirectory,
72+
`${diff ? "diff" : "empty"}-${repository.directoryName}-success.diff`,
73+
);
74+
await writeFile(file, diff);
75+
return {
76+
repository,
77+
fail: false as const,
78+
diff,
79+
};
80+
}),
81+
);
5682

57-
const failedJobsCount = errors.length;
83+
const failedJobs = results.filter((result) => result.fail);
84+
const failedJobsCount = failedJobs.length;
5885
await logger.brief(
5986
`Job finished, succeed on ${results.length - failedJobsCount} repositories, fail on ${failedJobsCount} repositories.\n\nPreparing reports ...`,
6087
);
6188

62-
await clearDirectory(reportsDirectory);
63-
6489
return {
6590
alternative,
6691
original,
67-
results: await Promise.all(
68-
results.map(async (result, index) => {
69-
const repository = repositories[index];
70-
if (result.status === "rejected") {
71-
const error = result.reason;
72-
const file = path.join(
73-
reportsDirectory,
74-
`error-${repository.directoryName}.txt`,
75-
);
76-
const stringifiedError = inspect(error);
77-
await writeFile(file, stringifiedError);
78-
return {
79-
repository,
80-
fail: true as const,
81-
error,
82-
stringifiedError: stringifiedError,
83-
};
84-
}
85-
86-
const diff = result.value;
87-
const file = path.join(
88-
reportsDirectory,
89-
`${diff ? "diff" : "empty"}-${repository.directoryName}-success.diff`,
90-
);
91-
await writeFile(file, diff);
92-
return {
93-
repository,
94-
fail: false as const,
95-
diff,
96-
};
97-
}),
98-
),
92+
results,
9993
};
10094
}

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ async function run() {
2626
);
2727

2828
const commandExecuteResult = await executeCommand(commandString);
29+
const errors = commandExecuteResult.results
30+
.filter((result) => result.fail)
31+
.map(({ error }) => error);
2932

3033
const report = getReport(commandExecuteResult);
3134

@@ -35,6 +38,10 @@ async function run() {
3538
);
3639

3740
await reportOnGithubIssue(report);
41+
42+
if (THROW_EXECUTE_ERROR && errors.length) {
43+
throw new AggregateError(errors, "Command exclude failure.");
44+
}
3845
}
3946

4047
try {

0 commit comments

Comments
 (0)