动态生成uni-app 配置文件互相影响(尚未完工)

3 篇文章 1 订阅
1 篇文章 0 订阅
这段代码使用fs-extra、path和hjson等库来读取和处理配置文件,如manifest.js和pages.js。通过chokidar库实现对这些文件的实时监控,当文件发生改变时,会重新加载文件内容并更新对应的json文件。此外,还包含了清理缓存和错误处理的功能。
摘要由CSDN通过智能技术生成
const fs = require("fs-extra")
let path = require("path")
let Hjson = require("hjson")
// const envConfig = require("./config.js")
const chokidar = require("chokidar")
const decache = require("decache")

let watcherConfigManifest = null
let watcherConfigPages = null

let rootPath = (function () {
    let e = path.resolve(__dirname, "./")
    return e
})()


// function pathExist(e) {
//   return fs.pathExistsSync(e)
// }

// function readRouteFile() {
//   let infile = pathExist(path.resolve(rootPath, "src/router", "routes.js"));
//   let obj = {};
//   if (infile) {
//       data = require(path.resolve(rootPath, "src/router", "routes.js"));
//       decache(path.resolve(rootPath, "src/router", "routes.js"));
//       obj = data || {};
//   }
//   return obj;
// }


function loadManifestJson() {
    fs.ensureFileSync(path.resolve(rootPath, "./uni-config/", "manifest.js"))
    delete require.cache[require.resolve('./uni-config/manifest.js')];
    let pdata = require("./uni-config/manifest.js")
    fs.writeFileSync(
        path.resolve(rootPath, "./", "manifest.json"),
        Hjson.rt.stringify(pdata, { quotes: "all", separator: true, space: 4 })
    )
}

function loadPageJson() {
    fs.ensureFileSync(path.resolve(rootPath, "./uni-config/", "pages.js"))
    delete require.cache[require.resolve('./uni-config/pages.js')];
    let pdata = require("./uni-config/pages.js")
    console.log(pdata.easycom, 'loadpage')
    fs.writeFileSync(
        path.resolve(rootPath, "./", "pages.json"),
        Hjson.rt.stringify(pdata, { quotes: "all", separator: true, space: 4 })
    )

}
function creatPagesJs() {
    try {
        const fileContent = fs.readFileSync('./pages.json', 'utf8');
        const jsonObj = Hjson.rt.parse(fileContent);
        let pstr = "module.exports = " + JSON.stringify(jsonObj, null, 2);
        fs.outputFileSync(
            path.resolve(rootPath, "uni-config", "pages.js"),
            pstr
        );
        runWatcherConfigPages();
    } catch (err) {
        console.log(err);
    }
}
function creatManifestJs() {
    try {
        const fileContent = fs.readFileSync('./manifest.json', 'utf8');
        const jsonObj = Hjson.rt.parse(fileContent);
        let pstr = "module.exports =" + JSON.stringify(jsonObj, null, 2);
        fs.outputFileSync(
            path.resolve(rootPath, "uni-config", "manifest.js"),
            pstr
        );
        runWatcherConfigManifest();

    } catch (err) {
        console.log(err);
    }
}

// const config = {
//     active: () => {
//         watcherConfig = chokidar
//             .watch(path.resolve(__dirname, "./uni-config"))
//         watcherConfig.on("all", (event, path) => {
//             if (event == "change" && /manifest\.js$/.test(path)) {
//                 loadManifestJson()
//             }
//             if (event == "change" && /pages\.js$/.test(path)) {
//                 loadPageJson();
//             }
//         })
//     },
//     close: () => {
//         watcherConfig.close()
//     }
// }
// config.active()




function runWatcherConfigManifest() {
    watcherConfigManifest = chokidar
        .watch(path.resolve(__dirname, "./uni-config/manifest.js"))
    watcherConfigManifest.on("all", (event, path) => {
        console.log(event, path, 'manifest')
        if (event == "change") {
            loadManifestJson()
        }
    })
}

function runWatcherConfigPages() {
    watcherConfigPages = chokidar
        .watch(path.resolve(__dirname, "./uni-config/pages.js"));
    watcherConfigPages.on("all", (event) => {
        console.log(event, path, 'pages')
        if (event == "change") {
            loadPageJson()
        }
    })
}

function initJs() {
    creatPagesJs();
    creatManifestJs();
}
initJs()
// const watcherConfig = chokidar
//     .watch(path.resolve(__dirname, "./uni-config"))
// watcherConfig.on("all", (event, path) => {
//     if (event == "change" && /manifest\.js$/.test(path)) {
//         loadManifestJson()
//     }
//     if (event == "change" && /pages\.js$/.test(path)) {
//         loadPageJson();
//     }
// })

const watcherManifestJson = chokidar
    .watch(path.resolve(__dirname, "./manifest.json"))
watcherManifestJson.on("all", (event) => {
    console.log(event, path, 'manifest.json', watcherConfigManifest.closed)
    if (event == "change") {
        watcherConfigManifest.close()
        creatManifestJs();
    }
})

const watcherPagesJson = chokidar
    .watch(path.resolve(__dirname, "./pages.json"))
watcherPagesJson.on("all", (event, path) => {
    console.log(event, path, 'pages.json', watcherConfigPages.closed)
    if (event == "change") {
        watcherConfigPages.close()
        creatPagesJs()
    }
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值