Last active
June 16, 2016 09:05
-
-
Save mactive/20360f473efd181aaef50f0599620cc2 to your computer and use it in GitHub Desktop.
用 node写了一段脚本 重构代码, 整理一下头部的 import 以及单例的初始化
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from 'fs'; | |
import * as path from 'path'; | |
// app 目录 | |
const appFolder = path.resolve(__dirname, '../app'); | |
console.log(appFolder); | |
// sync version | |
function walkSync(currentDirPath, callback) { | |
fs.readdirSync(currentDirPath).forEach(function (name, index) { | |
var filePath = path.join(currentDirPath, name); | |
var stat = fs.statSync(filePath); | |
if (stat.isFile()) { | |
callback(filePath, stat); | |
} else if (stat.isDirectory()) { | |
walkSync(filePath, callback); | |
} | |
}); | |
} | |
function readSync(filePath,callback){ | |
let data = fs.readFileSync(filePath, 'utf8'); | |
let lines = data.split("\n"); | |
let filterIndex = 0, insertIndex = 0, filterIndexArr = []; | |
let containsLine = lines.filter(function(line){ | |
let regexUser = /^import.*User.*from.*User/; | |
let regexAccount = /^import.*Account.*from.*Account/; | |
let regexTask = /^import.*Task.*from.*Task'/; | |
let regexNetwork = /^import.*AppNetworking.*from.*AppNetworking/; | |
let regexConstants = /^import.*Constants.*from.*Constants/; | |
let regexUtil = /^import.*Util.*from.*Util/; | |
let regexToastUtil = /^import.*ToastUtil.*from.*ToastUtil/; | |
let regexInitUser = /^const.*User.*new.*User/; | |
let regexInitTask = /^const.*Task.*new.*Task/; | |
let regexInitAccount = /^const.*Account.*new.*Account/; | |
let regexInitNetworking = /^const.*Networking.*new.*Networking/; | |
let regexResult = | |
!regexUser.test(line) && | |
!regexAccount.test(line) && | |
!regexTask.test(line) && | |
!regexNetwork.test(line) && | |
!regexConstants.test(line) && | |
!regexUtil.test(line) && | |
!regexToastUtil.test(line) && | |
!regexInitUser.test(line) && | |
!regexInitTask.test(line) && | |
!regexInitAccount.test(line) && | |
!regexInitNetworking.test(line); | |
if(!regexResult){ | |
filterIndexArr.push(filterIndex); | |
} | |
filterIndex += 1; | |
return regexResult; | |
}); | |
insertIndex = filterIndexArr[0]; | |
callback(containsLine,insertIndex); | |
} | |
let filecCount = 0; | |
let addedLine = function(filePath,appFolder){ | |
let subtractStr = filePath.replace(appFolder,''); | |
let level = subtractStr.split("/"); | |
console.log(subtractStr,'level'+level.length); | |
// 第一级是level3 第二级目录 level 4 | |
let innerStr = ''; | |
for(let i = 2; i<level.length;i++){ | |
innerStr = innerStr + '../'; | |
} | |
let addedLine = "import {Networking,Util,Constants,User,Task,Account,ToastUtil} from './" | |
+ innerStr +"SharedInstance';"; | |
return addedLine; | |
} | |
walkSync(appFolder, function(filePath, stat) { | |
if(filePath.endsWith('.js')){ | |
filecCount += 1; | |
console.log("===",path.basename(filePath),filecCount); | |
readSync(filePath,function(lines,insertIndex){ | |
let _addedLine = addedLine(filePath,appFolder); | |
console.log(_addedLine); | |
lines.splice(insertIndex, 0, _addedLine); | |
let refactorFile = lines.join("\n"); | |
fs.writeFile(filePath, refactorFile, function(err) { | |
if (err) { // if error, report | |
console.log (err); | |
} | |
}); | |
lines.forEach(function(line){ | |
// console.log(line); | |
}) | |
}) | |
} | |
}); |
Author
mactive
commented
Jun 16, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment