增设企微端产品库数据埋点
This commit is contained in:
167
bin/serve.js
167
bin/serve.js
@@ -1,57 +1,30 @@
|
|||||||
const fsExtra = require('fs-extra')
|
const {chalkTag, findPages, fs, fsExtra} = require("./tools");
|
||||||
const path = require('path')
|
const axios = require("axios");
|
||||||
const fs = require('fs')
|
let apps = {list: [], desc: "用于产品库主页面获取应用使用"}
|
||||||
/**
|
const getFileInfo = (app, file) => {
|
||||||
* 将函数封装成promise
|
let vue = fs.readFileSync(file).toString()
|
||||||
*/
|
if (/appName/.test(vue)) {
|
||||||
const promisify = fn => {
|
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1')
|
||||||
return function () {
|
app.label = appName.replace(/(appName:|["'])/g, '')?.trim()
|
||||||
let args = arguments;
|
if (/customNavigation/.test(vue)) {
|
||||||
return new Promise(function (resolve, reject) {
|
app.style = {navigationStyle: "custom"}
|
||||||
[].push.call(args, function (err, result) {
|
} else
|
||||||
if (err) {
|
app.style = {navigationBarTitleText: app.label}
|
||||||
console.log(err)
|
}
|
||||||
reject(err);
|
if (/^App/.test(app.name)) {
|
||||||
} else {
|
let {name, label} = app,
|
||||||
resolve(result);
|
path = app.path.replace(/.+[\\\/]([^\\\/]+)[\\\/]([^\\\/]+)$/g, `/apps/$1/$2`)
|
||||||
}
|
apps.list.push({id: name, name, label, path, libPath: file?.replace(/\\/g, '/')?.replace(/^src(\/.+)\.vue/, '$1'), type: 'wxwork'})
|
||||||
});
|
}
|
||||||
fn.apply(null, args);
|
}
|
||||||
});
|
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 readdir = promisify(fs.readdir)
|
|
||||||
const stat = promisify(fs.stat)
|
|
||||||
/**
|
|
||||||
* 封装打印工具
|
|
||||||
*/
|
|
||||||
const chalk = require('chalk')
|
|
||||||
const {log} = console
|
|
||||||
const chalkTag = {
|
|
||||||
info: msg => log([chalk.bgBlue.black(' INFO '), msg].join(' ')),
|
|
||||||
done: msg => log([chalk.bgGreen.black(' DONE '), msg].join(' ')),
|
|
||||||
warn: msg => log([chalk.bgYellow.black(' WARN '), msg].join(' ')),
|
|
||||||
error: msg => log([chalk.bgRed.black(' ERROR '), msg].join(' ')),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 遍历应用的方法
|
|
||||||
*/
|
|
||||||
const findApp = (dir, cb) => {
|
|
||||||
return readdir(dir).then(apps => {
|
|
||||||
return Promise.all(apps.map(e => {
|
|
||||||
let cPath = path.join(dir, e)
|
|
||||||
return stat(cPath).then(state => {
|
|
||||||
if (state.isDirectory()) {
|
|
||||||
return findApp(cPath, cb)
|
|
||||||
} else if (state.isFile()) {
|
|
||||||
cb && cb(cPath)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}) || [])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
const start = () => {
|
const start = () => {
|
||||||
chalkTag.info('开始生成pages.json...')
|
chalkTag.info('开始生成pages.json...')
|
||||||
let json = {
|
let json = {
|
||||||
@@ -68,67 +41,51 @@ const start = () => {
|
|||||||
navigationStyle: "custom"
|
navigationStyle: "custom"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
findApp('src/components/pages', file => {
|
Promise.all([
|
||||||
if (/.+\\[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
findPages('src/components/pages', file => {
|
||||||
let app = {
|
if (/.+\\[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
||||||
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
let app = {
|
||||||
|
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
||||||
|
}
|
||||||
|
getFileInfo(app, file)
|
||||||
|
return json.pages.push(app)
|
||||||
}
|
}
|
||||||
let vue = fs.readFileSync(file).toString()
|
}),
|
||||||
if (/appName/.test(vue)) {
|
findPages('src/apps', file => {
|
||||||
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1'),
|
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
||||||
title = appName.replace(/(appName:|["'])/g, '')
|
let app = {
|
||||||
app.style = {navigationBarTitleText: title.trim()}
|
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
|
||||||
|
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
||||||
|
}
|
||||||
|
getFileInfo(app, file)
|
||||||
|
return json.pages.push(app)
|
||||||
}
|
}
|
||||||
json.pages.push(app)
|
}),
|
||||||
}
|
findPages('src/saas', file => {
|
||||||
}).then(() => findApp('src/apps', file => {
|
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
||||||
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
let app = {
|
||||||
let app = {
|
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
|
||||||
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
|
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
||||||
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
}
|
||||||
|
getFileInfo(app, file)
|
||||||
|
return json.pages.push(app)
|
||||||
}
|
}
|
||||||
let vue = fs.readFileSync(file).toString()
|
}),
|
||||||
if (/appName/.test(vue)) {
|
findPages('src/project', file => {
|
||||||
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1'),
|
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
||||||
title = appName.replace(/(appName:|["'])/g, '')
|
let app = {
|
||||||
app.style = {navigationBarTitleText: title.trim()}
|
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
|
||||||
|
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
||||||
|
}
|
||||||
|
getFileInfo(app, file)
|
||||||
|
return json.pages.push(app)
|
||||||
}
|
}
|
||||||
json.pages.push(app)
|
})
|
||||||
}
|
]).then(() => {
|
||||||
})).then(() => findApp('src/saas', file => {
|
saveApps(apps)
|
||||||
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
|
||||||
let app = {
|
|
||||||
name: file.replace(/.*\\([^\\]+).vue/g, '$1'),
|
|
||||||
path: file.replace(/^src\\(.*).vue/g, '$1').replace(/\\/g, '/')
|
|
||||||
}
|
|
||||||
let vue = fs.readFileSync(file).toString()
|
|
||||||
if (/appName/.test(vue)) {
|
|
||||||
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1'),
|
|
||||||
title = appName.replace(/(appName:|["'])/g, '')
|
|
||||||
app.style = {navigationBarTitleText: title.trim()}
|
|
||||||
}
|
|
||||||
json.pages.push(app)
|
|
||||||
}
|
|
||||||
})).then(() => findApp('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, '/')
|
|
||||||
}
|
|
||||||
let vue = fs.readFileSync(file).toString()
|
|
||||||
if (/appName/.test(vue)) {
|
|
||||||
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1'),
|
|
||||||
title = appName.replace(/(appName:|["'])/g, '')
|
|
||||||
app.style = {navigationBarTitleText: title.trim()}
|
|
||||||
}
|
|
||||||
json.pages.push(app)
|
|
||||||
}
|
|
||||||
})).then(() => {
|
|
||||||
fsExtra.outputJson('src/pages.json', json, () => {
|
fsExtra.outputJson('src/pages.json', json, () => {
|
||||||
chalkTag.done('生成pages.json')
|
chalkTag.done('生成pages.json')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
start();
|
start();
|
||||||
|
|||||||
85
bin/tools.js
Normal file
85
bin/tools.js
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
const fsExtra = require('fs-extra')
|
||||||
|
const path = require('path')
|
||||||
|
const chalk = require('chalk')
|
||||||
|
const fs = require('fs')
|
||||||
|
/**
|
||||||
|
* 将函数封装成promise
|
||||||
|
*/
|
||||||
|
const promisify = fn => {
|
||||||
|
return function () {
|
||||||
|
let args = arguments;
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
[].push.call(args, function (err, result) {
|
||||||
|
if (err) {
|
||||||
|
console.log(err)
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve(result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
fn.apply(null, args);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const readdir = promisify(fs.readdir)
|
||||||
|
const stat = promisify(fs.stat)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装打印工具
|
||||||
|
*/
|
||||||
|
const {log} = console
|
||||||
|
const chalkTag = {
|
||||||
|
info: msg => log([chalk.bgBlue.black(' INFO '), msg].join(' ')),
|
||||||
|
done: msg => log([chalk.bgGreen.black(' DONE '), msg].join(' ')),
|
||||||
|
warn: msg => log([chalk.bgYellow.black(' WARN '), msg].join(' ')),
|
||||||
|
error: msg => log([chalk.bgRed.black(' ERROR '), msg].join(' ')),
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 遍历应用的方法
|
||||||
|
*/
|
||||||
|
const findApp = (dir, cb) => {
|
||||||
|
fsExtra.ensureDirSync(dir)
|
||||||
|
return readdir(dir).then(apps => {
|
||||||
|
return Promise.all(apps.map(e => {
|
||||||
|
let cPath = path.join(dir, e)
|
||||||
|
return stat(cPath).then(state => {
|
||||||
|
if (state.isDirectory()) {
|
||||||
|
return findApp(cPath, cb)
|
||||||
|
} else if (state.isFile()) {
|
||||||
|
cb && cb(dir)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}) || [])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const findPages = (dir, cb) => {
|
||||||
|
fsExtra.ensureDirSync(dir)
|
||||||
|
return readdir(dir).then(apps => {
|
||||||
|
return Promise.all(apps.map(e => {
|
||||||
|
let cPath = path.join(dir, e)
|
||||||
|
return stat(cPath).then(state => {
|
||||||
|
if (state.isDirectory()) {
|
||||||
|
return findPages(cPath, cb)
|
||||||
|
} else if (state.isFile()) {
|
||||||
|
cb && cb(cPath)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}) || [])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const copyFiles = (dir, source = 'src/mods') => {
|
||||||
|
chalkTag.info(`开始扫描${source}...`)
|
||||||
|
return new Promise(resolve => {
|
||||||
|
fsExtra.emptyDir(dir, err => {
|
||||||
|
if (!err) {
|
||||||
|
fsExtra.copy(source, dir).then(() => {
|
||||||
|
chalkTag.done(source + ' 扫描完毕')
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
module.exports = {findApp, chalkTag, fsExtra, copyFiles, fs, path, findPages}
|
||||||
Reference in New Issue
Block a user