SPB Git

spb/spbgit Public MIT

SPB Git — the platform hosting itself

JavaScript 73.9% CSS 11.7% Nunjucks 11.6% Shell 2.7%
2.2 KB · 68 lines javascript
Raw Blame History
1/**2 * ─────────────────────────────────────────────3 *  SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 *  Author  : Simon-Pierre Boucher6 *  Contact : contact@spboucher.ai7 *  File    : cli/lib/config.mjs8 *  Purpose : ~/.spbgit/config.json loading, saving, validation9 *  License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { readFileSync, writeFileSync, mkdirSync, chmodSync, existsSync } from 'node:fs';14import { homedir } from 'node:os';15import { join } from 'node:path';1617export const CONFIG_DIR = join(homedir(), '.spbgit');18export const CONFIG_PATH = join(CONFIG_DIR, 'config.json');1920const DEFAULTS = {21  server: 'https://git.spboucher.ai',22  token: '',23  workspace: join(homedir(), 'code'),24  author: { name: 'Simon-Pierre Boucher', email: 'contact@spboucher.ai' },25};2627/** Expand a leading `~` to the home directory. */28export function expandHome(path) {29  if (!path) return path;30  return path.startsWith('~') ? join(homedir(), path.slice(1)) : path;31}3233/**34 * @returns {object|null} parsed config or null when not initialized35 */36export function loadCliConfig() {37  if (!existsSync(CONFIG_PATH)) return null;38  try {39    const parsed = JSON.parse(readFileSync(CONFIG_PATH, 'utf8'));40    return { ...DEFAULTS, ...parsed, workspace: expandHome(parsed.workspace ?? DEFAULTS.workspace) };41  } catch {42    return null;43  }44}4546/**47 * Load config or exit(1) with a helpful message.48 * @returns {object}49 */50export function requireCliConfig() {51  const config = loadCliConfig();52  if (!config || !config.server) {53    console.error('spbgit is not configured. Run: spbgit init');54    process.exit(1);55  }56  return config;57}5859/**60 * Persist config with 600 permissions (contains the PAT).61 * @param {object} config62 */63export function saveCliConfig(config) {64  mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });65  writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });66  chmodSync(CONFIG_PATH, 0o600);67}68