自动化接入
This commit is contained in:
41
wxmp/bin/PageBase.js
Normal file
41
wxmp/bin/PageBase.js
Normal file
@@ -0,0 +1,41 @@
|
||||
class PageBase {
|
||||
constructor(path, vue) {
|
||||
this.path = path
|
||||
this.name = path.replace(/.*[\\\/]([^\\\/]+)$/g, '$1')
|
||||
this.init(vue)
|
||||
}
|
||||
|
||||
init(vue) {
|
||||
if (/customNavigation/.test(vue)) {
|
||||
this.style = {navigationStyle: "custom"}
|
||||
} else {
|
||||
this.style = {navigationBarTitleText: this.label}
|
||||
//是否开启下拉刷新
|
||||
if (/enablePullDownRefresh/.test(vue)) {
|
||||
this.style.enablePullDownRefresh = true
|
||||
}
|
||||
//导航栏标题颜色及状态栏前景颜色,仅支持 black/white
|
||||
if (/navigationBarTextStyle/.test(vue)) {
|
||||
this.style.navigationBarTextStyle = vue.replace(/[\s\S]*(navigationBarTextStyle:.+),[\s\S]*/gm, '$1')
|
||||
}
|
||||
//导航栏背景颜色(同状态栏背景色)
|
||||
if (/navigationBarBackgroundColor/.test(vue)) {
|
||||
this.style.navigationBarBackgroundColor = vue.replace(/[\s\S]*(navigationBarBackgroundColor:.+),[\s\S]*/gm, '$1')
|
||||
}
|
||||
// //下拉显示出来的窗口的背景色
|
||||
// if (/backgroundColor/.test(vue)) {
|
||||
// this.style.backgroundColor = vue.replace(/[\s\S]*(backgroundColor:.+),[\s\S]*/gm, '$1')
|
||||
// }
|
||||
}
|
||||
if (/appName/.test(vue)) {
|
||||
let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1')
|
||||
this.label = appName.replace(/(appName:|["'])/g, '')?.trim()
|
||||
}
|
||||
}
|
||||
|
||||
setLabel(name) {
|
||||
this.label = name
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PageBase
|
||||
43
wxmp/bin/pages.js
Normal file
43
wxmp/bin/pages.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const {chalkTag, findPages, fsExtra, fs} = require("./tools");
|
||||
const PageBase = require("./PageBase");
|
||||
const start = () => {
|
||||
chalkTag.info('开始生成pages.json...')
|
||||
let json = {
|
||||
easycom: {
|
||||
"^(K|V)(.*)": "@/components/$1$2.vue"
|
||||
},
|
||||
pages: [
|
||||
{path: 'pages/home', style: {navigationBarTitleText: "buy-lite"}},
|
||||
{path: "pages/mine", style: {navigationBarTitleText: "个人中心"}}
|
||||
],
|
||||
subPackages: [
|
||||
{root: "mods/", pages: []},
|
||||
{root: "components/pages/", pages: []},
|
||||
],
|
||||
globalStyle: {
|
||||
pageOrientation: "auto",
|
||||
navigationBarTextStyle: "white",
|
||||
navigationBarBackgroundColor: "#4181FF"
|
||||
}
|
||||
}
|
||||
Promise.all([
|
||||
findPages('src/components/pages', file => {
|
||||
if (/.+\\pages\\[^\\]+\.vue/g.test(file)) {
|
||||
const app = new PageBase(file.replace(/^src\\components\\pages\\(.*).vue/g, '$1').replace(/\\/g, '/'), fs.readFileSync(file).toString())
|
||||
return json.subPackages[1].pages.push(app)
|
||||
}
|
||||
}),
|
||||
findPages('src/mods', file => {
|
||||
if (/.+\\App[^\\]+\\[^\\]+\.vue/g.test(file)) {
|
||||
const app = new PageBase(file.replace(/^src\\mods\\(.*).vue/g, '$1').replace(/\\/g, '/'), fs.readFileSync(file).toString())
|
||||
return json.subPackages[0].pages.push(app)
|
||||
}
|
||||
}),
|
||||
]).then(() => {
|
||||
fsExtra.outputJson('src/pages.json', json, () => {
|
||||
chalkTag.done('生成pages.json')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
start();
|
||||
84
wxmp/bin/tools.js
Normal file
84
wxmp/bin/tools.js
Normal file
@@ -0,0 +1,84 @@
|
||||
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(' ')),
|
||||
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