chore: 优化 db 路径查找方式与初始化脚本

This commit is contained in:
idootop 2024-06-16 23:01:58 +08:00
parent ecbb793bdf
commit 22c8af0d0a
4 changed files with 33 additions and 43 deletions

View File

@ -12,8 +12,8 @@
- ✅ 登录凭证过期后自动刷新 token https://github.com/idootop/mi-gpt/issues/76
- ✅ 优化网络请求错误重试策略(消息/播放状态轮询)
- ✅ 优化 db 路径查找方式与初始化脚本
- ✅ 移除 TTS 不发音字符emoji
- ✅ 优化 db 路径查找方式
## 📚 文档

View File

@ -25,7 +25,7 @@
"build": "npx -y prisma generate && tsup",
"db:gen": "npx -y prisma migrate dev --name init",
"db:reset": "rm -f .mi.json .bot.json prisma/app.db prisma/app.db-journal",
"postinstall": "npx prisma migrate dev --name hello"
"postinstall": "npx -y prisma migrate dev --name hello"
},
"dependencies": {
"@prisma/client": "^5.14.0",

View File

@ -37,16 +37,16 @@ export function getDBInfo() {
rootDir = "/" + rootDir; // linux root path
}
const dbPath = rootDir + "/prisma/app.db";
const withSchema = `--schema ${rootDir}/prisma/schema.prisma`;
return { dbPath, withSchema };
return { rootDir, dbPath };
}
export async function initDB(debug = false) {
const { dbPath, withSchema } = getDBInfo();
const { rootDir, dbPath } = getDBInfo();
if (!exists(dbPath)) {
await deleteFile(".bot.json");
await Shell.run(`npx prisma migrate dev --name init ${withSchema}`, {
silent: debug ? false : true,
await Shell.run(`npm run postinstall`, {
cwd: rootDir,
silent: !debug,
});
}
const success = exists(dbPath);

View File

@ -1,48 +1,38 @@
import { exec as execSync, spawn } from "child_process";
import { exec as execSync } from "child_process";
import { promisify } from "util";
import { isNotEmpty } from "./is";
const exec = promisify(execSync);
interface StdIO {
stdout: string;
stderr: string;
stdout?: string;
stderr?: string;
error?: any;
}
export class Shell {
static async run(command: string, options?: { silent?: boolean }) {
const { silent } = options ?? {};
if (silent) {
return new Promise<StdIO>((resolve) => {
const commands = command.split(" ").filter((e) => isNotEmpty(e.trim()));
const bin = commands[0];
const [, ...args] = commands;
let res: StdIO = {
stdout: "",
stderr: "",
};
try {
const ps = spawn(bin, args, {
stdio: "ignore",
});
ps.stdout?.on("data", (data) => {
res.stdout += data;
});
ps.stderr?.on("data", (data) => {
res.stderr += data;
});
ps.on("close", () => {
resolve(res);
});
} catch {
resolve(res);
}
});
}
return exec(command);
}
static get args() {
return process.argv.slice(2);
}
static async run(
command: string,
options?: { silent?: boolean; cwd?: string }
): Promise<StdIO> {
const { silent, cwd } = options ?? {};
try {
const { stdout, stderr } = await exec(command, { cwd });
if (!silent) {
console.log(`stdout: ${stdout}`);
if (stderr) {
console.error(`stderr: ${stderr}`);
}
}
return { stdout, stderr };
} catch (error) {
if (!silent) {
console.error(`error: ${error}`);
}
return { error };
}
}
}