fix: 编译库代码

This commit is contained in:
2025-02-05 01:40:07 +08:00
parent 8795337a11
commit 4cb0d7cdad
2 changed files with 196 additions and 194 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
import { default as Koa } from 'koa';
export default CreateServer; export default CreateServer;
/** /**
* 创建服务 * 创建服务
* port: 服务端口号 * port: 服务端口号
*/ */
declare function CreateServer(port?: number): void; declare function CreateServer(port?: number): Promise<Koa<Koa.DefaultState, Koa.DefaultContext>>;
+194 -193
View File
@@ -1,46 +1,46 @@
import F from "yaml"; import G from "yaml";
import B from "fs"; import k from "fs";
import G from "cuid"; import U from "cuid";
import U from "lodash"; import A from "lodash";
import w, { dirname as R, join as h } from "path"; import w, { dirname as P, join as h } from "path";
import { JSONFile as m, Low as D } from "lowdb"; import { JSONFile as m, Low as D } from "lowdb";
import { fileURLToPath as $ } from "url"; import { fileURLToPath as $ } from "url";
import A from "koa"; import M from "koa";
import M from "koa-router"; import T from "koa-router";
import T from "koa-body"; import z from "koa-body";
import z from "koa-static"; import C from "koa-static";
import C from "koa-mount"; import E from "koa-mount";
const j = class j { const v = class v {
static initConfig() { static initConfig() {
this.config = F.parse(B.readFileSync("./config.yml").toString()); this.config = G.parse(k.readFileSync("./config.yml").toString());
} }
}; };
j.config = null; v.config = null;
let u = j; let u = v;
const b = R($( const b = P($(
import.meta.url import.meta.url
)), L = (n, a) => { )), L = (n, t) => {
let t = ""; let a = "";
return u.config && u.config.db && u.config.db[n] ? t = u.config.path + u.config.db[n] : t = a, t; return u.config && u.config.db && u.config.db[n] ? a = u.config.path + u.config.db[n] : a = t, a;
}, E = async () => {
let n = L("db", h(b, "db.json"));
const a = new m(n), t = new D(a);
return await t.read(), t;
}, I = async () => { }, I = async () => {
let n = L("resourceDB", h(b, "resourceDB.json")); let n = L("db", h(b, "db.json"));
const a = new m(n), t = new D(a); const t = new m(n), a = new D(t);
return await t.read(), t; return await a.read(), a;
}, O = async () => { }, O = async () => {
let n = L("backupDB", h(b, "backupDB.json")); let n = L("resourceDB", h(b, "resourceDB.json"));
const a = new m(n), t = new D(a); const t = new m(n), a = new D(t);
return await t.read(), t; return await a.read(), a;
}, J = async () => { }, J = async () => {
let n = L("backupDB", h(b, "backupDB.json"));
const t = new m(n), a = new D(t);
return await a.read(), a;
}, K = async () => {
let n = L("logDB", h(b, "logDB.json")); let n = L("logDB", h(b, "logDB.json"));
const a = new m(n), t = new D(a); const t = new m(n), a = new D(t);
return await t.read(), t; return await a.read(), a;
}, y = class y { }, f = class f {
static async init() { static async init() {
this.baseDB = await E(), this.resourceDB = await I(), this.backupDB = await O(), this.logDB = await J(); this.baseDB = await I(), this.resourceDB = await O(), this.backupDB = await J(), this.logDB = await K();
} }
static getBackupDb() { static getBackupDb() {
return this.backupDB; return this.backupDB;
@@ -49,22 +49,22 @@ const b = R($(
return this.logDB; return this.logDB;
} }
}; };
y.baseDB = null, y.resourceDB = null, y.backupDB = null, y.logDB = null; f.baseDB = null, f.resourceDB = null, f.backupDB = null, f.logDB = null;
let g = y; let y = f;
class v { class R {
static async addDataLog({ static async addDataLog({
tableName: a, tableName: t,
type: t, type: a,
data: i data: i
}) { }) {
const e = await g.getLogDb(); const e = await y.getLogDb();
return p.create({ return p.create({
db: e, db: e,
name: "dataLog", name: "dataLog",
data: { data: {
dataId: i.id, dataId: i.id,
type: t, type: a,
tableName: a, tableName: t,
data: i, data: i,
// 待生效 // 待生效
status: 0, status: 0,
@@ -73,20 +73,20 @@ class v {
}); });
} }
static async modifyDataLogStatus({ static async modifyDataLogStatus({
id: a id: t
}) { }) {
const t = await g.getLogDb(), { const a = await y.getLogDb(), {
data: i, data: i,
code: e code: e
} = await p.retrieveSingle({ } = await p.retrieveSingle({
db: t, db: a,
name: "dataLog", name: "dataLog",
params: { params: {
id: a id: t
} }
}); });
e == 0 && await p.update({ e == 0 && await p.update({
db: t, db: a,
name: "dataLog", name: "dataLog",
data: { data: {
...i, ...i,
@@ -96,38 +96,38 @@ class v {
}); });
} }
} }
class S { class j {
constructor({ constructor({
name: a, name: t,
type: t type: a
}) { }) {
this.name = "", this.type = "", this.id = null, this.name = a, this.type = t; this.name = "", this.type = "", this.id = null, this.name = t, this.type = a;
} }
async addDataLog({ async addDataLog({
data: a data: t
}) { }) {
const { const {
name: t, name: a,
type: i type: i
} = this; } = this;
if (t != "dataLog") { if (a != "dataLog") {
const { const {
data: e data: e
} = await v.addDataLog({ } = await R.addDataLog({
tableName: t, tableName: a,
type: i, type: i,
data: a data: t
}); });
this.id = e.id; this.id = e.id;
} }
} }
async modifyDataLogStatus() { async modifyDataLogStatus() {
const { const {
name: a, name: t,
id: t id: a
} = this; } = this;
a != "dataLog" && t && await v.modifyDataLogStatus({ t != "dataLog" && a && await R.modifyDataLogStatus({
id: t id: a
}); });
} }
} }
@@ -138,25 +138,25 @@ class p {
* @returns * @returns
*/ */
static async retrieveList({ static async retrieveList({
db: a, db: t,
name: t, name: a,
params: i params: i
}) { }) {
await a.read(); await t.read();
const e = a.data[t].filter((c) => { const e = t.data[a].filter((l) => {
let l = !0; let c = !0;
for (let r in i) for (let r in i)
if (!["page", "pageSize"].includes(r) && (typeof c[r] == "string" ? l = c[r].indexOf(i[r]) > -1 : typeof c[r] == "number" ? l = c[r] == i[r] : c[r] instanceof Array && (l = c[r].includes(i[r])), !l)) if (!["page", "pageSize"].includes(r) && (typeof l[r] == "string" ? c = l[r].indexOf(i[r]) > -1 : typeof l[r] == "number" ? c = l[r] == i[r] : l[r] instanceof Array && (c = l[r].includes(i[r])), !c))
break; break;
return l; return c;
}), { }), {
page: o, page: s,
pageSize: s pageSize: o
} = i; } = i;
let d = e; let d = e;
return o && s && (d = e.slice((o - 1) * s, o * s)), { return s && o && (d = e.slice((s - 1) * o, s * o)), {
code: 0, code: 0,
data: d.sort((c, l) => c.sort - l.sort) || [], data: d.sort((l, c) => l.sort - c.sort) || [],
total: e.length total: e.length
}; };
} }
@@ -165,16 +165,16 @@ class p {
* @param {*} param0 * @param {*} param0
*/ */
static async retrieveSingle({ static async retrieveSingle({
db: a, db: t,
name: t, name: a,
params: i params: i
}) { }) {
await a.read(); await t.read();
const e = a.data[t], o = i; const e = t.data[a], s = i;
let s = {}; let o = {};
return o.id && e && (s = e.find((d) => d.id == o.id)), { return s.id && e && (o = e.find((d) => d.id == s.id)), {
code: 0, code: 0,
data: s || {} data: o || {}
}; };
} }
/** /**
@@ -182,22 +182,22 @@ class p {
* @param {*} param0 * @param {*} param0
*/ */
static async update({ static async update({
db: a, db: t,
name: t, name: a,
data: i data: i
}) { }) {
await a.read(), a.data[t] || (a.data[t] = []); await t.read(), t.data[a] || (t.data[a] = []);
const e = a.data[t], o = i.id; const e = t.data[a], s = i.id;
if (o && e) { if (s && e) {
const s = e.find((c) => c.id == o); const o = e.find((l) => l.id == s);
s && U.merge(s, i); o && A.merge(o, i);
const d = new S({ const d = new j({
name: t, name: a,
type: "upd" type: "upd"
}); });
await d.addDataLog({ await d.addDataLog({
data: s data: o
}), await a.write(), await d.modifyDataLogStatus(); }), await t.write(), await d.modifyDataLogStatus();
} }
return { return {
code: 0 code: 0
@@ -208,20 +208,20 @@ class p {
* @param {*} param0 * @param {*} param0
*/ */
static async create({ static async create({
db: a, db: t,
name: t, name: a,
data: i data: i
}) { }) {
await a.read(), a.data[t] || (a.data[t] = []); await t.read(), t.data[a] || (t.data[a] = []);
const e = a.data[t]; const e = t.data[a];
i.id = G(), i.createTime = /* @__PURE__ */ new Date(), e.push(i); i.id = U(), i.createTime = /* @__PURE__ */ new Date(), e.push(i);
const o = new S({ const s = new j({
name: t, name: a,
type: "add" type: "add"
}); });
return await o.addDataLog({ return await s.addDataLog({
data: i data: i
}), await a.write(), await o.modifyDataLogStatus(), { }), await t.write(), await s.modifyDataLogStatus(), {
code: 0, code: 0,
data: i data: i
}; };
@@ -232,58 +232,58 @@ class p {
* @returns * @returns
*/ */
static async delete({ static async delete({
db: a, db: t,
name: t, name: a,
data: i data: i
}) { }) {
await a.read(), a.data[t] || (a.data[t] = []); await t.read(), t.data[a] || (t.data[a] = []);
const e = a.data[t], o = i.ids; const e = t.data[a], s = i.ids;
if (o.length > 0 && e) { if (s.length > 0 && e) {
let s = null; let o = null;
const d = []; const d = [];
for (let l = 0; l < o.length; l++) { for (let c = 0; c < s.length; c++) {
s = o[l]; o = s[c];
const r = e.findIndex((f) => f.id == s); const r = e.findIndex((S) => S.id == o);
r != -1 && (d.push(e[r]), e.splice(r, 1)); r != -1 && (d.push(e[r]), e.splice(r, 1));
} }
const c = new S({ const l = new j({
name: t, name: a,
type: "del" type: "del"
}); });
await c.addDataLog({ await l.addDataLog({
data: d data: d
}), await a.write(), await c.modifyDataLogStatus(); }), await t.write(), await l.modifyDataLogStatus();
} }
return { return {
code: 0 code: 0
}; };
} }
} }
class et { class it {
constructor({ constructor({
router: a, router: t,
db: t, db: a,
name: i name: i
}) { }) {
this.name = "", this.db = null, this.router = null, this.router = a, this.db = t, this.name = i; this.name = "", this.db = null, this.router = null, this.router = t, this.db = a, this.name = i;
} }
/** /**
* 创建列表数据路由 * 创建列表数据路由
*/ */
createGetList() { createGetList() {
const { const {
name: a, name: t,
db: t, db: a,
router: i router: i
} = this; } = this;
i.post(`/api/${a}/list`, async (e) => { i.post(`/api/${t}/list`, async (e) => {
e.type = "application/json"; e.type = "application/json";
const o = e.request.body, s = await p.retrieveList({ const s = e.request.body, o = await p.retrieveList({
db: t, db: a,
name: a, name: t,
params: o params: s
}); });
e.body = s; e.body = o;
}); });
} }
/** /**
@@ -291,18 +291,18 @@ class et {
*/ */
createGetDetail() { createGetDetail() {
const { const {
name: a, name: t,
db: t, db: a,
router: i router: i
} = this; } = this;
i.get(`/api/${a}`, async (e) => { i.get(`/api/${t}`, async (e) => {
e.type = "application/json"; e.type = "application/json";
const o = e.request.query, s = await p.retrieveSingle({ const s = e.request.query, o = await p.retrieveSingle({
db: t, db: a,
name: a, name: t,
params: o params: s
}); });
e.body = s; e.body = o;
}); });
} }
/** /**
@@ -310,18 +310,18 @@ class et {
*/ */
createUpdate() { createUpdate() {
const { const {
name: a, name: t,
db: t, db: a,
router: i router: i
} = this; } = this;
i.post(`/api/${a}/upd`, async (e) => { i.post(`/api/${t}/upd`, async (e) => {
e.type = "application/json"; e.type = "application/json";
const o = e.request.body, s = await p.update({ const s = e.request.body, o = await p.update({
db: t, db: a,
name: a, name: t,
data: o data: s
}); });
e.body = s; e.body = o;
}); });
} }
/** /**
@@ -329,18 +329,18 @@ class et {
*/ */
createAdd() { createAdd() {
const { const {
name: a, name: t,
db: t, db: a,
router: i router: i
} = this; } = this;
i.post(`/api/${a}/add`, async (e) => { i.post(`/api/${t}/add`, async (e) => {
e.type = "application/json"; e.type = "application/json";
const o = e.request.body, s = await p.create({ const s = e.request.body, o = await p.create({
db: t, db: a,
name: a, name: t,
data: o data: s
}); });
e.body = s; e.body = o;
}); });
} }
/** /**
@@ -348,76 +348,77 @@ class et {
*/ */
createDelete() { createDelete() {
const { const {
name: a, name: t,
db: t, db: a,
router: i router: i
} = this; } = this;
i.post(`/api/${a}/delete`, async (e) => { i.post(`/api/${t}/delete`, async (e) => {
e.type = "application/json"; e.type = "application/json";
const o = e.request.body, s = await p.delete({ const s = e.request.body, o = await p.delete({
db: t, db: a,
name: a, name: t,
data: o data: s
}); });
e.body = s; e.body = o;
}); });
} }
creaetBaseRouter() { creaetBaseRouter() {
this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete(); this.createGetList(), this.createGetDetail(), this.createAdd(), this.createUpdate(), this.createDelete();
} }
} }
const q = R($(
import.meta.url
));
u.initConfig(); u.initConfig();
(async () => await g.init())(); (async () => await y.init())();
function it(n = 4873) { async function st(n = 4873) {
const a = new M(), t = new A(); const t = P($(
t.use(T()), t.use( import.meta.url
C("/resource/static", z( )), a = process.cwd(), i = new T(), e = new M();
w.join(q, "./static"), e.use(z()), e.use(
E("/resource/static", C(
w.join(a, "./static"),
{} {}
)) ))
), t.use(a.routes()), t.use(a.allowedMethods()); ), e.use(i.routes()), e.use(i.allowedMethods());
const e = (d) => { const o = async (c) => {
B.readdir(d, async (c, l) => { k.readdir(c, async (r, S) => {
if (c) { if (r) {
console.error("Error reading directory:", c); console.error("Error reading directory:", r);
return; return;
} }
for (const r of l) for (const B of S)
try { try {
const f = w.join(d, r), k = await B.statSync(f); const g = w.join(c, B), q = await k.statSync(g);
if (k.isDirectory()) if (q.isDirectory())
await e(f); await o(g);
else if (k.isFile()) { else if (q.isFile()) {
const P = w.join(d, r); const F = w.join(c, B);
(await import("./" + w.relative(q, P))).default(a); (await import("./" + w.relative(t, F))).default(i);
} }
} catch (f) { } catch (g) {
console.error(`Error importing ${r}:`, f); console.error(`Error importing ${B}:`, g);
} }
}); });
}; };
(async () => await e("./routers"))(); await (async () => {
let o = null; await o("./routers");
(() => {
o = t.listen(n, () => {
console.log("server is listen in ", n);
}), o.on("close", (d, c) => {
o = null, console.log("server is close");
});
})(); })();
let d = null;
return (() => {
d = e.listen(n, () => {
console.log("server is listen in ", n);
}), d.on("close", (c, r) => {
d = null, console.log("server is close");
});
})(), e;
} }
export { export {
p as CURDHelper, p as CURDHelper,
u as ConfigHelper, u as ConfigHelper,
it as CreateServer, st as CreateServer,
g as DBHelper, y as DBHelper,
v as LogHelper, R as LogHelper,
et as RouterFactor, it as RouterFactor,
O as getBackupDb, J as getBackupDb,
E as getDb, I as getDb,
J as getLogDb, K as getLogDb,
I as getResourceDb O as getResourceDb
}; };