Skip to content

Commit 1b70b19

Browse files
committed
Minor refactor
1 parent 8acaa1f commit 1b70b19

File tree

7 files changed

+331
-63
lines changed

7 files changed

+331
-63
lines changed

eslint.config.mjs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,54 @@
11
import eslint from "@eslint/js";
22
import { defineConfig } from "eslint/config";
3-
import tseslint from "typescript-eslint";
43
import eslintConfigPrettier from "eslint-config-prettier/flat";
4+
import eslintPluginSimpleImportSort from "eslint-plugin-simple-import-sort";
5+
import eslintPluginUnicorn from "eslint-plugin-unicorn";
56
import globals from "globals";
7+
import tseslint from "typescript-eslint";
68

79
export default defineConfig(
8-
{ languageOptions: { globals: { ...globals.builtin, ...globals.node } } },
910
eslint.configs.recommended,
1011
tseslint.configs.recommended,
12+
{
13+
languageOptions: { globals: { ...globals.builtin, ...globals.node } },
14+
plugins: {
15+
unicorn: eslintPluginUnicorn,
16+
"simple-import-sort": eslintPluginSimpleImportSort,
17+
},
18+
},
1119
eslintConfigPrettier,
12-
{ ignores: ["build", "running", "repositories"] },
20+
{
21+
rules: {
22+
"unicorn/template-indent": "error",
23+
24+
/* eslint-plugin-simple-import-sort */
25+
"simple-import-sort/imports": [
26+
"error",
27+
{
28+
groups: [
29+
// https://github.com/lydell/eslint-plugin-simple-import-sort/blob/20e25f3b83c713825f96b8494e2091e6600954d6/src/imports.js#L5-L19
30+
// Side effect imports.
31+
[String.raw`^\u0000`],
32+
// Remove blank lines between groups
33+
// https://github.com/lydell/eslint-plugin-simple-import-sort#how-do-i-remove-all-blank-lines-between-imports
34+
[
35+
// Node.js builtins prefixed with `node:`.
36+
"^node:",
37+
// Packages.
38+
// Things that start with a letter (or digit or underscore), or `@` followed by a letter.
39+
String.raw`^@?\w`,
40+
// Absolute imports and other imports such as Vue-style `@/foo`.
41+
// Anything not matched in another group.
42+
"^",
43+
// Relative imports.
44+
// Anything that starts with a dot.
45+
String.raw`^\.`,
46+
],
47+
],
48+
},
49+
],
50+
"simple-import-sort/exports": "error",
51+
},
52+
},
53+
{ ignores: ["running", "repositories", "reports"] },
1354
);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"main": "index.js",
99
"type": "module",
1010
"scripts": {
11-
"fix": "prettier . -w",
11+
"fix": "prettier . --write && eslint --fix",
1212
"lint": "run-p \"lint:*\"",
1313
"lint:eslint": "eslint",
1414
"lint:prettier": "prettier . --check",
@@ -32,6 +32,8 @@
3232
"@typescript-eslint/parser": "8.49.0",
3333
"eslint": "9.39.1",
3434
"eslint-config-prettier": "10.1.8",
35+
"eslint-plugin-simple-import-sort": "^12.1.1",
36+
"eslint-plugin-unicorn": "^62.0.0",
3537
"globals": "16.5.0",
3638
"npm-run-all": "4.1.5",
3739
"prettier": "3.7.4",

src/execute-command.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import path from "node:path";
12
import { inspect } from "node:util";
2-
import { runPrettier } from "./run-prettier.ts";
3-
import * as logger from "./logger.ts";
3+
import { outdent } from "outdent";
4+
import { reportsDirectory } from "./constants.ts";
45
import { installPrettier } from "./install-prettier.ts";
6+
import * as logger from "./logger.ts";
57
import { parseCommand } from "./parse-command.ts";
8+
import { runPrettier } from "./run-prettier.ts";
69
import { clearDirectory, createTemporaryDirectory } from "./utilities.ts";
710
import { writeFile } from "./utilities.ts";
8-
import { reportsDirectory } from "./constants.ts";
9-
import path from "node:path";
1011

1112
export type ExecuteCommandResult = Awaited<ReturnType<typeof executeCommand>>;
1213

@@ -83,7 +84,13 @@ export async function executeCommand(commandString: string) {
8384
const failedJobs = results.filter((result) => result.fail);
8485
const failedJobsCount = failedJobs.length;
8586
await logger.brief(
86-
`Job finished, succeed on ${results.length - failedJobsCount} repositories, fail on ${failedJobsCount} repositories.\n\nPreparing reports ...`,
87+
outdent`
88+
Job finished,
89+
succeed on ${results.length - failedJobsCount} repositories,
90+
fail on ${failedJobsCount} repositories.
91+
92+
Preparing reports ...
93+
`,
8794
);
8895

8996
return {

src/logger.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import { inspect } from "node:util";
12
import * as github from "@actions/github";
3+
import { outdent } from "outdent";
24
import {
3-
IS_TRIGGERED_BY_GITHUB_ISSUE_COMMENT,
45
GITHUB_ACTION_JOB_URL,
6+
IS_TRIGGERED_BY_GITHUB_ISSUE_COMMENT,
57
} from "./constants.ts";
68
import { getOctokit } from "./octokit.ts";
7-
import { codeBlock } from "./utilities.ts";
8-
import { inspect } from "node:util";
9-
import { outdent } from "outdent";
9+
import { printMarkdownCodeBlock, printMarkdownDetails } from "./utilities.ts";
1010

1111
type Comment = Awaited<ReturnType<typeof createComment>>;
1212

@@ -42,8 +42,9 @@ export async function brief(message: string) {
4242
messages.push({ time: new Date(), message });
4343

4444
const body = outdent`
45-
${messages.map(({ time, message }) => `[${time.toISOString()}]: ${message}`).join("\n")}
46-
__[details](${GITHUB_ACTION_JOB_URL})__
45+
${messages.map(({ time, message }) => `[${time.toISOString()}]: ${message}`).join("\n")}
46+
47+
_[details](${GITHUB_ACTION_JOB_URL})_
4748
`;
4849

4950
if (briefCommentRequest) {
@@ -65,12 +66,11 @@ export async function error(error: Error) {
6566
return;
6667
}
6768

68-
const text = outdent`
69-
<details><summary>[${error.name}](${error.message})</summary>
69+
const text = printMarkdownDetails(
70+
`[${error.name}](${error.message})`,
71+
printMarkdownCodeBlock(inspect(error)),
72+
);
7073

71-
${codeBlock(inspect(error))}
72-
</details>
73-
`;
7474
return await brief(text);
7575
}
7676

src/report.ts

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { type ExecuteCommandResult } from "./execute-command.ts";
12
import { PRETTIER_PACKAGE_TYPE_PULL_REQUEST } from "./parse-command.ts";
23
import { type PrettierVersion } from "./parse-command.ts";
3-
import { type ExecuteCommandResult } from "./execute-command.ts";
4-
import { codeBlock } from "./utilities.ts";
5-
import { outdent } from "outdent";
4+
import { printMarkdownCodeBlock, printMarkdownDetails } from "./utilities.ts";
65

76
function getPrettierVersionDescription(prettier: PrettierVersion) {
87
if (prettier.type === PRETTIER_PACKAGE_TYPE_PULL_REQUEST) {
@@ -52,7 +51,7 @@ export function getReport({
5251
const results = rawResults.map((rawResult) => ({
5352
...rawResult,
5453
text: rawResult.fail
55-
? codeBlock(rawResult.stringifiedError)
54+
? printMarkdownCodeBlock(rawResult.stringifiedError)
5655
: formatDiff(rawResult.diff),
5756
}));
5857

@@ -76,36 +75,8 @@ function formatDiff(diff: string) {
7675
return "**The diff is empty.**";
7776
}
7877
const linesCount = diff.split("\n").length;
79-
const code = codeBlock(diff, "diff");
78+
const code = printMarkdownCodeBlock(diff, "diff");
8079
return linesCount > LONG_DIFF_THRESHOLD_IN_LINES
81-
// Note: do not indent the code block, otherwise the diff render incorrectly in comments.
82-
/*
83-
``````md
84-
```diff
85-
- this is not remove
86-
```
87-
``````
88-
89-
and
90-
91-
92-
``````md
93-
```diff
94-
- this is not remove
95-
```
96-
``````
97-
98-
are different
99-
*/
100-
101-
? outdent`
102-
<details>
103-
<summary>Diff (${linesCount} lines)</summary>
104-
105-
106-
${code}
107-
108-
</details>
109-
`
80+
? printMarkdownDetails(`Diff (${linesCount} lines)`, code)
11081
: code;
11182
}

src/utilities.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import assert from "node:assert/strict";
2+
import crypto from "node:crypto";
13
import fs from "node:fs/promises";
24
import os from "node:os";
35
import path from "node:path";
4-
import crypto from "node:crypto";
5-
import { temporaryDirectory } from "./constants.ts";
66
import spawn, { SubprocessError } from "nano-spawn";
7-
import assert from "node:assert/strict";
7+
import { outdent } from "outdent";
8+
import { temporaryDirectory } from "./constants.ts";
89

910
export async function createTemporaryDirectory() {
1011
const directory =
@@ -95,7 +96,7 @@ export async function resetToCommitHash(directory: string, commitHash: string) {
9596
assert.equal(await getCommitHash(directory), commitHash);
9697
}
9798

98-
export function codeBlock(content: string, syntax?: string) {
99+
export function printMarkdownCodeBlock(content: string, syntax?: string) {
99100
const backtickSequences = content.match(/`+/g) || [];
100101
const longestBacktickSequenceLength = Math.max(
101102
...backtickSequences.map(({ length }) => length),
@@ -104,3 +105,32 @@ export function codeBlock(content: string, syntax?: string) {
104105
const fence = "`".repeat(fenceLength);
105106
return [fence + (syntax || ""), content, fence].join("\n");
106107
}
108+
109+
export function printMarkdownDetails(summary: string, body: string) {
110+
/*
111+
Note: do not indent the code block, otherwise the diff render incorrectly in comments.
112+
``````md
113+
```diff
114+
- this is not removal
115+
- this is removal
116+
```
117+
118+
and
119+
120+
```diff
121+
- this is not removal
122+
- this is removal
123+
```
124+
125+
are different
126+
``````
127+
*/
128+
return outdent`
129+
<details>
130+
<summary>${summary}</summary>
131+
132+
${body}
133+
134+
</details>
135+
`;
136+
}

0 commit comments

Comments
 (0)