mirror of
https://github.com/GMGNAI/gmgn-skills.git
synced 2026-08-24 05:28:06 +00:00
initial import
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { config as loadDotenv } from "dotenv";
|
||||
import { homedir } from "os";
|
||||
import { join } from "path";
|
||||
|
||||
|
||||
// Load global config first (~/.config/gmgn/.env), then project .env (project takes precedence)
|
||||
loadDotenv({ path: join(homedir(), ".config", "gmgn", ".env") });
|
||||
loadDotenv({ override: true });
|
||||
|
||||
export interface Config {
|
||||
apiKey: string;
|
||||
privateKeyPem?: string;
|
||||
host: string;
|
||||
}
|
||||
|
||||
let _config: Config | null = null;
|
||||
|
||||
export function getConfig(requirePrivateKey = false): Config {
|
||||
if (_config) {
|
||||
if (requirePrivateKey && !_config.privateKeyPem) {
|
||||
die("GMGN_PRIVATE_KEY is required for swap/order commands");
|
||||
}
|
||||
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("GMGN_PRIVATE_KEY is required for swap/order commands");
|
||||
}
|
||||
|
||||
const host = process.env.GMGN_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);
|
||||
}
|
||||
Reference in New Issue
Block a user