Skip to content

Commit 70eb1e3

Browse files
jevin98sleeprite
authored andcommitted
!477 ci(eslint): 控制台新增格式化进度日志
* ci(eslint): 控制台新增格式化进度日志
1 parent c8f9dc2 commit 70eb1e3

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

build/commit-eslint.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const { execSync, spawnSync } = require("node:child_process");
1+
const { execSync, spawn } = require("node:child_process");
22
const path = require("node:path");
33
const process = require("node:process");
4+
const { consola } = require("consola");
45

56
/**
67
* 获取 `暂存区` 所有文件(过滤以删除的文件)
@@ -26,8 +27,8 @@ function getStagedFiles() {
2627
async function processFilesWithESLint(stagedFiles) {
2728
try {
2829
const results = await Promise.allSettled(
29-
stagedFiles.map(file =>
30-
eslintFix(file).then(() => runGitAdd(file)),
30+
stagedFiles.map((file, index) =>
31+
eslintFix(file, stagedFiles.length, index + 1).then(() => runGitAdd(file)),
3132
),
3233
);
3334

@@ -46,31 +47,40 @@ async function processFilesWithESLint(stagedFiles) {
4647
/**
4748
* 执行 ESLint 修复单个文件
4849
*/
49-
function eslintFix(filePath) {
50+
function eslintFix(filePath, length, index) {
5051
return new Promise((resolve, reject) => {
51-
const eslint = spawnSync(
52+
consola.start(`[${index}/${length}] Eslint Formatting... [filePath: ${filePath}] `);
53+
54+
// 使用异步的 spawn
55+
const eslintProcess = spawn(
5256
"npx eslint",
5357
["--fix", "--quiet", filePath],
54-
{
55-
stdio: "inherit",
56-
shell: true,
57-
},
58+
{ shell: true, stdio: "inherit" }, // stdio: 'inherit' 直接输出到控制台
5859
);
5960

60-
if (eslint.status !== 0) {
61-
reject(new Error(`ESLint 修复失败: ${filePath}`));
62-
}
63-
else {
64-
resolve();
65-
}
61+
eslintProcess.on("close", (code) => {
62+
if (code === 0) {
63+
consola.success(`[${index}/${length}] Eslint Format successfully! [filePath: ${filePath}]`);
64+
resolve();
65+
}
66+
else {
67+
consola.error(`[${index}/${length}] Eslint Format failed! [filePath: ${filePath}]`);
68+
reject(new Error(`ESLint 修复失败: ${filePath}`));
69+
}
70+
});
71+
72+
eslintProcess.on("error", (error) => {
73+
consola.error(`[${index}/${length}] Eslint Execution Error! [filePath: ${filePath}]`);
74+
reject(error);
75+
});
6676
});
6777
}
6878

6979
/**
7080
* 执行 `git add ` 将 `eslint --fix` 的文件推送至暂存区
7181
*/
7282
function runGitAdd(filePath) {
73-
spawnSync(
83+
spawn(
7484
"git",
7585
["add", "--", path.normalize(filePath)],
7686
{

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default antfu(
1313
},
1414

1515
ignores: [
16+
"**/*.md",
1617
"docs/",
1718
"**/*.test.*",
1819
],

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"commitizen": "^4.2.4",
5555
"commitlint-config-cz": "^0.13.3",
5656
"components-helper": "2.1.3",
57+
"consola": "^3.4.2",
5758
"cz-conventional-changelog": "^3.3.0",
5859
"cz-customizable": "^6.3.0",
5960
"eslint": "^9.23.0",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy