import { runCommand } from "../utils/process"; import { RmapiNode } from "../types"; import RemarkablePlugin from "../main"; import { join } from "path"; import { getVaultBasePath } from "../utils/paths"; export class RmapiBridge { plugin: RemarkablePlugin; constructor(plugin: RemarkablePlugin) { this.plugin = plugin; } private getEnv(): Record { return { RMAPI_HOST: this.plugin.settings.remarkableHost, RMAPI_CONFIG: join(getVaultBasePath(this.plugin), ".obsidian", "rmapi"), }; } async runRmapi(args: string[]): Promise<{ stdout: string; stderr: string; code: number }> { return runCommand(this.plugin.settings.rmapiBinaryPath, args, this.getEnv()); } async list(path: string = "/"): Promise { // flags must come before positional args for Go's flag parser const { stdout, stderr, code } = await this.runRmapi(["ls", "--json", path]); if (code !== 0) { throw new Error(`rmapi ls failed: ${stderr || stdout}`); } try { return JSON.parse(stdout) as RmapiNode[]; } catch (e) { throw new Error(`Failed to parse rmapi output: ${e}`); } } async downloadFile(remotePath: string, localPath: string): Promise { const { stdout, stderr, code } = await this.runRmapi(["get", remotePath, "-o", localPath]); if (code !== 0) { throw new Error(`rmapi get failed: ${stderr || stdout}`); } } async downloadAnnotatedPdf(remotePath: string, localPath: string): Promise { const { stdout, stderr, code } = await this.runRmapi(["geta", remotePath, "-o", localPath]); if (code !== 0) { throw new Error(`rmapi geta failed: ${stderr || stdout}`); } } async isAuthenticated(): Promise { const { code, stderr } = await this.runRmapi(["ls", "--json", "/"]); // rmapi returns auth errors on stderr return code === 0 && !stderr.includes("auth") && !stderr.includes("register"); } }