diff --git a/.gitignore b/.gitignore index badd2ed..3389d96 100644 --- a/.gitignore +++ b/.gitignore @@ -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 + diff --git a/bin/clean.js b/bin/clean.js index f7ec9e3..6bda71a 100644 --- a/bin/clean.js +++ b/bin/clean.js @@ -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')) diff --git a/bin/lib.js b/bin/lib.js new file mode 100644 index 0000000..c9d160b --- /dev/null +++ b/bin/lib.js @@ -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() diff --git a/bin/pages.js b/bin/pages.js index 4e9ac34..a2e1469 100644 --- a/bin/pages.js +++ b/bin/pages.js @@ -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...') diff --git a/bin/tools.js b/bin/tools.js new file mode 100644 index 0000000..8ea4c5d --- /dev/null +++ b/bin/tools.js @@ -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} diff --git a/package.json b/package.json index 7a10d48..96c19b4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/pages/home.vue b/src/pages/home.vue index 8255e4f..9e2ae58 100644 --- a/src/pages/home.vue +++ b/src/pages/home.vue @@ -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})) diff --git a/src/project/build.js b/src/project/build.js index 9ff3e71..e0ff30a 100644 --- a/src/project/build.js +++ b/src/project/build.js @@ -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() - }) - } - }) - }) -} /** * 初始化打包配置文件 */