Skip to content

Commit 5b0a6fc

Browse files
committed
chore: log 体系化
1 parent 8691ea3 commit 5b0a6fc

3 files changed

Lines changed: 94 additions & 15 deletions

File tree

scripts/filename-lint.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import path from 'path';
22
import { fileURLToPath } from 'url';
33
import fs from 'fs';
44
import sanitize from 'sanitize-filename';
5-
import chalk from 'chalk';
65
import {traversal} from '../utils/fs-utils.js';
6+
import { logger } from '../utils/logger.js';
77

88
const __filename = fileURLToPath(import.meta.url);
99
const __dirname = path.dirname(__filename);
@@ -24,6 +24,7 @@ export function lint() {
2424
const newFileName = fileNameLint(pth);
2525
if (pth !== newFileName) {
2626
renameCount ++;
27+
logger.warn('重命名', `${path.basename(pth)} -> ${path.basename(newFileName)}`);
2728
}
2829
fs.renameSync(pth, newFileName);
2930
} catch(e) {
@@ -33,9 +34,9 @@ export function lint() {
3334
});
3435

3536
if (renameCount > 0) {
36-
console.log(chalk.red.bold('filename lint失败, 已自动将路径非法字符改为"-", 请在git中review'));
37+
logger.error('Lint 失败', '已自动修复非法文件名,请检查变更后重新提交。');
3738
process.exit(1);
3839
} else {
39-
console.log(chalk.green.bold('filename lint完成, 没有不兼容的目录或文件名'));
40+
logger.success('文件名检查通过');
4041
}
4142
}

scripts/pre-commit.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { execSync } from 'child_process';
22
import path from 'path';
33
import matter from 'gray-matter';
44
import fs from 'fs';
5-
import chalk from 'chalk';
65
import { minimatch } from 'minimatch';
76
import { lint } from './filename-lint.js';
87
import {formatDate} from '../utils/date-format.js';
98
import { genPostId } from '../utils/post-id';
9+
import { logger } from '../utils/logger.js';
1010

