Files
dvcp_v2_wechat_app/bin/pages.js
yanran200730 1ba7c55040 bug
2022-11-01 18:30:18 +08:00

96 lines
3.0 KiB
JavaScript

const axios = require('axios')
const {chalkTag, findPages, fsExtra, fs} = require("./tools");
let apps = {list: [], desc: "用于产品库主页面获取应用使用", type: "mp"}
const getFileInfo = (app, file) => {
let vue = fs.readFileSync(file).toString()
if (/appName/.test(vue)) {
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1')
app.label = appName.replace(/(appName:|["'])/g, '')?.trim()
if (/customNavigation/.test(vue)) {
app.style = {navigationStyle: "custom"}
} else
app.style = {navigationBarTitleText: app.label}
}
if (/^App/.test(app.name)) {
let {name, label} = app,
path = app.path.replace(/.+[\\\/]([^\\\/]+)[\\\/]([^\\\/]+)$/g, `/mods/$1/$2`)
apps.list.push({
id: file.replace(/\.\/?(vue)?/g, '')?.replace(/[\\\/]/g, '_'),
name,
label,
path,
libPath: file?.replace(/\\/g, '/')?.replace(/^src(\/.+)\.vue/, '$1'),
type: 'mp'
})
}
}
const saveApps = app => {
if (app.list.length > 0) {
axios.post("http://192.168.1.87:12525/node/wechatapps/addOrUpdate", app).then(res => {
if (res?.code == 0) chalkTag.done("产品库目录已同步至后台数据库...")
}).catch(() => 0)
}
}
const start = () => {
chalkTag.info('开始生成pages.json...')
let json = {
easycom: {
"^u-(.*)": "uview-ui/components/u-$1/u-$1.vue",
"^(Ai|V)(.*)": "@/components/$1$2/$1$2.vue"
},
pages: [
{path: 'pages/home', style: {navigationBarTitleText: "小程序应用库"}}
],
subPackages: [
{root: "mods/", pages: []},
{root: "components/pages/", pages: []},
{root: "project/", pages: []},
],
globalStyle: {
pageOrientation: "auto",
navigationBarTextStyle: "white",
navigationBarBackgroundColor: "#4181FF",
backgroundColor: "#4181FF"
}
}
Promise.all([
findPages('src/components/pages', file => {
if (/.+\\pages\\[^\\]+\.vue/g.test(file)) {
let app = {
path: file.replace(/^src\\components\\pages\\(.*).vue/g, '$1').replace(/\\/g, '/')
}
getFileInfo(app, file)
return json.subPackages[1].pages.push(app)
}
}),
findPages('src/mods', file => {
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
let app = {
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
path: file.replace(/^src\\mods\\(.*).vue/g, '$1').replace(/\\/g, '/')
}
getFileInfo(app, file)
return json.subPackages[0].pages.push(app)
}
}),
findPages('src/project', file => {
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
let app = {
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
path: file.replace(/^src\\project\\(.*).vue/g, '$1').replace(/\\/g, '/')
}
console.log(app)
getFileInfo(app, file)
return json.subPackages[2].pages.push(app)
}
})
]).then(() => {
saveApps(apps)
fsExtra.outputJson('src/pages.json', json, () => {
chalkTag.done('生成pages.json')
})
})
}
start();