Skip to content

Commit bb95754

Browse files
committed
feat(feature): 支持仅lint暂存区文件
1 parent e5eec70 commit bb95754

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

scripts/lint-script.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { eslintFix } from "./../src/command/eslint-fix";
2+
import { loggerWarring } from "../src/shared";
3+
import { esLintOptions } from "../src/shared/config";
4+
5+
async function lint() {
6+
try {
7+
await Promise.all([eslintFix(process.cwd(), esLintOptions)]);
8+
} catch (error) {
9+
loggerWarring(error);
10+
}
11+
}
12+
13+
lint();

src/command/eslint-fix.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ESLint } from "eslint";
22
import path from "node:path";
33

44
import {
5+
execCommand,
56
loading,
67
loggerError,
78
loggerInfo,
@@ -15,11 +16,12 @@ export const eslintFix = async (
1516
options: EsLintOptions
1617
) => {
1718
try {
18-
const { eslintrc, paths } = options;
19+
const { eslintrc, staged, paths } = options;
1920

2021
if (ACTIVATION) {
2122
loggerInfo("eslintFix 参数信息: \n");
2223
console.table(cwd);
24+
console.table(staged);
2325
console.table(options);
2426
}
2527

@@ -30,7 +32,17 @@ export const eslintFix = async (
3032
overrideConfigFile: path.join(cwd, eslintrc),
3133
});
3234

33-
const files = paths.map((path) => `${cwd}/${path}`);
35+
let files: string[] = [];
36+
if (staged) {
37+
const result = await execCommand("git", [
38+
"diff",
39+
"--name-only",
40+
"--cached",
41+
]);
42+
files = result?.split("\n").map((path) => `${cwd}/${path}`) || [];
43+
} else {
44+
files = paths.map((path) => `${cwd}/${path}`);
45+
}
3446

3547
const resultText = await loading(async () => {
3648
const results = await eslint.lintFiles(files);

src/setup.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { eslintFix } from "./command/eslint-fix";
22
import type { CAC } from "cac";
33

4-
import { cleanUpDirs, cwd, esLintOptions, gitCommitScopes, gitCommitTypes } from "./shared/config";
4+
import {
5+
cleanUpDirs,
6+
cwd,
7+
esLintOptions,
8+
gitCommitScopes,
9+
gitCommitTypes,
10+
} from "./shared/config";
511
import { SetupSet } from "./shared/types";
612

713
import { gitCommitVerify } from "./command/git-commit-verify";
@@ -62,16 +68,19 @@ export const setupSet: SetupSet = {
6268
},
6369
eslintFix: (cli: CAC) => {
6470
cli
65-
.command("lint", "Inspected the code and try to fix it.")
71+
.command("lint", "Inspecte the code and try to fix it.")
6672
.option("--eslintrc <file>", "eslintrc file", {
6773
default: esLintOptions.eslintrc,
6874
})
69-
.option("--path <path>", "inspected path", {
75+
.option("--path <path>", "Inspecte path", {
7076
default: esLintOptions.paths,
7177
})
78+
.option("--staged", "Inspecte staged files", {
79+
default: false,
80+
})
7281
.action(async (options) => {
73-
const { eslintrc, path } = options;
74-
await eslintFix(cwd, { eslintrc, paths: path });
82+
const { eslintrc, staged, path } = options;
83+
await eslintFix(cwd, { eslintrc, staged, paths: path });
7584
});
7685
},
7786
};

src/shared/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@ export const ACTIVATION = process.env.CG_DEBUG === "activation";
9797
export const esLintOptions: EsLintOptions = {
9898
eslintrc: ".eslintrc.json",
9999
paths: ["src"],
100+
staged: true,
100101
};

src/shared/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export interface CommitScope {
1818
export interface EsLintOptions {
1919
eslintrc: string;
2020
paths: string[];
21-
}
21+
staged: boolean;
22+
}

0 commit comments

Comments
 (0)