Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: implement workspace registry in ix init with root resolution

riley0227f6aeb9036394
diff --git a/ix-cli/src/cli/commands/init.ts b/ix-cli/src/cli/commands/init.ts
index 32c48554..4c2e177b 100644
--- a/ix-cli/src/cli/commands/init.ts
+++ b/ix-cli/src/cli/commands/init.ts
@@ -1,10 +1,11 @@
import { mkdir, writeFile, readFile } from "node:fs/promises";
import { existsSync } from "node:fs";
-import { join } from "node:path";
+import { join, basename, resolve } from "node:path";
import { homedir } from "node:os";
+import { randomUUID } from "node:crypto";
import type { Command } from "commander";
import { IxClient } from "../../client/api.js";
-import { getEndpoint } from "../config.js";
+import { getEndpoint, loadConfig, saveConfig, type WorkspaceConfig } from "../config.js";
export const IX_MARKER_START = "<!-- IX-MEMORY START -->";
export const IX_MARKER_END = "<!-- IX-MEMORY END -->";
@@ -79,6 +80,38 @@ export function registerInitCommand(program: Command): void {
);
console.log(" [ok] Created ~/.ix/config.yaml");
+ // 2b. Register workspace
+ const rootPath = resolve(process.cwd());
+ const workspaceName = basename(rootPath);
+
+ const config = loadConfig();
+ const existingWorkspaces = config.workspaces ?? [];
+ const alreadyRegistered = existingWorkspaces.find(w => w.root_path === rootPath);
+
+ if (alreadyRegistered) {
+ console.log(` [ok] Workspace already registered: ${alreadyRegistered.workspace_name} (${alreadyRegistered.workspace_id})`);
+ } else {
+ const hasDefault = existingWorkspaces.some(w => w.default);
+ const newWs: WorkspaceConfig = {
+ workspace_id: randomUUID().slice(0, 8),
+ workspace_name: workspaceName,
+ root_path: rootPath,
+ default: !hasDefault,
+ };
+
+ if (hasDefault) {
+ const defaultWs = existingWorkspaces.find(w => w.default)!;
+ console.log(`\n A default workspace already exists:`);
+ console.log(` ${defaultWs.workspace_name} at ${defaultWs.root_path}`);
+ console.log(` New workspace: ${workspaceName} at ${rootPath}`);
+ console.log(` (Set as non-default. Use 'ix init --set-default' to change.)\n`);
+ }
+
+ config.workspaces = [...existingWorkspaces, newWs];
+ saveConfig(config);
+ console.log(` [ok] Registered workspace: ${workspaceName} (${newWs.workspace_id})`);
+ }
+
// 3. Add IX block to CLAUDE.md (using markers for clean add/remove)
if (existsSync("CLAUDE.md")) {
const existing = await readFile("CLAUDE.md", "utf-8");
diff --git a/ix-cli/src/cli/config.ts b/ix-cli/src/cli/config.ts
index 39a544a5..ccd4c79b 100644
--- a/ix-cli/src/cli/config.ts
+++ b/ix-cli/src/cli/config.ts
@@ -1,11 +1,20 @@
-import { readFileSync, existsSync } from "node:fs";
+import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
-import { parse } from "yaml";
+import { execSync } from "node:child_process";
+import { parse, stringify } from "yaml";
+
+export interface WorkspaceConfig {
+ workspace_id: string;
+ workspace_name: string;
+ root_path: string;
+ default: boolean;
+}
export interface IxConfig {
endpoint: string;
format: string;
+ workspaces?: WorkspaceConfig[];
}
const defaultConfig: IxConfig = {
@@ -25,6 +34,45 @@ export function loadConfig(): IxConfig {
}
}
+export function saveConfig(config: IxConfig): void {
+ const configPath = join(homedir(), ".ix", "config.yaml");
+ writeFileSync(configPath, stringify(config));
+}
+
export function getEndpoint(): string {
return process.env.IX_ENDPOINT || loadConfig().endpoint;
}
+
+export function loadWorkspaces(): WorkspaceConfig[] {
+ const config = loadConfig();
+ return config.workspaces ?? [];
+}
+
+export function findWorkspaceForCwd(cwd: string): WorkspaceConfig | undefined {
+ const workspaces = loadWorkspaces();
+ return workspaces
+ .filter(w => cwd.startsWith(w.root_path))
+ .sort((a, b) => b.root_path.length - a.root_path.length)[0];
+}
+
+export function getDefaultWorkspace(): WorkspaceConfig | undefined {
+ return loadWorkspaces().find(w => w.default);
+}
+
+export function resolveWorkspaceRoot(explicitRoot?: string): string {
+ // 1. Explicit --root
+ if (explicitRoot) return explicitRoot;
+ // 2. Nearest initialized workspace containing cwd
+ const cwd = process.cwd();
+ const nearest = findWorkspaceForCwd(cwd);
+ if (nearest) return nearest.root_path;
+ // 3. Configured default workspace
+ const defaultWs = getDefaultWorkspace();
+ if (defaultWs) return defaultWs.root_path;
+ // 4. Git root
+ try {
+ return execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim();
+ } catch {}
+ // 5. cwd fallback
+ return cwd;
+}