chore: 引入统一日志模块,统一 cli 输出

This commit is contained in:
rbetree
2026-01-16 17:29:05 +08:00
parent f2f59108a0
commit 87d1f0244c
26 changed files with 903 additions and 150 deletions

View File

@@ -2,6 +2,10 @@ const fs = require('node:fs');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const { createLogger, isVerbose, startTimer } = require('../src/generator/utils/logger');
const log = createLogger('format:check:changed');
function runGit(args, cwd, options = {}) {
const { allowFailure = false, stdio } = options;
try {
@@ -103,17 +107,13 @@ function collectChangedFiles(repoRoot, range) {
const baseExists = gitObjectExists(repoRoot, range.base);
const headExists = gitObjectExists(repoRoot, range.head);
if (!baseExists || !headExists) {
console.warn(
'格式检查:检测到 diff range 所需提交缺失,尝试补全 git 历史(避免浅克隆导致失败)'
);
log.warn('检测到 diff range 所需提交缺失,尝试补全 git 历史(避免浅克隆导致失败)');
tryFetchMoreHistory(repoRoot);
}
const output = runGit(diffArgs, repoRoot, { allowFailure: true });
if (!output) {
console.warn(
'格式检查:无法计算 revision range回退为 HEAD 变更文件(可能仅覆盖最后一次提交)'
);
log.warn('无法计算 revision range回退为 HEAD 变更文件(可能仅覆盖最后一次提交)');
return collectHeadChangedFiles(repoRoot);
}
@@ -175,6 +175,9 @@ function resolvePrettierBin(repoRoot) {
function main() {
const repoRoot = path.resolve(__dirname, '..');
const elapsedMs = startTimer();
log.info('开始');
const event = tryReadGithubEvent(process.env.GITHUB_EVENT_PATH);
const range = getDiffRangeFromGithubEvent(event);
@@ -186,21 +189,33 @@ function main() {
const filesToCheck = candidateFiles.filter(shouldCheckFile);
if (filesToCheck.length === 0) {
console.log('格式检查:未发现需要检查的文件,跳过');
log.ok('未发现需要检查的文件,跳过');
return;
}
const prettierBin = resolvePrettierBin(repoRoot);
if (!prettierBin) {
console.error('格式检查失败:未找到 prettier可先运行 npm ci / npm install');
log.error('未找到 prettier可先运行 npm ci / npm install');
process.exitCode = 1;
return;
}
console.log(`格式检查:共 ${filesToCheck.length} 个文件`);
filesToCheck.forEach((filePath) => console.log(`- ${filePath}`));
log.info('准备检查文件格式', { files: filesToCheck.length });
if (isVerbose()) {
filesToCheck.forEach((filePath) => log.info('待检查', { file: filePath }));
}
execFileSync(prettierBin, ['--check', ...filesToCheck], { cwd: repoRoot, stdio: 'inherit' });
try {
execFileSync(prettierBin, ['--check', ...filesToCheck], { cwd: repoRoot, stdio: 'inherit' });
log.ok('通过', { ms: elapsedMs(), files: filesToCheck.length });
} catch (error) {
log.error('未通过', {
ms: elapsedMs(),
files: filesToCheck.length,
exit: error && error.status ? error.status : 1,
});
process.exitCode = error && error.status ? error.status : 1;
}
}
main();