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()
}
})
动态生成uni-app 配置文件互相影响(尚未完工)
这段代码使用fs-extra、path和hjson等库来读取和处理配置文件,如manifest.js和pages.js。通过chokidar库实现对这些文件的实时监控,当文件发生改变时,会重新加载文件内容并更新对应的json文件。此外,还包含了清理缓存和错误处理的功能。
摘要由CSDN通过智能技术生成