微信小程序轉換器(二)—— 文件的(de)操作
發表時(shí)間:2021-1-5
發布人(rén):融晨科技
浏覽次數:78
啓動方法
轉換器的(de)主函數,整個(gè)轉換器在(zài)這(zhè)裏啓動。
function run() {
const currentPath = inputAppPath()
const outPutPath = outputAppPath()
deleteall(outPutPath) // 删除以(yǐ)前的(de)編譯結果
fs.mkdirSync(outPutPath) // 新建存放編譯結果文件夾
const files = fs.readdirSync(currentPath); // 獲取當前目錄下的(de)所有文件
AnalyzeApp().run() // 編譯全局文件
files.forEach(file => { // 遍曆當前目錄的(de)所有文件
const currentFilePath = currentPath + '/' + file
const currentOutputPath = outPutPath + '/' + file
// 遍曆根目錄,如果是(shì)文件夾走進Analyze方法,繼續遞歸遍曆
if (fs.statSync(currentFilePath).isDirectory() && file !== config.output.name){
fs.mkdirSync(currentOutputPath)
// 去遍曆文件
Analyze(currentFilePath, currentOutputPath)
}
})
}
run()
複制代碼
Analyze函數
用來(lái)遍曆文件,并将文件導向對應的(de)轉換器。
function Analyze(filePath, outputPath){
// 将文件導向相應的(de)處理函數
function analyzeFile(filePath, outputPath) {
switch(path.extname(filePath)) {
case '.js':
buildJs(filePath, outputPath)
break;
case '.wxss':
buildWxss(filePath, outputPath.replace('.wxss', '.acss'))
break;
case '.json':
buildJson(filePath, outputPath)
break;
case '.wxml':
buildXml(filePath, outputPath.replace('.wxml','.axml'))
break;
case '.ts':
break;
case '.DS_Store':
break;
default:
copyFile(filePath, outputPath.replace('.wxss', ''))
}
}
// 如果當前文件是(shì)文件夾,繼續向下遍曆 否則就(jiù)調用analyzeFile導向相應的(de)處理函數
if (fs.statSync(filePath).isDirectory()) {
const files = fs.readdirSync(filePath)
files.forEach(file => {
const currentFilePath = filePath+'/'+file
const currentOutputPath = outputPath+'/'+file
// 如果是(shì)文件夾,繼續向下遍曆 否則就(jiù)調用analyzeFile導向相應的(de)處理函數
if(fs.statSync(currentFilePath).isDirectory()) {
fs.mkdirSync(currentOutputPath)
Analyze(currentFilePath, currentOutputPath)
} else analyzeFile(currentFilePath, currentOutputPath)
})
} else analyzeFile(filePath, outputPath) // 處理文件
}
複制代碼
全局文件處理
因爲(wéi / wèi)全局的(de)app.json和(hé / huò)頁面的(de)json文件不(bù)一樣,需要(yào / yāo)單獨處理。索性就(jiù)把app.json、app.js、app.wxss這(zhè)幾個(gè)在(zài)最外層的(de)文件給單獨處理了(le/liǎo),其他(tā)非文件夾文件不(bù)做處理好了(le/liǎo)。
其實可以(yǐ)在(zài)配置文件中增加個(gè)exclude屬性,來(lái)匹配不(bù)用處理的(de)文件路徑,但既然是(shì)個(gè)簡易的(de)隻有框框的(de)轉換器,就(jiù)先采用前面提到(dào)的(de)方式。
function AnalyzeApp() {
//處理app.json
function json() {
const appJson = JSON.parse(readFile(inputAppPath('./app.json')))
const appWindow = appJson.window
if (appWindow) {
Object.keys(appWindow).forEach(key => {
const item = compares.WINDOWCONVERTERCONFIG[key]
if (item) {
if (item.target) replaceAtrr(appWindow, key, item.target)
item.handler && item.handler(appWindow)
}
})
}
if (appJson.tabBar) {
compares.TABBARCONVERTERCONFIG.forEach(element => {
if (appJson.tabBar.hasOwnProperty(element.originalKey)) {
if (element.originalKey == 'list') compares.TABBARCONVERTERCONFIG.list.forEach(item => {
element.list.forEach(keyItem => {
replaceAtrr(item, keyItem.originalKey, keyItem.key)
})
})
replaceAtrr(appJson.tabBar, element.originalKey, element.key)
}
})
}
writeFile(outputAppPath('./app.json'), JSON.stringify(appJson))
return appJson
}
//處理app.wxss
function wxss() {
const outputPath = outputAppPath('./app.acss')
buildWxss(inputAppPath('./app.wxss'), outputPath)
}
//處理app.js
function js() {
return buildJs(inputAppPath('./app.js'), outputAppPath('./app.js'))
}
return {
json, wxss, js,
run() {
json()
wxss()
js()
}
}
}
複制代碼
最後
對,文件操作部分就(jiù)是(shì)那麽短hhhhhhh。node就(jiù)是(shì)那麽開心