@@ -2,11 +2,11 @@ import { execSync } from 'child_process';
22import path from 'path' ;
33import matter from 'gray-matter' ;
44import fs from 'fs' ;
5- import chalk from 'chalk' ;
65import { minimatch } from 'minimatch' ;
76import { lint } from './filename-lint.js' ;
87import { formatDate } from '../utils/date-format.js' ;
98import { genPostId } from '../utils/post-id' ;
9+ import { logger } from '../utils/logger.js' ;
1010
1111function 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 */
2121try {
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 ( )
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}
0 commit comments