-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmock-obsidian.ts
More file actions
163 lines (137 loc) · 4.1 KB
/
mock-obsidian.ts
File metadata and controls
163 lines (137 loc) · 4.1 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
import {
readFileSync,
writeFileSync,
existsSync,
mkdirSync,
promises as fs,
} from "fs";
import * as path from "path";
// Mock Obsidian's Vault class
export class Vault {
configDir: string;
private rootPath: string;
constructor(rootPath: string) {
this.rootPath = rootPath;
this.configDir = ".obsidian";
// Ensure vault directory exists
if (!existsSync(this.rootPath)) {
mkdirSync(this.rootPath, { recursive: true });
}
// Ensure config directory exists
if (!existsSync(path.join(this.rootPath, this.configDir))) {
mkdirSync(path.join(this.rootPath, this.configDir), { recursive: true });
}
}
getRoot() {
return { path: this.rootPath };
}
get adapter() {
return {
read: async (filePath: string) => {
const fullPath = path.join(this.rootPath, filePath);
return readFileSync(fullPath, "utf8");
},
write: async (filePath: string, data: string) => {
const fullPath = path.join(this.rootPath, filePath);
const dir = path.dirname(fullPath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(fullPath, data);
},
readBinary: async (filePath: string) => {
const fullPath = path.join(this.rootPath, filePath);
return readFileSync(fullPath);
},
writeBinary: async (filePath: string, data: ArrayBuffer) => {
const fullPath = path.join(this.rootPath, filePath);
const dir = path.dirname(fullPath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(fullPath, Buffer.from(data));
},
exists: async (filePath: string) => {
const fullPath = path.join(this.rootPath, filePath);
return existsSync(fullPath);
},
mkdir: async (dirPath: string) => {
const fullPath = path.join(this.rootPath, dirPath);
if (!existsSync(fullPath)) {
mkdirSync(fullPath, { recursive: true });
}
},
remove: async (filePath: string) => {
const fullPath = path.join(this.rootPath, filePath);
if (existsSync(fullPath)) {
await fs.unlink(fullPath);
}
},
list: async (dirPath: string) => {
const fullPath = path.join(this.rootPath, dirPath);
if (!existsSync(fullPath)) {
return { files: [], folders: [] };
}
const entries = await fs.readdir(fullPath, { withFileTypes: true });
const files = entries
.filter((entry) => entry.isFile())
.map((entry) => path.join(dirPath, entry.name));
const folders = entries
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(dirPath, entry.name));
return { files, folders };
},
};
}
}
// Mock Notice
export class Notice {
constructor(message: string, timeout?: number) {
console.log(`NOTICE: ${message}`);
}
hide() {
// Do nothing in mock
}
}
interface RequestUrlParam {
url: string;
method?: string;
headers?: Record<string, string>;
contentType?: string;
body?: string | ArrayBuffer;
throw?: boolean;
}
export async function requestUrl(options: RequestUrlParam) {
const response = await fetch(options.url, {
method: options.method || "GET",
headers: options.headers,
body: options.body,
});
const isJsonResponse = response.headers
.get("content-type")
?.includes("application/json");
// Convert to expected Obsidian response format
if (isJsonResponse) {
return {
status: response.status,
json: await response.json(),
};
} else {
return {
status: response.status,
arrayBuffer: await response.arrayBuffer(),
};
}
}
// Mock utility functions
export function normalizePath(path: string): string {
return path.replace(/\\/g, "/");
}
export function arrayBufferToBase64(buffer: ArrayBuffer): string {
return Buffer.from(buffer).toString("base64");
}
export function base64ToArrayBuffer(base64: string): ArrayBuffer {
return Buffer.from(base64, "base64");
}
// Mock Event reference
export type EventRef = string;