1111
function decodeUtf8(str: string): string {
1212
str = str.replace(/\\(\d{3})/g, (_match: string, octal: string) => {
@@ -19,7 +19,7 @@ function decodeUtf8(str: string): string {
1919
* 在md文件的frontmatter中更新lastUpdated字段
2020
*/
2121
try {
22-
console.log(chalk.bgCyan.black.bold(' 1. Markdown 预处理 '), chalk.gray('(自动补充 frontmatter 信息)'));
22+
logger.section('1. Markdown 预处理', '(自动补充 frontmatter 信息)');
2323
// 获取本次提交修改过的文件列表
2424
const modifiedFiles = execSync('git diff --cached --name-only', {encoding: 'utf-8'})
2525
.trim()
@@ -35,8 +35,8 @@ try {
3535
}).map(decodeUtf8);
3636

3737
const mdFiles = modifiedFiles.filter(file => minimatch(file, '_posts/**/*.md')).filter(file => fs.existsSync(file));
38-
console.log(chalk.cyan(' • 本次提交修改的文件: '), modifiedFiles);
39-
console.log(chalk.cyan(' • 在_posts中的md文件:'), mdFiles);
38+
logger.info('本次提交修改的文件: ', modifiedFiles);
39+
logger.info('在_posts中的md文件:', mdFiles);
4040
mdFiles.forEach(file => {
4141
const pth = path.resolve(file);
4242
const content = fs.readFileSync(pth, {encoding: 'utf-8'});
@@ -48,22 +48,22 @@ try {
4848
// 无title字段则使用文件名
4949
if (!frontmatter.title) {
5050
frontmatter.title = fileName;
51-
console.log(chalk.green(' ✔ 更新 Title: ') + chalk.yellow(fileName));
51+
logger.success('更新 Title', fileName);
5252
}
5353

5454
// 更新lastUpdated字段
5555
frontmatter.lastUpdated = formatDate(stats.mtime);
56-
console.log(chalk.green(' ✔ 更新 LastUpdated: ') + chalk.yellow(fileName));
56+
logger.success('更新 LastUpdated', fileName);
5757

5858
if (!frontmatter.date) {
5959
const creationTime = stats.birthtime;
6060
frontmatter.date = formatDate(creationTime);
61-
console.log(chalk.green(' ✔ 更新 Date: ') + chalk.yellow(fileName));
61+
logger.success('更新 Date', fileName);
6262
}
6363

6464
if (!frontmatter.id) {
6565
frontmatter.id = genPostId();
66-
console.log(chalk.green(' ✔ 生成 ID: ') + chalk.yellow(fileName));
66+
logger.success('生成 ID', fileName);
6767
}
6868

6969
const newContent = matter.stringify(fileContent.content, frontmatter);
@@ -75,14 +75,14 @@ try {
7575
if (mdFiles.length > 0) {
7676
execSync(`git add ${mdFiles.map(file => `"${file}"`).join(' ')}`);
7777
} else {
78-
console.log(chalk.gray(' - 没有需要更新的文件'));
78+
logger.gray('没有需要更新的文件');
7979
}
8080

81-
console.log('\n' + chalk.bgCyan.black.bold(' 2. FileName Lint '), chalk.gray('(检查文件名兼容性)'));
81+
logger.section('2. FileName Lint', '(检查文件名兼容性)');
8282
lint();
83-
console.log('\n' + chalk.green.bold('✨ Pre-commit hook completed successfully.'));
83+
logger.complete('Pre-commit hook completed successfully.');
8484
process.exit(0);
8585
} catch (error) {
86-
console.error('\n' + chalk.red.bold('✖ Error running pre-commit hook:'), error);
86+
logger.error('Error running pre-commit hook:', error);
8787
process.exit(1);
8888
}

utils/logger.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import chalk from 'chalk';
2+
3+
export const logger = {
4+
/**
5+
* 打印章节标题
6+
* @param title 标题文本
7+
* @param subtitle 副标题/说明文本
8+
*/
9+
section: (title: string, subtitle: string = '') => {
10+
console.log('\n' + chalk.bgCyan.black.bold(` ${title} `) + (subtitle ? ' ' + chalk.gray(subtitle) : ''));
11+
},
12+
13+
/**
14+
* 打印信息列表项 (带 • 前缀)
15+
* @param label 标签
16+
* @param value 值 (可选)
17+
*/
18+
info: (label: string, value?: object | Array<unknown> | string) => {
19+
let msg = chalk.cyan(` • ${label}`);
20+
if (value !== undefined) {
21+
// 如果是对象或数组,格式化一下,否则直接拼接
22+
const valStr = typeof value === 'object' ? JSON.stringify(value) : String(value);
23+
msg += ' ' + valStr;
24+
}
25+
console.log(msg);
26+
},
27+
28+
/**
29+
* 打印成功/完成的子项 (带 ✔ 前缀)
30+
* @param label 动作描述
31+
* @param detail 详细信息 (通常是文件名,会高亮显示)
32+
*/
33+
success: (label: string, detail?: string) => {
34+
let msg = chalk.green(` ✔ ${label}`);
35+
if (detail) {
36+
msg += ': ' + chalk.yellow(detail);
37+
}
38+
console.log(msg);
39+
},
40+
41+
/**
42+
* 打印警告信息 (带 ⚠ 前缀)
43+
* @param label 警告描述
44+
* @param detail 详细信息
45+
*/
46+
warn: (label: string, detail?: string) => {
47+
let msg = chalk.yellow(` ⚠ ${label}`);
48+
if (detail) {
49+
msg += ': ' + detail;
50+
}
51+
console.log(msg);
52+
},
53+
54+
/**
55+
* 打印错误信息 (带 ✖ 前缀)
56+
* @param label 错误描述
57+
* @param error 错误对象或详细信息
58+
*/
59+
error: (label: string, error?: object | string | Array<unknown>) => {
60+
console.error('\n' + chalk.red.bold(`✖ ${label}`), error || '');
61+
},
62+
63+
/**
64+
* 打印完成状态 (带 ✨ 前缀)
65+
* @param message 完成信息
66+
*/
67+
complete: (message: string) => {
68+
console.log('\n' + chalk.green.bold(`✨ ${message}`));
69+
},
70+
71+
/**
72+
* 打印灰色的一般信息 (用于无操作等场景)
73+
* @param message 信息文本
74+
*/
75+
gray: (message: string) => {
76+
console.log(chalk.gray(` - ${message}`));
77+
}
78+
};

0 commit comments

Comments
 (0)