微信小程序轉換器(二)—— 文件的(de)操作 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 小程序相關 >

微信小程序轉換器(二)—— 文件的(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ì)那麽開心

相關案例查看更多