mirror of
https://github.com/GMGNAI/gmgn-skills.git
synced 2026-08-15 00:58:05 +00:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { config as loadDotenv } from "dotenv";
|
|
import { homedir } from "os";
|
|
import { join } from "path";
|
|
|
|
|
|
// Load global config first (~/.config/gmgn/.env, takes precedence), then project .env (supplements only)
|
|
loadDotenv({ path: join(homedir(), ".config", "gmgn", ".env"), override: true });
|
|
loadDotenv();
|
|
|
|
export interface Config {
|
|
apiKey: string;
|
|
privateKeyPem?: string;
|
|
host: string;
|
|
}
|
|
|
|
let _config: Config | null = null;
|
|
const PRIVATE_KEY_REQUIRED_MSG =
|
|
"GMGN_PRIVATE_KEY is required for critical-auth commands (swap, order, and follow-wallet commands)";
|
|
|
|
export function getConfig(requirePrivateKey = false): Config {
|
|
if (_config) {
|
|
if (requirePrivateKey && !_config.privateKeyPem) {
|
|
die(PRIVATE_KEY_REQUIRED_MSG);
|
|
}
|
|
return _config;
|
|
}
|
|
|
|
const apiKey = process.env.GMGN_API_KEY;
|
|
if (!apiKey) {
|
|
die("GMGN_API_KEY is required. Set it in your .env file or environment.");
|
|
}
|
|
|
|
let privateKeyPem: string | undefined;
|
|
const privateKey = process.env.GMGN_PRIVATE_KEY;
|
|
if (privateKey) {
|
|
// Support escaped newlines (e.g. from single-line .env values)
|
|
privateKeyPem = privateKey.replace(/\\n/g, "\n");
|
|
} else if (requirePrivateKey) {
|
|
die(PRIVATE_KEY_REQUIRED_MSG);
|
|
}
|
|
|
|
const host = "https://openapi.gmgn.ai";
|
|
_config = { apiKey: apiKey!, privateKeyPem, host };
|
|
return _config;
|
|
}
|
|
|
|
function die(msg: string): never {
|
|
console.error(`[gmgn-cli] Error: ${msg}`);
|
|
process.exit(1);
|
|
}
|