Files
dvcp_v2_wxcp_app/bin/sync.js
2023-03-16 10:16:40 +08:00

69 lines
2.4 KiB
JavaScript

const {chalkTag, fs, findPages} = require("./tools");
const axios = require("axios");
let apps = {list: [], desc: "用于产品库主页面获取应用使用", type: 'wxwork'}
const saveApps = app => {
if (app.list.length > 0) {
return axios.post("http://192.168.1.87:12525/node/wechatapps/addOrUpdate", app, {timeout: 1000}).then(res => {
if (res.data.code == 0) chalkTag.done("产品库目录已同步至后台数据库...")
}).catch(err => {
chalkTag.error(err)
})
}
}
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) && app.label) {
let {name, label} = app,
path = app.path.replace(/.+[\\\/]([^\\\/]+)[\\\/]([^\\\/]+)$/g, `/apps/$1/$2`)
return apps.list.push({
id: file.replace(/\.\/?(vue)?/g, '').replace(/[\\\/]/g, '_'),
name,
label,
path,
libPath: file.replace(/\\\//g, '/').replace(/^src(\/.+)\.vue/, '$1'),
type: 'wxwork'
})
}
}
const start = () => {
chalkTag.info('开始同步数据到数据库')
Promise.all([
findPages('src/components/pages', file => {
if (/.+[\\\/][^\\\/]+[\\\/][^\\\/]+\.vue/g.test(file)) {
let app = {
path: file.replace(/^src[\\\/](.*).vue/g, '$1').replace(/[\\\/]/g, '/')
}
return getFileInfo(app, file)
}
}),
findPages('src/apps', file => {
if (/.+[\\\/]App[^\\\/]+[\\\/][^\\\/]+\.vue/g.test(file)) {
let app = {
name: file.replace(/.*[\\\/]([^\\\/]+).vue/g, '$1'),
path: file.replace(/^src[\\\/](.*).vue/g, '$1').replace(/[\\\/]/g, '/')
}
return getFileInfo(app, file)
}
}),
findPages('src/project', file => {
if (/.+[\\\/]App[^\\\/]+[\\\/][^\\\/]+\.vue/g.test(file)) {
let app = {
name: file.replace(/.*[\\\/]([^\\\/]+).vue/g, '$1'),
path: file.replace(/^src[\\\/](.*).vue/g, '$1').replace(/[\\\/]/g, '/')
}
return getFileInfo(app, file)
}
})
]).then(() => saveApps(apps)).finally(() => chalkTag.done('同步完成'))
}
start()