Files
dvcp_v2_wechat_app/bin/pages.js
2022-05-12 09:49:40 +08:00

136 lines
3.9 KiB
JavaScript

const fsExtra = require('fs-extra')
const path = require('path')
const fs = require('fs')
const axios = require('axios')
/**
* 将函数封装成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 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) => {
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(cPath)
}
})
}) || [])
})
}
let apps = {list: [], desc: "用于产品库主页面获取应用使用"}
const getFileInfo = (app, file) => {
let vue = fs.readFileSync(file).toString()
if (/customNavigation/.test(vue)) {
app.style = {navigationStyle: "custom"}
} else if (/appName/.test(vue)) {
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1'),
title = appName.replace(/(appName:|["'])/g, '')
app.style = {navigationBarTitleText: title}
}
if (/^App/.test(app.name)) {
let {name, style: {navigationBarTitleText: label}} = app
apps.list.push({id: name, name, label, path: `/mods/${name}/${name}`, libPath: file?.replace(/\\/g, '/')?.replace(/^src(\/.+)\.vue/, '$1')})
}
}
const saveApps = app => {
axios.post("http://localhost:12525/node/wechatapps/addOrUpdate", app).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([
findApp('src/components/pages', file => {
if (/.+\\[^\\]+\\[^\\]+\.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)
}
}),
findApp('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)
}
}),
findApp('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, '/')
}
getFileInfo(app, file)
return json.subPackages[2].pages.push(app)
}
})
]).then(() => {
saveApps(apps)
fsExtra.outputJson('src/pages.json', json, () => {
chalkTag.done('生成pages.json')
})
})
}
start();