-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.ts
More file actions
36 lines (28 loc) · 1 KB
/
env.ts
File metadata and controls
36 lines (28 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { existsSync, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
export const CONFIG_DIR = join(homedir(), ".scrapegraphai");
export const CONFIG_PATH = join(CONFIG_DIR, "config.json");
if (process.env.JUST_SCRAPE_API_URL && !process.env.SGAI_API_URL)
process.env.SGAI_API_URL = process.env.JUST_SCRAPE_API_URL;
if (process.env.JUST_SCRAPE_DEBUG === "1" && !process.env.SGAI_DEBUG) process.env.SGAI_DEBUG = "1";
if (process.env.JUST_SCRAPE_TIMEOUT_S && !process.env.SGAI_TIMEOUT_S)
process.env.SGAI_TIMEOUT_S = process.env.JUST_SCRAPE_TIMEOUT_S;
function loadConfigFile(): Record<string, unknown> {
if (!existsSync(CONFIG_PATH)) return {};
try {
return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
} catch {
return {};
}
}
export type Env = {
apiKey?: string;
};
function resolve(): Env {
const config = loadConfigFile();
return {
apiKey: process.env.SGAI_API_KEY || (config["api-key"] as string) || undefined,
};
}
export const env = resolve();