initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { runCommand } from "../utils/process";
|
||||
import { RmapiNode } from "../types";
|
||||
import RemarkablePlugin from "../main";
|
||||
|
||||
export class RmapiBridge {
|
||||
plugin: RemarkablePlugin;
|
||||
|
||||
constructor(plugin: RemarkablePlugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private getEnv(): Record<string, string> {
|
||||
return {
|
||||
RMAPI_HOST: this.plugin.settings.remarkableHost,
|
||||
RMAPI_CONFIG: this.plugin.app.vault.adapter.getBasePath() + "/.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<RmapiNode[]> {
|
||||
// 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<void> {
|
||||
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<void> {
|
||||
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<boolean> {
|
||||
const { code, stderr } = await this.runRmapi(["ls", "--json", "/"]);
|
||||
// rmapi returns auth errors on stderr
|
||||
return code === 0 && !stderr.includes("auth") && !stderr.includes("register");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user