co-mono/packages/pods/src/config.ts
Mario Zechner a74c5da112 Initial monorepo setup with npm workspaces and dual TypeScript configuration
- Set up npm workspaces for three packages: pi-tui, pi-agent, and pi (pods)
- Implemented dual TypeScript configuration:
  - Root tsconfig.json with path mappings for development and type checking
  - Package-specific tsconfig.build.json for clean production builds
- Configured lockstep versioning with sync script for inter-package dependencies
- Added comprehensive documentation for development and publishing workflows
- All packages at version 0.5.0 ready for npm publishing
2025-08-09 17:18:38 +02:00

80 lines
2 KiB
TypeScript

import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { homedir } from "os";
import { join } from "path";
import type { Config, Pod } from "./types.js";
// Get config directory from env or use default
const getConfigDir = (): string => {
const configDir = process.env.PI_CONFIG_DIR || join(homedir(), ".pi");
if (!existsSync(configDir)) {
mkdirSync(configDir, { recursive: true });
}
return configDir;
};
const getConfigPath = (): string => {
return join(getConfigDir(), "pods.json");
};
export const loadConfig = (): Config => {
const configPath = getConfigPath();
if (!existsSync(configPath)) {
// Return empty config if file doesn't exist
return { pods: {} };
}
try {
const data = readFileSync(configPath, "utf-8");
return JSON.parse(data);
} catch (e) {
console.error(`Error reading config: ${e}`);
return { pods: {} };
}
};
export const saveConfig = (config: Config): void => {
const configPath = getConfigPath();
try {
writeFileSync(configPath, JSON.stringify(config, null, 2));
} catch (e) {
console.error(`Error saving config: ${e}`);
process.exit(1);
}
};
export const getActivePod = (): { name: string; pod: Pod } | null => {
const config = loadConfig();
if (!config.active || !config.pods[config.active]) {
return null;
}
return { name: config.active, pod: config.pods[config.active] };
};
export const addPod = (name: string, pod: Pod): void => {
const config = loadConfig();
config.pods[name] = pod;
// If no active pod, make this one active
if (!config.active) {
config.active = name;
}
saveConfig(config);
};
export const removePod = (name: string): void => {
const config = loadConfig();
delete config.pods[name];
// If this was the active pod, clear active
if (config.active === name) {
config.active = undefined;
}
saveConfig(config);
};
export const setActivePod = (name: string): void => {
const config = loadConfig();
if (!config.pods[name]) {
console.error(`Pod '${name}' not found`);
process.exit(1);
}
config.active = name;
saveConfig(config);
};