From ef7fc6e97c43dbf2847ed0fff8bb48f5047ddf15 Mon Sep 17 00:00:00 2001 From: aixianling Date: Thu, 16 Mar 2023 09:39:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=90=8C=E6=AD=A5=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/serve.js | 24 +++---------------- bin/sync.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 bin/sync.js diff --git a/bin/serve.js b/bin/serve.js index e47116f7..4833415a 100644 --- a/bin/serve.js +++ b/bin/serve.js @@ -1,5 +1,5 @@ const {chalkTag, findPages, fs, fsExtra} = require("./tools"); -const axios = require("axios"); + let apps = {list: [], desc: "用于产品库主页面获取应用使用", type: 'wxwork'} const getFileInfo = (app, file) => { let vue = fs.readFileSync(file).toString() @@ -11,27 +11,9 @@ const getFileInfo = (app, file) => { } 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`) - apps.list.push({ - id: file.replace(/\.\/?(vue)?/g, '').replace(/[\\\/]/g, '_'), - name, - label, - path, - libPath: file.replace(/\\/g, '/').replace(/^src(\/.+)\.vue/, '$1'), - type: 'wxwork' - }) - } -} -const saveApps = app => { - if (app.list.length > 0) { - 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 => 0).finally(() => fsExtra.outputJson('src/config.json', {apps: app.list})) - } } + const start = () => { chalkTag.info('开始生成pages.json...') let json = { @@ -79,7 +61,7 @@ const start = () => { } }) ]).then(() => { - saveApps(apps) + fsExtra.outputJson('src/config.json', {apps: apps.list}) fsExtra.outputJson('src/pages.json', json, () => { chalkTag.done('生成pages.json') }) diff --git a/bin/sync.js b/bin/sync.js new file mode 100644 index 00000000..560601f4 --- /dev/null +++ b/bin/sync.js @@ -0,0 +1,68 @@ +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 => { + console.log(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`) + 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() diff --git a/package.json b/package.json index 6750b5c6..18a7ea45 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "dev": "node bin/serve.js&&cross-env NODE_ENV=development UNI_PLATFORM=h5 vue-cli-service uni-serve --minimize", "lib": "npm unpublish --force&&npm publish", "lib:projects": "npm unpublish --workspaces --force&&npm publish --workspaces", - "pages": "node bin/serve.js" + "pages": "node bin/serve.js", + "sync": "node bin/sync.js" }, "main": "src/apps/index.js", "files": [