递归替换目录下的所有代码文件 , 自动识别 2空格或4空格
replaceToTabs.js
var baseDir = "F:\\opensource\\project";//待替换的目录
const fileUtil = require("./FileUtil.js");
run().catch(function(err){
console.error( err );
});
async function run(){
var fileExt =/\.(ts|js|css|html|java|jsp|vue|vuex|less)$/;
await traversal( baseDir , async function( filePath ){
if( !fileExt.test( filePath ))
return;
let text = await fileUtil.readText( filePath );
var tabSize = 4;
var reg ;
if( /\n( )[\w\<\.\(\{"']/.test( text ) ){
tabSize =2 ;
reg = /\n( )+/g ;
}else if( /\n( )[\w\<\.\(\{"']/.test( text ) ){
tabSize = 4 ;
reg = /\n( )+/g ;
}else{
return;
}
let successCnt = 0;
var newText = text.replace( reg , function( a , $1 , $2 , $3){
var tabs = ((a.length-1)/tabSize );
successCnt ++;
return "\n".padEnd( tabs+1 , "