@farthershore/cli 0.3.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.js ADDED
@@ -0,0 +1,58 @@
1
+ // ~/.farthershore/ config management
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { BUILD_API_URL } from "./build-info.js";
6
+ const CONFIG_DIR = join(homedir(), ".farthershore");
7
+ const CONFIG_FILE = join(CONFIG_DIR, "config.json");
8
+ const CREDENTIALS_FILE = join(CONFIG_DIR, "credentials.json");
9
+ function ensureDir(dir) {
10
+ if (!existsSync(dir))
11
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
12
+ }
13
+ // --- Config ---
14
+ const DEFAULT_CONFIG = {
15
+ apiUrl: BUILD_API_URL,
16
+ defaultFormat: "table",
17
+ };
18
+ export function loadConfig() {
19
+ ensureDir(CONFIG_DIR);
20
+ if (!existsSync(CONFIG_FILE))
21
+ return DEFAULT_CONFIG;
22
+ try {
23
+ // JSON.parse → `any`; cast to a partial of the typed shape so the
24
+ // spread doesn't propagate `any` into the returned union.
25
+ const parsed = JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
26
+ return { ...DEFAULT_CONFIG, ...parsed };
27
+ }
28
+ catch {
29
+ return DEFAULT_CONFIG;
30
+ }
31
+ }
32
+ export function saveConfig(config) {
33
+ ensureDir(CONFIG_DIR);
34
+ const current = loadConfig();
35
+ writeFileSync(CONFIG_FILE, JSON.stringify({ ...current, ...config }, null, 2) + "\n");
36
+ }
37
+ // --- Credentials ---
38
+ export function loadCredentials() {
39
+ if (!existsSync(CREDENTIALS_FILE))
40
+ return null;
41
+ try {
42
+ return JSON.parse(readFileSync(CREDENTIALS_FILE, "utf-8"));
43
+ }
44
+ catch {
45
+ return null;
46
+ }
47
+ }
48
+ export function saveCredentials(creds) {
49
+ ensureDir(CONFIG_DIR);
50
+ writeFileSync(CREDENTIALS_FILE, JSON.stringify(creds, null, 2) + "\n", {
51
+ mode: 0o600,
52
+ });
53
+ }
54
+ export function clearCredentials() {
55
+ if (existsSync(CREDENTIALS_FILE)) {
56
+ writeFileSync(CREDENTIALS_FILE, "{}");
57
+ }
58
+ }