Files
dvcp_v2_webapp/bin/appsSync.js
2022-11-21 16:39:36 +08:00

39 lines
1.4 KiB
JavaScript

const axios = require("axios");
const {chalkTag, findApp, fs} = require("./tools");
const compiler = require('vue-template-compiler')
const saveApps = app => {
if (app.list.length > 0) {
return axios.post("http://dvcp.sinoecare.net/node/wechatapps/addOrUpdate", app, {timeout: 1000}).then(res => {
if (res.data.code == 0) chalkTag.done("产品库目录已同步至后台数据库...")
}).catch(() => 0)
} else return Promise.reject("没有应用")
}
const getAppInfo = (file, apps) => {
if (/[\\\/](App[A-Z][^\\\/]+)\.vue$/g.test(file)) {
const name = file.replace(/.+[\\\/](App[^\\\/]+)\.vue$/, '$1'),
source = fs.readFileSync(file).toString(),
parsed = compiler.parseComponent(source),
script = parsed.script?.content || "",
label = script.match(/label:[^,]+/)?.[0]?.replace(/.+["']([^"']+).+/, '$1')
apps.push({
id: file.replace(/\.vue$/, '').replace(/[\\\/]/g, '_'),
label: label || name,
libPath: `/${file.replace(/\.vue$/, '').replace(/[\\\/]/g, '/')}`,
name,
type: 'web'
})
}
}
const sync = () => {
chalkTag.info("开始扫描库工程...")
const list = []
Promise.all([
findApp('packages', app => getAppInfo(app, list)),
findApp('project', app => getAppInfo(app, list)),
]).then(() => {
chalkTag.info("正在同步...")
saveApps({type: "web", list}).catch(() => 0).finally(() => chalkTag.done("同步成功!"))
})
}
sync()