Refactor error handling and localDb structure, and fix usage tracking bug.
This commit is contained in:
@@ -2,32 +2,12 @@ import { Low } from "lowdb";
|
||||
import { JSONFile } from "lowdb/node";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import path from "node:path";
|
||||
import os from "node:os";
|
||||
import fs from "node:fs";
|
||||
import lockfile from "proper-lockfile";
|
||||
import { DATA_DIR } from "@/lib/dataDir.js";
|
||||
|
||||
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
|
||||
const isCloud = typeof caches !== 'undefined' || typeof caches === 'object';
|
||||
|
||||
function getAppName() {
|
||||
return "9router";
|
||||
}
|
||||
|
||||
function getUserDataDir() {
|
||||
if (isCloud) return "/tmp";
|
||||
if (process.env.DATA_DIR) return process.env.DATA_DIR;
|
||||
|
||||
const platform = process.platform;
|
||||
const homeDir = os.homedir();
|
||||
const appName = getAppName();
|
||||
|
||||
if (platform === "win32") {
|
||||
return path.join(process.env.APPDATA || path.join(homeDir, "AppData", "Roaming"), appName);
|
||||
}
|
||||
return path.join(homeDir, `.${appName}`);
|
||||
}
|
||||
|
||||
const DATA_DIR = getUserDataDir();
|
||||
const DB_FILE = isCloud ? null : path.join(DATA_DIR, "db.json");
|
||||
|
||||
if (!isCloud && !fs.existsSync(DATA_DIR)) {
|
||||
|
||||
Reference in New Issue
Block a user