@@ -62,21 +62,25 @@ function downloadFile(url, filePath) {
6262server . stdout . on ( 'data' , ( data ) => {
6363 const output = data . toString ( ) ;
6464
65- // 使用正则表达式匹配包含 404 错误的行并提取路径
66- const match = output . match ( / G E T \s + ( [ ^ \s ] + ) \s + E r r o r \s + \ (4 0 4 \) / ) ;
65+ // 定义正则表达式,支持任意文件后缀
66+ const regex = / " G E T ( \/ . * ? \. [ a - z A - Z 0 - 9 ] + ) " E r r o r \ (4 0 4 \) : / g ;
6767
68- if ( match ) {
69- const missingFile = match [ 1 ] . replace ( / " / g, '' ) ; // 移除引号
70- console . log ( 'Missing file:' , missingFile ) ;
68+ // 使用正则表达式匹配包含 404 错误的行并提取路径
69+ const matches = [ ...output . matchAll ( regex ) ] ;
70+ if ( matches . length > 0 ) {
71+ matches . forEach ( ( match ) => {
72+ const missingFile = match [ 1 ] . replace ( / " / g, '' ) ; // 移除引号
73+ console . log ( 'Missing file:' , missingFile ) ;
7174
72- // 构建完整的本地文件路径(从项目根目录开始)
73- const localPath = path . join ( process . cwd ( ) , missingFile ) ;
75+ // 构建完整的本地文件路径(从项目根目录开始)
76+ const localPath = path . join ( process . cwd ( ) , missingFile ) ;
7477
75- // 使用命令行传入的baseUrl构建下载URL
76- const downloadUrl = `${ baseUrl } ${ missingFile } ` ;
78+ // 使用命令行传入的baseUrl构建下载URL
79+ const downloadUrl = `${ baseUrl } ${ missingFile } ` ;
7780
78- // 下载文件
79- downloadFile ( downloadUrl , localPath ) ;
81+ // 下载文件
82+ downloadFile ( downloadUrl , localPath ) ;
83+ } ) ;
8084 }
8185} ) ;
8286
@@ -88,4 +92,4 @@ server.stderr.on('data', (data) => {
8892// 监听进程结束
8993server . on ( 'close' , ( code ) => {
9094 console . log ( `http-server process exited with code ${ code } ` ) ;
91- } ) ;
95+ } ) ;
0 commit comments