refactor(src): split oversized modules
This commit is contained in:
58
src/infra/state-migrations.fs.ts
Normal file
58
src/infra/state-migrations.fs.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import fs from "node:fs";
|
||||
|
||||
import JSON5 from "json5";
|
||||
|
||||
export type SessionEntryLike = {
|
||||
sessionId?: string;
|
||||
updatedAt?: number;
|
||||
} & Record<string, unknown>;
|
||||
|
||||
export function safeReadDir(dir: string): fs.Dirent[] {
|
||||
try {
|
||||
return fs.readdirSync(dir, { withFileTypes: true });
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function existsDir(dir: string): boolean {
|
||||
try {
|
||||
return fs.existsSync(dir) && fs.statSync(dir).isDirectory();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function ensureDir(dir: string) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
export function fileExists(p: string): boolean {
|
||||
try {
|
||||
return fs.existsSync(p) && fs.statSync(p).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isLegacyWhatsAppAuthFile(name: string): boolean {
|
||||
if (name === "creds.json" || name === "creds.json.bak") return true;
|
||||
if (!name.endsWith(".json")) return false;
|
||||
return /^(app-state-sync|session|sender-key|pre-key)-/.test(name);
|
||||
}
|
||||
|
||||
export function readSessionStoreJson5(storePath: string): {
|
||||
store: Record<string, SessionEntryLike>;
|
||||
ok: boolean;
|
||||
} {
|
||||
try {
|
||||
const raw = fs.readFileSync(storePath, "utf-8");
|
||||
const parsed = JSON5.parse(raw);
|
||||
if (parsed && typeof parsed === "object") {
|
||||
return { store: parsed as Record<string, SessionEntryLike>, ok: true };
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return { store: {}, ok: false };
|
||||
}
|
||||
Reference in New Issue
Block a user