constfs=require("fs");const{join,resolve}=require('path');constexecSync=require('child_process').execSync;const{mergeUrl}=require('./url.utils');constfindPath=resolve('./dist/template');constrootPath=resolve('./');//文件版本记录constfileVersionNote={};consttoComPath=path=>path.replace(//g,'/');//获取版本号constgetVersion=(dir,fileName)=>{letdirPathPart=toComPath(dir).split('/');dirPathPart.pop();让dirPath=dirPathPart.join('/');fileName=fileName.replace(/^s*(.*?)s*$/,'$1');if(!/^//.test(fileName)){fileName=join(dirPath,fileName).replace(rootPath,'');}else{fileName='dist'+fileName;}returngetGitCommitVersion(toComPath(fileName).replace(/^/(.*?)$/,'$1'));}//获取git文件版本号constgetGitCommitVersion=(file)=>{letgv=null;if(/?/.test(file)){file=file.replace(/^(.*?)?.*?$/,'$1');}让noteV=fileVersionNote[file]如果(typeofnoteV!=='string'||noteV===''){if(fs.existsSync(file)){try{gv=execSync(`gitlog${file}|grepcommit|awk'NR==1'|awk-F'''{print$2}'`);gv=gv.toString();}catch(error){}}if(typeofgv!=='string'||gv.length<10){console.log('版本号获取错误',file);注意V='';}else{noteV=gv.substr(0,7);console.log('新文件git提交编号:',file,noteV);}fileVersionNote[文件]=noteV;}returnnoteV;}//读取所有htmlfunctionfindHtmlAndAddVersion(dir){if(fs.existsSync(dir)){fs.stat(dir,(err,stat)=>{if(err||!stat){returnconsole.error('Errorreadingfilestatus',err);}else{if(stat.isFile()){//只是读取html文件if(/.htm(l){0,1}$/i.test(dir)){fs.readFile(dir,'utf8',(err,data)=>{if(err){returnconsole.error('读取文件错误',err);}else{lethtml=data.replace(//gi,($0,$1,$2,$3,$4,$5)=>{if(/^s*http[s]{0,1}:///i.test($3)){return$0;}else{return``;}}).replace(//gi,($0,$1,$2,$3,$4,$5)=>{if(/^s*http[s]{0,1}:///i.test($3)){返回$0;}else{返回``;}});fs.writeFileSync(dir,html,function(err){if(err){returnconsole.error('写入文件失败',err);}});}});}}elseif(stat.isDirectory()){fs.readdir(dir,function(err,files){if(err||!Array.isArray(files)){returnconsole.error('错误读取文件夹',err);}else{files.forEach(file=>{findHtmlAndAddVersion(join(dir,file));});}});}else{console.error('文件类型识别错误',stat);}}});}else{console.error('文件夹不存在!!!',dir);}}//添加版本号findHtmlAndAddVersion(findPath);
