知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
微信小程序轉(zhuǎn)換器(二)—— 文件的操作
發(fā)表時間:2021-1-5
發(fā)布人:葵宇科技
瀏覽次數(shù):78
啟動方法
轉(zhuǎn)換器的主函數(shù),整個轉(zhuǎn)換器在這里啟動。
function run() {
const currentPath = inputAppPath()
const outPutPath = outputAppPath()
deleteall(outPutPath) // 刪除以前的編譯結果
fs.mkdirSync(outPutPath) // 新建存放編譯結果文件夾
const files = fs.readdirSync(currentPath); // 獲取當前目錄下的所有文件
AnalyzeApp().run() // 編譯全局文件
files.forEach(file => { // 遍歷當前目錄的所有文件
const currentFilePath = currentPath + '/' + file
const currentOutputPath = outPutPath + '/' + file
// 遍歷根目錄,如果是文件夾走進Analyze方法,繼續(xù)遞歸遍歷
if (fs.statSync(currentFilePath).isDirectory() && file !== config.output.name){
fs.mkdirSync(currentOutputPath)
// 去遍歷文件
Analyze(currentFilePath, currentOutputPath)
}
})
}
run()
復制代碼
Analyze函數(shù)
用來遍歷文件,并將文件導向?qū)霓D(zhuǎn)換器。
function Analyze(filePath, outputPath){
// 將文件導向相應的處理函數(shù)
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', ''))
}
}
// 如果當前文件是文件夾,繼續(xù)向下遍歷 否則就調(diào)用analyzeFile導向相應的處理函數(shù)
if (fs.statSync(filePath).isDirectory()) {
const files = fs.readdirSync(filePath)
files.forEach(file => {
const currentFilePath = filePath+'/'+file
const currentOutputPath = outputPath+'/'+file
// 如果是文件夾,繼續(xù)向下遍歷 否則就調(diào)用analyzeFile導向相應的處理函數(shù)
if(fs.statSync(currentFilePath).isDirectory()) {
fs.mkdirSync(currentOutputPath)
Analyze(currentFilePath, currentOutputPath)
} else analyzeFile(currentFilePath, currentOutputPath)
})
} else analyzeFile(filePath, outputPath) // 處理文件
}
復制代碼
全局文件處理
因為全局的app.json和頁面的json文件不一樣,需要單獨處理。索性就把app.json、app.js、app.wxss這幾個在最外層的文件給單獨處理了,其他非文件夾文件不做處理好了。
其實可以在配置文件中增加個exclude屬性,來匹配不用處理的文件路徑,但既然是個簡易的只有框框的轉(zhuǎn)換器,就先采用前面提到的方式。
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()
}
}
}
復制代碼
最后
對,文件操作部分就是那么短hhhhhhh。node就是那么開心
相關案例查看更多
相關閱讀
- 小程序制作
- 云南小程序開發(fā)公司
- 昆明軟件定制公司
- web前端
- 報廢車回收管理系統(tǒng)
- 河南小程序制作
- 汽車拆解管理系統(tǒng)
- 云南科技公司
- 云南網(wǎng)站建設靠譜公司
- 微信小程序開發(fā)入門課程
- 網(wǎng)站排名
- 昆明小程序開發(fā)
- 網(wǎng)站優(yōu)化
- 網(wǎng)站建設招商
- 生成海報
- 小程序開發(fā)排名前十名
- 百度快速排名
- 云南網(wǎng)站制作哪家好
- 軟件定制公司
- 怎么做網(wǎng)站
- 網(wǎng)站建設方案 doc
- 搜索排名
- 云南小程序開發(fā)課程
- 小程序密鑰
- 云南小程序設計
- 小程序退款
- 云南小程序定制
- 英文網(wǎng)站建設公司
- 云南小程序商城
- 百度自然排名