Simplify path logic

This commit is contained in:
Mateusz Tymek
2026-01-11 13:57:18 +01:00
parent 30953b3176
commit 8345fff6e9
4 changed files with 22 additions and 31 deletions

26
main.js
View File

@@ -170,6 +170,7 @@ var OpenCodeView = class extends import_obsidian2.ItemView {
const iframeContainer = this.contentEl.createDiv({ const iframeContainer = this.contentEl.createDiv({
cls: "opencode-iframe-container" cls: "opencode-iframe-container"
}); });
console.log("[OpenCode] Loading iframe with URL:", this.plugin.getServerUrl());
this.iframeEl = iframeContainer.createEl("iframe", { this.iframeEl = iframeContainer.createEl("iframe", {
cls: "opencode-iframe", cls: "opencode-iframe",
attr: { attr: {
@@ -414,13 +415,12 @@ var OpenCodeSettingTab = class extends import_obsidian3.PluginSettingTab {
// src/ProcessManager.ts // src/ProcessManager.ts
var import_child_process = require("child_process"); var import_child_process = require("child_process");
var ProcessManager = class { var ProcessManager = class {
constructor(settings, workingDirectory, projectDirectory, onStateChange) { constructor(settings, projectDirectory, onStateChange) {
this.process = null; this.process = null;
this.state = "stopped"; this.state = "stopped";
this.lastError = null; this.lastError = null;
this.earlyExitCode = null; this.earlyExitCode = null;
this.settings = settings; this.settings = settings;
this.workingDirectory = workingDirectory;
this.projectDirectory = projectDirectory; this.projectDirectory = projectDirectory;
this.onStateChange = onStateChange; this.onStateChange = onStateChange;
} }
@@ -461,7 +461,7 @@ var ProcessManager = class {
opencodePath: this.settings.opencodePath, opencodePath: this.settings.opencodePath,
port: this.settings.port, port: this.settings.port,
hostname: this.settings.hostname, hostname: this.settings.hostname,
cwd: this.workingDirectory, cwd: this.projectDirectory,
projectDirectory: this.projectDirectory projectDirectory: this.projectDirectory
}); });
this.process = (0, import_child_process.spawn)( this.process = (0, import_child_process.spawn)(
@@ -476,7 +476,7 @@ var ProcessManager = class {
"app://obsidian.md" "app://obsidian.md"
], ],
{ {
cwd: this.workingDirectory, cwd: this.projectDirectory,
env: { ...process.env }, env: { ...process.env },
stdio: ["ignore", "pipe", "pipe"], stdio: ["ignore", "pipe", "pipe"],
detached: false detached: false
@@ -594,11 +594,9 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin {
console.log("Loading OpenCode plugin"); console.log("Loading OpenCode plugin");
registerOpenCodeIcons(); registerOpenCodeIcons();
await this.loadSettings(); await this.loadSettings();
const vaultPath = this.getVaultPath();
const projectDirectory = this.getProjectDirectory(); const projectDirectory = this.getProjectDirectory();
this.processManager = new ProcessManager( this.processManager = new ProcessManager(
this.settings, this.settings,
vaultPath,
projectDirectory, projectDirectory,
(state) => this.notifyStateChange(state) (state) => this.notifyStateChange(state)
); );
@@ -728,8 +726,7 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin {
return (_a = this.processManager.getLastError()) != null ? _a : null; return (_a = this.processManager.getLastError()) != null ? _a : null;
} }
getServerUrl() { getServerUrl() {
var _a; return this.processManager.getUrl();
return (_a = this.processManager.getUrl()) != null ? _a : `http://127.0.0.1:${this.settings.port}`;
} }
onProcessStateChange(callback) { onProcessStateChange(callback) {
this.stateChangeCallbacks.push(callback); this.stateChangeCallbacks.push(callback);
@@ -745,18 +742,17 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin {
callback(state); callback(state);
} }
} }
getVaultPath() { getProjectDirectory() {
if (this.settings.projectDirectory) {
console.log("[OpenCode] Using project directory from settings:", this.settings.projectDirectory);
return this.settings.projectDirectory;
}
const adapter = this.app.vault.adapter; const adapter = this.app.vault.adapter;
const vaultPath = adapter.basePath || ""; const vaultPath = adapter.basePath || "";
if (!vaultPath) { if (!vaultPath) {
console.warn("[OpenCode] Warning: Could not determine vault path"); console.warn("[OpenCode] Warning: Could not determine vault path");
} }
console.log("[OpenCode] Using vault path as project directory:", vaultPath);
return vaultPath; return vaultPath;
} }
getProjectDirectory() {
if (this.settings.projectDirectory) {
return this.settings.projectDirectory;
}
return this.getVaultPath();
}
}; };

View File

@@ -152,6 +152,8 @@ export class OpenCodeView extends ItemView {
cls: "opencode-iframe-container", cls: "opencode-iframe-container",
}); });
console.log("[OpenCode] Loading iframe with URL:", this.plugin.getServerUrl());
this.iframeEl = iframeContainer.createEl("iframe", { this.iframeEl = iframeContainer.createEl("iframe", {
cls: "opencode-iframe", cls: "opencode-iframe",
attr: { attr: {

View File

@@ -9,18 +9,15 @@ export class ProcessManager {
private lastError: string | null = null; private lastError: string | null = null;
private earlyExitCode: number | null = null; private earlyExitCode: number | null = null;
private settings: OpenCodeSettings; private settings: OpenCodeSettings;
private workingDirectory: string;
private projectDirectory: string; private projectDirectory: string;
private onStateChange: (state: ProcessState) => void; private onStateChange: (state: ProcessState) => void;
constructor( constructor(
settings: OpenCodeSettings, settings: OpenCodeSettings,
workingDirectory: string,
projectDirectory: string, projectDirectory: string,
onStateChange: (state: ProcessState) => void onStateChange: (state: ProcessState) => void
) { ) {
this.settings = settings; this.settings = settings;
this.workingDirectory = workingDirectory;
this.projectDirectory = projectDirectory; this.projectDirectory = projectDirectory;
this.onStateChange = onStateChange; this.onStateChange = onStateChange;
} }
@@ -71,7 +68,7 @@ export class ProcessManager {
opencodePath: this.settings.opencodePath, opencodePath: this.settings.opencodePath,
port: this.settings.port, port: this.settings.port,
hostname: this.settings.hostname, hostname: this.settings.hostname,
cwd: this.workingDirectory, cwd: this.projectDirectory,
projectDirectory: this.projectDirectory, projectDirectory: this.projectDirectory,
}); });
@@ -87,7 +84,7 @@ export class ProcessManager {
"app://obsidian.md", "app://obsidian.md",
], ],
{ {
cwd: this.workingDirectory, cwd: this.projectDirectory,
env: { ...process.env }, env: { ...process.env },
stdio: ["ignore", "pipe", "pipe"], stdio: ["ignore", "pipe", "pipe"],
detached: false, detached: false,

View File

@@ -17,12 +17,10 @@ export default class OpenCodePlugin extends Plugin {
await this.loadSettings(); await this.loadSettings();
const vaultPath = this.getVaultPath();
const projectDirectory = this.getProjectDirectory(); const projectDirectory = this.getProjectDirectory();
this.processManager = new ProcessManager( this.processManager = new ProcessManager(
this.settings, this.settings,
vaultPath,
projectDirectory, projectDirectory,
(state) => this.notifyStateChange(state) (state) => this.notifyStateChange(state)
); );
@@ -182,7 +180,7 @@ export default class OpenCodePlugin extends Plugin {
} }
getServerUrl(): string { getServerUrl(): string {
return this.processManager.getUrl() ?? `http://127.0.0.1:${this.settings.port}`; return this.processManager.getUrl();
} }
onProcessStateChange(callback: (state: ProcessState) => void): () => void { onProcessStateChange(callback: (state: ProcessState) => void): () => void {
@@ -201,19 +199,17 @@ export default class OpenCodePlugin extends Plugin {
} }
} }
private getVaultPath(): string { getProjectDirectory(): string {
if (this.settings.projectDirectory) {
console.log("[OpenCode] Using project directory from settings:", this.settings.projectDirectory);
return this.settings.projectDirectory;
}
const adapter = this.app.vault.adapter as any; const adapter = this.app.vault.adapter as any;
const vaultPath = adapter.basePath || ""; const vaultPath = adapter.basePath || "";
if (!vaultPath) { if (!vaultPath) {
console.warn("[OpenCode] Warning: Could not determine vault path"); console.warn("[OpenCode] Warning: Could not determine vault path");
} }
console.log("[OpenCode] Using vault path as project directory:", vaultPath);
return vaultPath; return vaultPath;
} }
getProjectDirectory(): string {
if (this.settings.projectDirectory) {
return this.settings.projectDirectory;
}
return this.getVaultPath();
}
} }