Skip to content

Commit 86c4633

Browse files
committed
refactor(feature): 支持忽略列表文件路径
1 parent ab1094d commit 86c4633

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.release-it.js
2+
build.config.ts

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.release-it.js
2+
build.config.ts

src/command/eslint-fix.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path";
2+
import fs from "node:fs";
23

34
import {
45
execCommand,
@@ -14,7 +15,7 @@ export const eslintFix = async (
1415
options: EsLintOptions,
1516
) => {
1617
try {
17-
const { eslintrc, staged, paths, suffix } = options;
18+
const { eslintrc, ignorePath, staged, paths, suffix } = options;
1819

1920
if (ACTIVATION) {
2021
loggerInfo("eslintFix 参数信息: \n");
@@ -24,6 +25,12 @@ export const eslintFix = async (
2425

2526
const config = path.join(cwd, eslintrc);
2627

28+
const ignoreConfig = path.join(cwd, ignorePath);
29+
let ignoreConfigArgs: string[] = [];
30+
if (fs.existsSync(ignoreConfig)) {
31+
ignoreConfigArgs = ["--ignore-path", ignoreConfig];
32+
}
33+
2734
// 获取需要处理的文件
2835
const files = await getEveryFilesBySuffixes(cwd, staged, paths, suffix);
2936

@@ -38,6 +45,7 @@ export const eslintFix = async (
3845
"suggestion",
3946
"--config",
4047
config,
48+
...ignoreConfigArgs,
4149
...files,
4250
],
4351
{

src/command/prettier-format.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const prettierFormat = async (
1414
cwd = process.cwd(),
1515
options: PrettierFormatOptions,
1616
) => {
17-
const { prettierrc, staged, paths, suffix } = options;
17+
const { prettierrc, ignorePath, staged, paths, suffix } = options;
1818

1919
if (ACTIVATION) {
2020
loggerInfo("prettierFormat 参数信息: \n");
@@ -24,19 +24,28 @@ export const prettierFormat = async (
2424

2525
try {
2626
const config = path.join(cwd, prettierrc);
27-
2827
let configArgs: string[] = [];
2928
if (fs.existsSync(config)) {
3029
configArgs = ["--config", config];
3130
}
3231

32+
const ignoreConfig = path.join(cwd, ignorePath);
33+
let ignoreConfigArgs: string[] = [];
34+
if (fs.existsSync(ignoreConfig)) {
35+
ignoreConfigArgs = ["--ignore-path", ignoreConfig];
36+
}
37+
3338
// 获取需要处理的文件
3439
const files = await getEveryFilesBySuffixes(cwd, staged, paths, suffix);
3540

3641
// 运行 write 命令,重写代码风格
37-
await execCommand("npx", ["prettier", ...configArgs, "--write", ...files], {
38-
stdio: "inherit",
39-
});
42+
await execCommand(
43+
"npx",
44+
["prettier", ...configArgs, ...ignoreConfigArgs, "--write", ...files],
45+
{
46+
stdio: "inherit",
47+
},
48+
);
4049
} catch (error) {
4150
loggerError(error);
4251
}

src/setup.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export const setupSet: SetupSet = {
7474
.option("--eslintrc <file>", "eslintrc file", {
7575
default: esLintOptions.eslintrc,
7676
})
77+
.option("--ignore <file>", ".eslintignore file", {
78+
default: esLintOptions.ignorePath,
79+
})
7780
.option("--path <path>", "Inspecte path", {
7881
default: esLintOptions.paths,
7982
})
@@ -84,9 +87,10 @@ export const setupSet: SetupSet = {
8487
default: ".js,.jsx,.ts,.tsx",
8588
})
8689
.action(async (options) => {
87-
const { eslintrc, staged, path, suffix } = options;
90+
const { eslintrc, ignore, staged, path, suffix } = options;
8891
await eslintFix(cwd, {
8992
eslintrc,
93+
ignorePath: ignore,
9094
staged,
9195
paths: typeof path === "string" ? [path] : path,
9296
suffix: suffix.split(","),
@@ -99,6 +103,9 @@ export const setupSet: SetupSet = {
99103
.option("--prettierrc <file>", "prettierrc file", {
100104
default: prettierFormatOptions.prettierrc,
101105
})
106+
.option("--ignore <file>", ".prettierignore file", {
107+
default: prettierFormatOptions.ignorePath,
108+
})
102109
.option("--path <path>", "Inspecte path", {
103110
default: prettierFormatOptions.paths,
104111
})
@@ -109,9 +116,10 @@ export const setupSet: SetupSet = {
109116
default: ".js,.jsx,.ts,.tsx",
110117
})
111118
.action(async (options) => {
112-
const { prettierrc, staged, path, suffix } = options;
119+
const { prettierrc, ignore, staged, path, suffix } = options;
113120
await prettierFormat(cwd, {
114121
prettierrc,
122+
ignorePath: ignore,
115123
staged,
116124
paths: typeof path === "string" ? [path] : path,
117125
suffix: suffix.split(","),

src/shared/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ export const ACTIVATION = process.env.CG_DEBUG === "activation";
101101

102102
export const esLintOptions: EsLintOptions = {
103103
eslintrc: ".eslintrc.json",
104+
ignorePath: ".eslintignore",
104105
paths: ["src"],
105106
staged: true,
106107
suffix: [".js", "jsx", ".ts", ".tsx"],
107108
};
108109

109110
export const prettierFormatOptions: PrettierFormatOptions = {
110111
prettierrc: ".prettierrc",
112+
ignorePath: ".prettierignore",
111113
paths: ["src"],
112114
staged: true,
113115
suffix: [".js", "jsx", ".ts", ".tsx"],

src/shared/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export interface CommitScope {
1717

1818
export interface EsLintOptions {
1919
eslintrc: string;
20+
ignorePath: string;
2021
paths: string[];
2122
suffix: string[];
2223
staged: boolean;
2324
}
2425

2526
export interface PrettierFormatOptions {
2627
prettierrc: string;
28+
ignorePath: string;
2729
paths: string[];
2830
suffix: string[];
2931
staged: boolean;

0 commit comments

Comments
 (0)