微信小程序转换器(二)—— 文件的操作 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 小程序相关 >

微信小程序转换器(二)—— 文件的操作

发表时间:2021-1-5

发布人:葵宇科技

浏览次数:60

启动方法

转换器的主函数,整个转换器在这里启动。

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方法,继续递归遍历
        if (fs.statSync(currentFilePath).isDirectory() && file !== config.output.name){
            fs.mkdirSync(currentOutputPath)
            // 去遍历文件
            Analyze(currentFilePath, currentOutputPath)
        }
    })  
}

run()

复制代码

Analyze函数

用来遍历文件,并将文件导向对应的转换器。

function Analyze(filePath, outputPath){
    // 将文件导向相应的处理函数
    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', ''))
        }
    }
    
    // 如果当前文件是文件夹,继续向下遍历 否则就调用analyzeFile导向相应的处理函数
    if (fs.statSync(filePath).isDirectory()) {
        const files = fs.readdirSync(filePath)
        files.forEach(file => {
            const currentFilePath = filePath+'/'+file
            const currentOutputPath = outputPath+'/'+file
            // 如果是文件夹,继续向下遍历 否则就调用analyzeFile导向相应的处理函数
            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属性,来匹配不用处理的文件路径,但既然是个简易的只有框框的转换器,就先采用前面提到的方式。


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就是那么开心

相关案例查看更多