Repository navigation
Expand file tree
/
Copy pathvite.dev-api.ts
More file actions
328 lines (301 loc) · 10.9 KB
/
Copy pathvite.dev-api.ts
File metadata and controls
328 lines (301 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import fs from "node:fs/promises";
import { existsSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { execFile } from "node:child_process";
import type { Connect, Plugin } from "vite";
/**
* Dev-only Vite middleware that mirrors the Tauri Rust commands (src-tauri/
* src/commands/) against the real filesystem. Lets the frontend be opened
* directly in a browser tab (bypassing the native Tauri webview) for
* interaction testing with standard browser automation tooling, while still
* exercising real command behavior. See src/lib/tauri-dev-bridge.ts for the
* client side. Keep each case in sync with its Rust counterpart when adding
* commands.
*/
export function tauriDevApiPlugin(): Plugin {
return {
name: "tauri-dev-api",
configureServer(server) {
server.middlewares.use("/__dev_api/invoke", (req, res) => {
handleRequest(req, res).catch((err) => {
res.statusCode = 500;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ ok: false, error: String(err?.message ?? err) }));
});
});
},
};
}
async function handleRequest(req: Connect.IncomingMessage, res: import("node:http").ServerResponse) {
if (req.method !== "POST") {
res.statusCode = 405;
res.end();
return;
}
const chunks: Buffer[] = [];
for await (const chunk of req) chunks.push(chunk as Buffer);
const { cmd, args } = JSON.parse(Buffer.concat(chunks).toString("utf-8") || "{}");
const result = await handleCommand(cmd, args ?? {});
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ ok: true, result }));
}
// --- dev-only workspace persistence (mirrors tauri-plugin-store's app-data-dir JSON blob) ---
const devStorePath = path.join(process.cwd(), "node_modules", ".cache", "dev-workspace.json");
const devSettingsPath = path.join(process.cwd(), "node_modules", ".cache", "dev-settings.json");
// --- settings (mirrors src-tauri/src/commands/settings.rs) ---
interface AppSettings {
openrouterApiKey: string | null;
defaultModel: string;
}
const DEFAULT_SETTINGS: AppSettings = {
openrouterApiKey: null,
defaultModel: "anthropic/claude-sonnet-4.5",
};
async function readSettings(): Promise<AppSettings> {
try {
const raw = await fs.readFile(devSettingsPath, "utf-8");
return { ...DEFAULT_SETTINGS, ...JSON.parse(raw) };
} catch {
return DEFAULT_SETTINGS;
}
}
async function writeSettings(settings: AppSettings): Promise<void> {
await fs.mkdir(path.dirname(devSettingsPath), { recursive: true });
await fs.writeFile(devSettingsPath, JSON.stringify(settings, null, 2), "utf-8");
}
// --- CRUD (mirrors src-tauri/src/commands/crud.rs) ---
function dedupeName(dir: string, baseStem: string, extension: string | null): string {
const makeName = (n: number) => {
if (extension) return n === 1 ? `${baseStem}.${extension}` : `${baseStem} ${n}.${extension}`;
return n === 1 ? baseStem : `${baseStem} ${n}`;
};
let n = 1;
// eslint-disable-next-line no-constant-condition
while (true) {
const candidate = makeName(n);
if (!existsSync(path.join(dir, candidate))) return candidate;
n += 1;
}
}
function splitStemExt(name: string): [string, string | null] {
const idx = name.lastIndexOf(".");
if (idx > 0) return [name.slice(0, idx), name.slice(idx + 1)];
return [name, null];
}
interface RootFolder {
id: string;
path: string;
displayName: string;
addedAt: number;
}
async function readRoots(): Promise<RootFolder[]> {
try {
const raw = await fs.readFile(devStorePath, "utf-8");
return JSON.parse(raw);
} catch {
return [];
}
}
async function writeRoots(roots: RootFolder[]): Promise<void> {
await fs.mkdir(path.dirname(devStorePath), { recursive: true });
await fs.writeFile(devStorePath, JSON.stringify(roots, null, 2), "utf-8");
}
// --- markdown file listing (mirrors src-tauri/src/commands/files.rs) ---
function buildPreview(content: string): string {
let body = content;
if (body.startsWith("---\n")) {
const end = body.indexOf("\n---", 4);
if (end !== -1) body = body.slice(end + 4);
}
const text = body
.split("\n")
.map((line) => line.trim().replace(/^[#>\-*`\s]+/, ""))
.filter((line) => line.length > 0)
.join(" ");
return text.slice(0, 150);
}
async function collectFolders(dir: string, out: unknown[]): Promise<void> {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const d of dirents) {
if (d.name.startsWith(".") || !d.isDirectory()) continue;
const fullPath = path.join(dir, d.name);
out.push({ name: d.name, path: fullPath, isDir: true });
await collectFolders(fullPath, out);
}
}
interface FolderStats {
folderCount: number;
fileCount: number;
lastModifiedAt: number | null;
}
async function collectStats(dir: string, stats: FolderStats): Promise<void> {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const d of dirents) {
if (d.name.startsWith(".")) continue;
const fullPath = path.join(dir, d.name);
if (d.isDirectory()) {
stats.folderCount += 1;
await collectStats(fullPath, stats);
continue;
}
stats.fileCount += 1;
const stat = await fs.stat(fullPath);
if (stats.lastModifiedAt === null || stat.mtimeMs > stats.lastModifiedAt) {
stats.lastModifiedAt = stat.mtimeMs;
}
}
}
async function collectMarkdownFiles(dir: string, recursive: boolean, out: unknown[]): Promise<void> {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const d of dirents) {
if (d.name.startsWith(".")) continue;
const fullPath = path.join(dir, d.name);
if (d.isDirectory()) {
if (recursive) await collectMarkdownFiles(fullPath, recursive, out);
continue;
}
if (!d.name.toLowerCase().endsWith(".md")) continue;
const stat = await fs.stat(fullPath);
const content = await fs.readFile(fullPath, "utf-8").catch(() => "");
out.push({
name: d.name,
path: fullPath,
modifiedAt: stat.mtimeMs,
size: stat.size,
preview: buildPreview(content),
});
}
}
async function handleCommand(cmd: string, args: Record<string, unknown>): Promise<unknown> {
switch (cmd) {
// Only the Home directory variant is ever requested by the frontend
// (@tauri-apps/api/path's homeDir()), so the BaseDirectory arg is
// ignored rather than fully emulating the resolve_directory command.
case "plugin:path|resolve_directory": {
return os.homedir();
}
// `brain <folder>` CLI launches only exist as real OS processes — no
// dev-browser equivalent, so just report "nothing pending" rather than
// emulating the single-instance plugin here.
case "get_launch_folder": {
return null;
}
case "list_dir_children": {
const dirPath = args.path as string;
const dirents = await fs.readdir(dirPath, { withFileTypes: true });
return dirents
.filter((d) => d.isDirectory() && !d.name.startsWith("."))
.map((d) => ({ name: d.name, path: path.join(dirPath, d.name), isDir: true }))
.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
}
case "list_all_folders": {
const out: unknown[] = [];
await collectFolders(args.path as string, out);
out.sort((a: any, b: any) => (a.path as string).toLowerCase().localeCompare((b.path as string).toLowerCase()));
return out;
}
case "get_folder_stats": {
const stats: FolderStats = { folderCount: 0, fileCount: 0, lastModifiedAt: null };
await collectStats(args.path as string, stats);
return stats;
}
case "read_file": {
return await fs.readFile(args.path as string, "utf-8");
}
case "write_file": {
await fs.writeFile(args.path as string, args.content as string, "utf-8");
return null;
}
case "list_markdown_files": {
const out: unknown[] = [];
await collectMarkdownFiles(args.folderPath as string, Boolean(args.recursive), out);
out.sort((a: any, b: any) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
return out;
}
case "get_workspace_roots": {
return await readRoots();
}
case "add_root_folder": {
const roots = await readRoots();
const targetPath = args.path as string;
const existing = roots.find((r) => r.path === targetPath);
if (existing) return existing;
const root: RootFolder = {
id: Math.random().toString(16).slice(2),
path: targetPath,
displayName: path.basename(targetPath) || targetPath,
addedAt: Date.now(),
};
roots.push(root);
await writeRoots(roots);
return root;
}
case "remove_root_folder": {
const roots = await readRoots();
const next = roots.filter((r) => r.id !== args.id);
await writeRoots(next);
return null;
}
case "create_file": {
const dir = args.parentDir as string;
const raw = args.name as string;
const stem = raw.endsWith(".md") ? raw.slice(0, -3) : raw;
const finalName = dedupeName(dir, stem, "md");
const filePath = path.join(dir, finalName);
await fs.writeFile(filePath, "", "utf-8");
return { name: finalName, path: filePath };
}
case "create_folder": {
const dir = args.parentDir as string;
const finalName = dedupeName(dir, args.name as string, null);
const folderPath = path.join(dir, finalName);
await fs.mkdir(folderPath);
return { name: finalName, path: folderPath };
}
case "rename_entry": {
const src = args.path as string;
const dest = path.join(path.dirname(src), args.newName as string);
await fs.rename(src, dest);
return dest;
}
case "delete_entry": {
// Dev-only: no Trash API from Node, just remove directly (test fixtures only).
await fs.rm(args.path as string, { recursive: true, force: true });
return null;
}
case "move_entry": {
const src = args.srcPath as string;
const dir = args.destDir as string;
const name = path.basename(src);
if (path.dirname(src) === dir) return src;
const [stem, ext] = splitStemExt(name);
const finalName = dedupeName(dir, stem, ext);
const dest = path.join(dir, finalName);
await fs.rename(src, dest);
return dest;
}
case "reveal_in_finder": {
execFile("open", ["-R", args.path as string]);
return null;
}
case "get_settings": {
return await readSettings();
}
case "set_settings": {
await writeSettings(args.settings as AppSettings);
return null;
}
case "duplicate_entry": {
const src = args.path as string;
const dir = path.dirname(src);
const [stem, ext] = splitStemExt(path.basename(src));
const finalName = dedupeName(dir, `${stem} copy`, ext);
const dest = path.join(dir, finalName);
await fs.copyFile(src, dest);
return { name: finalName, path: dest };
}
default:
throw new Error(`Unhandled dev-mocked command: ${cmd}`);
}
}