增加手动同步库信息脚本

This commit is contained in:
aixianling
2022-09-02 15:25:46 +08:00
parent 37c6f38354
commit 5a287a627a
3 changed files with 110 additions and 1 deletions

38
bin/appsSync.js Normal file
View File

@@ -0,0 +1,38 @@
const axios = require("axios");
const {chalkTag, findApp, fs} = require("./tools");
const compiler = require('vue-template-compiler')
const saveApps = app => {
if (app.list.length > 0) {
return axios.post("http://192.168.1.87:12525/node/wechatapps/addOrUpdate", app, {timeout: 1000}).then(res => {
if (res.data.code == 0) chalkTag.done("产品库目录已同步至后台数据库...")
}).catch(() => 0)
} else return Promise.reject("没有应用")
}
const getAppInfo = (file, apps) => {
if (/[\\\/](App[^\\\/]+)\.vue$/g.test(file)) {
const name = file.replace(/.+[\\\/](App[^\\\/]+)\.vue$/, '$1'),
source = fs.readFileSync(file).toString(),
parsed = compiler.parseComponent(source),
script = parsed.script?.content || "",
label = script.match(/label:[^,]+/)?.[0]?.replace(/.+["']([^"']+).+/, '$1')
apps.push({
id: file.replace(/\.vue$/, '').replace(/[\\\/]/g, '_'),
label: label || name,
libPath: file.replace(/\.vue$/, ''),
name,
type: 'web'
})
}
}
const sync = () => {
chalkTag.info("开始扫描库工程...")
const list = []
Promise.all([
findApp('packages', app => getAppInfo(app, list)),
findApp('project', app => getAppInfo(app, list)),
]).then(() => {
chalkTag.info("正在同步...")
saveApps({type: "web", list}).catch(() => 0).finally(() => chalkTag.done("同步成功!"))
})
}
sync()

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(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}