Skip to content

Commit 8aeb829

Browse files
committed
Add CI-mode
1 parent da31150 commit 8aeb829

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const typescriptProjects = ["./tsconfig.json", "./tsconfig.eslint.json"];
33
/** @type import("eslint").Linter.Config */
44
module.exports = {
55
root: true,
6-
extends: ["@paleite"],
6+
extends: ["@paleite", "plugin:diff/ci"],
77
parserOptions: { project: typescriptProjects, tsconfigRootDir: __dirname },
88
overrides: [
99
{

src/git.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ const hasCleanIndex = (filePath: string): boolean => {
7171
return result;
7272
};
7373

74+
const fetchFromOrigin = (branch: string) => {
75+
const args = [
76+
"fetch",
77+
// "--quiet", // TODO: Uncomment this flag when testing is done
78+
"origin",
79+
branch,
80+
];
81+
82+
try {
83+
child_process.execFileSync(COMMAND, args, OPTIONS).toString();
84+
} catch (err: unknown) {}
85+
};
86+
7487
const getUntrackedFileList = (staged = false): string[] => {
7588
if (staged) {
7689
return [];
@@ -144,6 +157,7 @@ const getRangesForDiff = (diff: string): Range[] =>
144157
}, []);
145158

146159
export {
160+
fetchFromOrigin,
147161
getDiffFileList,
148162
getDiffForFile,
149163
getRangesForDiff,

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import { ciConfig, diff, diffConfig, staged, stagedConfig } from "./processors";
1+
import {
2+
ci,
3+
ciConfig,
4+
diff,
5+
diffConfig,
6+
staged,
7+
stagedConfig,
8+
} from "./processors";
29

310
const configs = {
411
ci: ciConfig,
512
diff: diffConfig,
613
staged: stagedConfig,
714
};
8-
const processors = { diff, staged };
15+
const processors = { ci, diff, staged };
916

1017
module.exports = { configs, processors };
1118

src/processors.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Linter } from "eslint";
2+
import { guessBranch } from "./ci";
23
import {
4+
fetchFromOrigin,
35
getDiffFileList,
46
getDiffForFile,
57
getRangesForDiff,
@@ -8,8 +10,6 @@ import {
810
} from "./git";
911
import type { Range } from "./Range";
1012

11-
const STAGED = true;
12-
1313
/**
1414
* Exclude unchanged files from being processed
1515
*
@@ -95,7 +95,22 @@ const getPostProcessor =
9595
});
9696
};
9797

98-
const getProcessors = (staged = false): Required<Linter.Processor> => {
98+
type ProcessorType = "diff" | "staged" | "ci";
99+
100+
const getProcessors = (
101+
processorType: ProcessorType
102+
): Required<Linter.Processor> => {
103+
const staged = processorType === "staged";
104+
if (processorType === "ci") {
105+
if (process.env.CI === undefined) {
106+
throw Error("Expected CI environment");
107+
}
108+
109+
const branch = process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? guessBranch();
110+
if (branch !== undefined) {
111+
fetchFromOrigin(branch);
112+
}
113+
}
99114
const untrackedFileList = getUntrackedFileList(staged);
100115
const diffFileList = getDiffFileList(staged);
101116

@@ -106,8 +121,9 @@ const getProcessors = (staged = false): Required<Linter.Processor> => {
106121
};
107122
};
108123

109-
const diff = getProcessors();
110-
const staged = getProcessors(STAGED);
124+
const ci = process.env.CI !== undefined ? getProcessors("ci") : {};
125+
const diff = getProcessors("diff");
126+
const staged = getProcessors("staged");
111127

112128
const diffConfig: Linter.BaseConfig = {
113129
plugins: ["diff"],
@@ -120,7 +136,17 @@ const diffConfig: Linter.BaseConfig = {
120136
};
121137

122138
const ciConfig: Linter.BaseConfig =
123-
process.env.CI !== undefined ? diffConfig : {};
139+
process.env.CI === undefined
140+
? {}
141+
: {
142+
plugins: ["diff"],
143+
overrides: [
144+
{
145+
files: ["*"],
146+
processor: "diff/ci",
147+
},
148+
],
149+
};
124150

125151
const stagedConfig: Linter.BaseConfig = {
126152
plugins: ["diff"],
@@ -133,6 +159,7 @@ const stagedConfig: Linter.BaseConfig = {
133159
};
134160

135161
export {
162+
ci,
136163
ciConfig,
137164
diff,
138165
diffConfig,

0 commit comments

Comments
 (0)