-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenv.ts
More file actions
38 lines (31 loc) · 1023 Bytes
/
env.ts
File metadata and controls
38 lines (31 loc) · 1023 Bytes
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
37
38
import { existsSync, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import { z } from "zod";
export const CONFIG_DIR = join(homedir(), ".scrapegraphai");
export const CONFIG_PATH = join(CONFIG_DIR, "config.json");
function loadConfigFile(): Record<string, unknown> {
if (!existsSync(CONFIG_PATH)) return {};
try {
return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
} catch {
return {};
}
}
const EnvSchema = z.object({
apiKey: z.string().optional(),
debug: z.boolean().default(false),
timeoutS: z.number().int().positive().default(120),
});
export type Env = z.infer<typeof EnvSchema>;
function resolve(): Env {
const config = loadConfigFile();
return EnvSchema.parse({
apiKey: process.env.SGAI_API_KEY || (config["api-key"] as string) || undefined,
debug: process.env.JUST_SCRAPE_DEBUG === "1",
timeoutS: process.env.JUST_SCRAPE_TIMEOUT_S
? Number(process.env.JUST_SCRAPE_TIMEOUT_S)
: undefined,
});
}
export const env = resolve();