小程序仓库路径统一化完成

This commit is contained in:
aixianling
2022-05-12 10:58:39 +08:00
parent 115ba62141
commit 5a4c6103e6
8 changed files with 101 additions and 137 deletions

5
.gitignore vendored
View File

@@ -2,7 +2,7 @@
node_modules/
unpackage/
dist/
lib/
# local env files
.env.local
.env.*.local
@@ -24,5 +24,4 @@ yarn-error.log*
/package-lock.json
/.hbuilderx/launch.json
/src/pages.json
/src/mods/project/
/src/pages/apps.json

View File

@@ -13,9 +13,10 @@ const getDirs = (dir, list = [], cb) => {
return list
}
getDirs(path.join(__dirname, '..', 'src'), [], (dir, path) => {
if (dir.name == "apps") {
if (["apps"].includes(dir.name)) {
fse.remove(path)
console.log("已清除%s", path)
}
return dir.name != "apps"
return !["apps"].includes(dir.name)
})
fse.remove(path.join(__dirname, '..', 'lib'))

16
bin/lib.js Normal file
View File

@@ -0,0 +1,16 @@
const {findApp, chalkTag, copyFiles} = require("./tools");
const start = () => {
chalkTag.info("扫描主库目录,并搬运打包应用至lib文件夹下")
let apps = []
findApp('src/mods', file => apps.push(file))
.then(() => Promise.all([...new Set(apps)].map(e => {
let name = e.replace(/.+[\\\/]([^\\\/]+)$/, '$1')
if (/^App/.test(name)) {
return copyFiles(`lib/${name}`, e)
}
})))
.then(() => {
chalkTag.done("打包完成")
})
}
start()

View File

@@ -1,59 +1,5 @@
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)
}
})
}) || [])
})
}
const {chalkTag, findApp, fsExtra, fs} = require("./tools");
let apps = {list: [], desc: "用于产品库主页面获取应用使用"}
const getFileInfo = (app, file) => {
let vue = fs.readFileSync(file).toString()
@@ -70,7 +16,11 @@ const getFileInfo = (app, file) => {
}
}
const saveApps = app => {
axios.post("http://localhost:12525/node/wechatapps/addOrUpdate", app).catch(() => 0)
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 start = () => {
chalkTag.info('开始生成pages.json...')

70
bin/tools.js Normal file
View File

@@ -0,0 +1,70 @@
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 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}

View File

@@ -6,12 +6,12 @@
"scripts": {
"dev": "node bin/pages.js&&cross-env NODE_ENV=development VUE_APP_CW_MODE=dev UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch --minimize",
"pages": "node bin/pages.js",
"lib": "npm unpublish --force&&npm publish",
"lib": "node bin/lib.js&&npm unpublish --force&&npm publish&&node bin/clean.js",
"lib:all": "node src/project/build.js&&npm unpublish --workspaces --force&&npm publish --workspaces&&node bin/clean.js",
"clean": "node bin/clean.js"
},
"files": [
"src/mods"
"lib"
],
"workspaces": [
"src/components",

View File

@@ -69,7 +69,7 @@ export default {
},
getApps() {
this.$instance.post("/node/wechatapps/list", null, {
baseURL: "http://localhost:12525", params: {size: 999}
baseURL: "http://192.168.1.87:12525", params: {size: 999}
}).then(res => {
if (res?.data) {
this.apps = res.data.records.map(e => ({...e, path: e.libPath}))

View File

@@ -1,77 +1,5 @@
const fsExtra = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const fs = require('fs')
const {exec} = require("child_process");
/**
* 将函数封装成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 {chalkTag, fsExtra, findApp, copyFiles, fs, path} = require("../../bin/tools");
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)
}
})
}) || [])
})
}
/**
* 迁移apps文件
*/
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()
})
}
})
})
}
/**
* 初始化打包配置文件
*/