Add CLI setup and QQ webhook integration
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import process from "node:process";
|
||||
import type { AppConfig } from "../config.js";
|
||||
import { loadConfigFromPath, parseConfig } from "../config.js";
|
||||
|
||||
export interface LoadedConfigFile {
|
||||
path: string;
|
||||
config: AppConfig;
|
||||
exists: boolean;
|
||||
source: "explicit" | "env" | "local" | "example";
|
||||
}
|
||||
|
||||
export function projectRoot(): string {
|
||||
return path.resolve(new URL("../..", import.meta.url).pathname);
|
||||
}
|
||||
|
||||
export function resolveCliConfigPath(configPath?: string): { path: string; exists: boolean; source: LoadedConfigFile["source"] } {
|
||||
if (configPath) {
|
||||
const resolved = path.resolve(configPath);
|
||||
return { path: resolved, exists: fs.existsSync(resolved), source: "explicit" };
|
||||
}
|
||||
|
||||
if (process.env.GORI_GATEWAY_CONFIG) {
|
||||
const resolved = path.resolve(process.env.GORI_GATEWAY_CONFIG);
|
||||
return { path: resolved, exists: fs.existsSync(resolved), source: "env" };
|
||||
}
|
||||
|
||||
const local = path.resolve("config.json");
|
||||
if (fs.existsSync(local)) return { path: local, exists: true, source: "local" };
|
||||
|
||||
return { path: path.resolve("config.json"), exists: false, source: "example" };
|
||||
}
|
||||
|
||||
export function loadConfigFile(configPath?: string): LoadedConfigFile {
|
||||
const resolved = resolveCliConfigPath(configPath);
|
||||
if (resolved.exists) {
|
||||
return {
|
||||
path: resolved.path,
|
||||
config: loadConfigFromPath(resolved.path),
|
||||
exists: true,
|
||||
source: resolved.source
|
||||
};
|
||||
}
|
||||
|
||||
const examplePath = path.join(projectRoot(), "config.example.json");
|
||||
const raw = fs.readFileSync(examplePath, "utf8");
|
||||
const config = parseConfig(JSON.parse(raw) as unknown);
|
||||
return {
|
||||
path: resolved.path,
|
||||
config,
|
||||
exists: false,
|
||||
source: "example"
|
||||
};
|
||||
}
|
||||
|
||||
export function writeConfigFile(configPath: string, config: AppConfig): void {
|
||||
const parsed = parseConfig(config);
|
||||
fs.mkdirSync(path.dirname(configPath), { recursive: true });
|
||||
fs.writeFileSync(configPath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8");
|
||||
}
|
||||
|
||||
export function readConfigJson(configPath?: string): unknown {
|
||||
const loaded = loadConfigFile(configPath);
|
||||
return loaded.config;
|
||||
}
|
||||
Reference in New Issue
Block a user