Skip to content

Commit e430fbe

Browse files
committed
feat(feature): lint 指定后缀文件
1 parent 937cff6 commit e430fbe

File tree

6 files changed

+82
-27
lines changed

6 files changed

+82
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ npm i -g code-genius
2525
| cup | --ignore \<path\> | 清理运行时生成的文件 |
2626
| ihooks | -- | 使用且有修改 simple-git-hooks 后需要重新初始化 |
2727
| run | -- | 运行列出的脚本 |
28-
| lint | --eslintrc \<file\>, --path \<path\>, --staged | 检查代码并尝试修复 |
28+
| lint | --eslintrc \<file\>, --path \<path\>, --staged, --suffix \<suffix\> | 检查代码并尝试修复 |
2929

3030
## API
3131

src/command/eslint-fix.ts

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

44
import {
5-
execCommand,
5+
getLintFiles,
66
loggerError,
77
loggerInfo,
88
loggerSuccess,
@@ -15,12 +15,11 @@ export const eslintFix = async (
1515
options: EsLintOptions
1616
) => {
1717
try {
18-
const { eslintrc, staged, paths } = options;
18+
const { eslintrc, staged, paths, suffix } = options;
1919

2020
if (ACTIVATION) {
2121
loggerInfo("eslintFix 参数信息: \n");
2222
console.table(cwd);
23-
console.table(staged);
2423
console.table(options);
2524
}
2625

@@ -31,28 +30,7 @@ export const eslintFix = async (
3130
overrideConfigFile: path.join(cwd, eslintrc),
3231
});
3332

34-
let files: string[] = [];
35-
if (staged) {
36-
const result = await execCommand("git", [
37-
"diff",
38-
"--name-only",
39-
"--cached",
40-
]);
41-
files =
42-
result
43-
?.split("\n")
44-
.map((path) => `${cwd}/${path}`)
45-
.filter(
46-
(path) =>
47-
path.endsWith(".js") ||
48-
path.endsWith(".jsx") ||
49-
path.endsWith(".ts") ||
50-
path.endsWith(".tsx")
51-
) || [];
52-
} else {
53-
files = paths.map((path) => `${cwd}/${path}`);
54-
}
55-
33+
const files = await getLintFiles(cwd, staged, paths, suffix);
5634
const results = await eslint.lintFiles(files);
5735
await ESLint.outputFixes(results);
5836
const formatter = await eslint.loadFormatter("stylish");

src/setup.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,17 @@ export const setupSet: SetupSet = {
7878
.option("--staged", "Inspecte staged files", {
7979
default: false,
8080
})
81+
.option("--suffix <suffix>", "Inspecte files with specified suffixes", {
82+
default: ".js,.jsx,.ts,.tsx",
83+
})
8184
.action(async (options) => {
82-
const { eslintrc, staged, path } = options;
85+
const { eslintrc, staged, path, suffix } = options;
86+
console.log(suffix)
8387
await eslintFix(cwd, {
8488
eslintrc,
8589
staged,
8690
paths: typeof path === "string" ? [path] : path,
91+
suffix: suffix.split(",")
8792
});
8893
});
8994
},

src/shared/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,5 @@ export const esLintOptions: EsLintOptions = {
9898
eslintrc: ".eslintrc.json",
9999
paths: ["src"],
100100
staged: true,
101+
suffix: [".js", "jsx", ".ts", ".tsx"],
101102
};

src/shared/index.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { execa } from "execa";
2+
import fs from "fs";
3+
import path from "path";
24
import type { Options } from "execa";
35
import { consola } from "consola";
46
import process from "node:process";
@@ -58,3 +60,71 @@ export const loggerSuccess = (content: string) => {
5860
export const loggerError = (content: string | unknown) => {
5961
console.log(lightRed(`[ERROR]: ${content}`));
6062
};
63+
64+
export function filterFiles(
65+
fileList: string[],
66+
allowedExtensions: string[]
67+
): string[] {
68+
const filteredFiles: string[] = [];
69+
70+
for (let i = 0; i < fileList.length; i++) {
71+
const file = fileList[i];
72+
73+
for (let j = 0; j < allowedExtensions.length; j++) {
74+
const extension = allowedExtensions[j];
75+
76+
if (file.endsWith(extension)) {
77+
filteredFiles.push(file);
78+
break;
79+
}
80+
}
81+
}
82+
return filteredFiles;
83+
}
84+
85+
export function getAllFiles(dirPaths: string[]): string[] {
86+
const fileList: string[] = [];
87+
function traverseDirectory(dirPath: string) {
88+
const files = fs.readdirSync(dirPath);
89+
90+
files.forEach((file) => {
91+
const filePath = path.join(dirPath, file);
92+
const stat = fs.statSync(filePath);
93+
94+
if (stat.isDirectory()) {
95+
// 如果是文件夹,则递归遍历
96+
traverseDirectory(filePath);
97+
} else {
98+
// 如果是文件,则将文件路径添加到列表中
99+
fileList.push(filePath);
100+
}
101+
});
102+
}
103+
104+
dirPaths.forEach((dirPath) => {
105+
traverseDirectory(dirPath);
106+
});
107+
108+
const uniqueFiles = Array.from(new Set(fileList));
109+
return uniqueFiles;
110+
}
111+
112+
export const getLintFiles = async (
113+
cwd: string,
114+
staged: boolean,
115+
paths: string[],
116+
suffix: string[]
117+
) => {
118+
let files: string[] = [];
119+
if (staged) {
120+
const result = await execCommand("git", [
121+
"diff",
122+
"--name-only",
123+
"--cached",
124+
]);
125+
files = result?.split("\n").map((path) => `${cwd}/${path}`) || [];
126+
} else {
127+
files = getAllFiles(paths.map((path) => `${cwd}/${path}`));
128+
}
129+
return filterFiles(files, suffix);
130+
};

src/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export interface CommitScope {
1818
export interface EsLintOptions {
1919
eslintrc: string;
2020
paths: string[];
21+
suffix: string[];
2122
staged: boolean;
2223
}

0 commit comments

Comments
 (0)