obsidian-launcher 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-HKIRRZUO.js +1146 -0
- package/dist/chunk-HKIRRZUO.js.map +1 -0
- package/dist/chunk-IYWFXZSX.cjs +1146 -0
- package/dist/chunk-IYWFXZSX.cjs.map +1 -0
- package/dist/cli.cjs +170 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +170 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +11 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +321 -0
- package/dist/index.d.ts +321 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1146 @@
|
|
|
1
|
+
// src/launcher.ts
|
|
2
|
+
import fsAsync4 from "fs/promises";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import zlib from "zlib";
|
|
5
|
+
import path4 from "path";
|
|
6
|
+
import os from "os";
|
|
7
|
+
import crypto from "crypto";
|
|
8
|
+
import fetch2 from "node-fetch";
|
|
9
|
+
import extractZip from "extract-zip";
|
|
10
|
+
import { pipeline } from "stream/promises";
|
|
11
|
+
import { downloadArtifact } from "@electron/get";
|
|
12
|
+
import child_process2 from "child_process";
|
|
13
|
+
import semver2 from "semver";
|
|
14
|
+
import CDP from "chrome-remote-interface";
|
|
15
|
+
|
|
16
|
+
// src/utils.ts
|
|
17
|
+
import fsAsync from "fs/promises";
|
|
18
|
+
import path from "path";
|
|
19
|
+
import { PromisePool } from "@supercharge/promise-pool";
|
|
20
|
+
async function fileExists(path5) {
|
|
21
|
+
try {
|
|
22
|
+
await fsAsync.access(path5);
|
|
23
|
+
return true;
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function withTmpDir(dest, func) {
|
|
29
|
+
dest = path.resolve(dest);
|
|
30
|
+
const tmpDir = await fsAsync.mkdtemp(path.join(path.dirname(dest), `.${path.basename(dest)}.tmp.`));
|
|
31
|
+
try {
|
|
32
|
+
let result = await func(tmpDir) ?? tmpDir;
|
|
33
|
+
if (!path.isAbsolute(result)) {
|
|
34
|
+
result = path.join(tmpDir, result);
|
|
35
|
+
} else if (!path.resolve(result).startsWith(tmpDir)) {
|
|
36
|
+
throw new Error(`Returned path ${result} not under tmpDir`);
|
|
37
|
+
}
|
|
38
|
+
if (await fileExists(dest) && (await fsAsync.stat(dest)).isDirectory()) {
|
|
39
|
+
await fsAsync.rename(dest, tmpDir + ".old");
|
|
40
|
+
}
|
|
41
|
+
await fsAsync.rename(result, dest);
|
|
42
|
+
await fsAsync.rm(tmpDir + ".old", { recursive: true, force: true });
|
|
43
|
+
} finally {
|
|
44
|
+
await fsAsync.rm(tmpDir, { recursive: true, force: true });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function linkOrCp(src, dest) {
|
|
48
|
+
try {
|
|
49
|
+
await fsAsync.link(src, dest);
|
|
50
|
+
} catch {
|
|
51
|
+
await fsAsync.copyFile(src, dest);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async function sleep(ms) {
|
|
55
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
56
|
+
}
|
|
57
|
+
async function withTimeout(promise, timeout) {
|
|
58
|
+
let timer;
|
|
59
|
+
const result = Promise.race([
|
|
60
|
+
promise,
|
|
61
|
+
new Promise((resolve, reject) => timer = setTimeout(() => reject(Error("Promise timed out")), timeout))
|
|
62
|
+
]);
|
|
63
|
+
return result.finally(() => clearTimeout(timer));
|
|
64
|
+
}
|
|
65
|
+
async function pool(size, items, func) {
|
|
66
|
+
const { results } = await PromisePool.for(items).withConcurrency(size).handleError(async (error) => {
|
|
67
|
+
throw error;
|
|
68
|
+
}).useCorrespondingResults().process(func);
|
|
69
|
+
return results;
|
|
70
|
+
}
|
|
71
|
+
async function maybe(promise) {
|
|
72
|
+
return promise.then((r) => ({ success: true, result: r, error: void 0 })).catch((e) => ({ success: false, result: void 0, error: e }));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/apis.ts
|
|
76
|
+
import _ from "lodash";
|
|
77
|
+
import fetch from "node-fetch";
|
|
78
|
+
import fsAsync2 from "fs/promises";
|
|
79
|
+
import { fileURLToPath } from "url";
|
|
80
|
+
function parseLinkHeader(linkHeader) {
|
|
81
|
+
function parseLinkData(linkData) {
|
|
82
|
+
return Object.fromEntries(
|
|
83
|
+
linkData.split(";").flatMap((x) => {
|
|
84
|
+
const partMatch = x.trim().match(/^([^=]+?)\s*=\s*"?([^"]+)"?$/);
|
|
85
|
+
return partMatch ? [[partMatch[1], partMatch[2]]] : [];
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const linkDatas = linkHeader.split(/,\s*(?=<)/).flatMap((link) => {
|
|
90
|
+
const linkMatch = link.trim().match(/^<([^>]*)>(.*)$/);
|
|
91
|
+
if (linkMatch) {
|
|
92
|
+
return [{
|
|
93
|
+
url: linkMatch[1],
|
|
94
|
+
...parseLinkData(linkMatch[2])
|
|
95
|
+
}];
|
|
96
|
+
} else {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
}).filter((l) => l.rel);
|
|
100
|
+
return Object.fromEntries(linkDatas.map((l) => [l.rel, l]));
|
|
101
|
+
}
|
|
102
|
+
function createURL(url, base, params = {}) {
|
|
103
|
+
params = _.pickBy(params, (x) => x !== void 0);
|
|
104
|
+
const urlObj = new URL(url, base);
|
|
105
|
+
const searchParams = new URLSearchParams({ ...Object.fromEntries(urlObj.searchParams), ...params });
|
|
106
|
+
if ([...searchParams].length > 0) {
|
|
107
|
+
urlObj.search = "?" + searchParams;
|
|
108
|
+
}
|
|
109
|
+
return urlObj.toString();
|
|
110
|
+
}
|
|
111
|
+
async function fetchGitHubAPI(url, params = {}) {
|
|
112
|
+
url = createURL(url, "https://api.github.com", params);
|
|
113
|
+
const token = process.env.GITHUB_TOKEN;
|
|
114
|
+
const headers = token ? { Authorization: "Bearer " + token } : {};
|
|
115
|
+
const response = await fetch(url, { headers });
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
throw new Error(`GitHub API error: ${await response.text()}`);
|
|
118
|
+
}
|
|
119
|
+
return await response;
|
|
120
|
+
}
|
|
121
|
+
async function fetchGitHubAPIPaginated(url, params = {}) {
|
|
122
|
+
const results = [];
|
|
123
|
+
let next = createURL(url, "https://api.github.com", { per_page: 100, ...params });
|
|
124
|
+
while (next) {
|
|
125
|
+
const response = await fetchGitHubAPI(next);
|
|
126
|
+
results.push(...await response.json());
|
|
127
|
+
next = parseLinkHeader(response.headers.get("link") ?? "").next?.url;
|
|
128
|
+
}
|
|
129
|
+
return results;
|
|
130
|
+
}
|
|
131
|
+
async function fetchObsidianAPI(url) {
|
|
132
|
+
url = createURL(url, "https://releases.obsidian.md");
|
|
133
|
+
const username = process.env.OBSIDIAN_USERNAME;
|
|
134
|
+
const password = process.env.OBSIDIAN_PASSWORD;
|
|
135
|
+
if (!username || !password) {
|
|
136
|
+
throw Error("OBSIDIAN_USERNAME or OBSIDIAN_PASSWORD environment variables are required to access the Obsidian API for beta versions.");
|
|
137
|
+
}
|
|
138
|
+
const response = await fetch(url, {
|
|
139
|
+
headers: {
|
|
140
|
+
// For some reason you have to set the User-Agent or it won't let you download
|
|
141
|
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
|
|
142
|
+
"Origin": "app://obsidian.md",
|
|
143
|
+
"Authorization": "Basic " + btoa(username + ":" + password)
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return response;
|
|
147
|
+
}
|
|
148
|
+
async function fetchWithFileUrl(url) {
|
|
149
|
+
if (url.startsWith("file:")) {
|
|
150
|
+
return await fsAsync2.readFile(fileURLToPath(url), "utf-8");
|
|
151
|
+
} else {
|
|
152
|
+
const response = await fetch(url);
|
|
153
|
+
if (response.ok) {
|
|
154
|
+
return response.text();
|
|
155
|
+
} else {
|
|
156
|
+
throw Error(`Request failed with ${response.status}: ${response.text()}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/chromeLocalStorage.ts
|
|
162
|
+
import path2 from "path";
|
|
163
|
+
import { ClassicLevel } from "classic-level";
|
|
164
|
+
var ChromeLocalStorage = class {
|
|
165
|
+
/** Pass the path to the user data dir for Chrome/Electron. If it doesn't exist it will be created. */
|
|
166
|
+
constructor(userDataDir) {
|
|
167
|
+
this.userDataDir = userDataDir;
|
|
168
|
+
this.encodeKey = (domain, key) => `_${domain}\0${key}`;
|
|
169
|
+
this.decodeKey = (key) => key.slice(1).split("\0");
|
|
170
|
+
this.encodeValue = (value) => `${value}`;
|
|
171
|
+
this.decodeValue = (value) => value.slice(1);
|
|
172
|
+
this.db = new ClassicLevel(path2.join(userDataDir, "Local Storage/leveldb/"));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get a value from localStorage
|
|
176
|
+
* @param domain Domain the value is under, e.g. "https://example.com" or "app://obsidian.md"
|
|
177
|
+
* @param key Key to retreive
|
|
178
|
+
*/
|
|
179
|
+
async getItem(domain, key) {
|
|
180
|
+
const value = await this.db.get(this.encodeKey(domain, key));
|
|
181
|
+
return value === void 0 ? null : this.decodeValue(value);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Set a value in localStorage
|
|
185
|
+
* @param domain Domain the value is under, e.g. "https://example.com" or "app://obsidian.md"
|
|
186
|
+
* @param key Key to set
|
|
187
|
+
* @param value Value to set
|
|
188
|
+
*/
|
|
189
|
+
async setItem(domain, key, value) {
|
|
190
|
+
await this.db.put(this.encodeKey(domain, key), this.encodeValue(value));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Removes a key from localStorage
|
|
194
|
+
* @param domain Domain the values is under, e.g. "https://example.com" or "app://obsidian.md"
|
|
195
|
+
* @param key key to remove.
|
|
196
|
+
*/
|
|
197
|
+
async removeItem(domain, key) {
|
|
198
|
+
await this.db.del(this.encodeKey(domain, key));
|
|
199
|
+
}
|
|
200
|
+
/** Get all items in localStorage as [domain, key, value] tuples */
|
|
201
|
+
async getAllItems() {
|
|
202
|
+
const result = [];
|
|
203
|
+
for await (const pair of this.db.iterator()) {
|
|
204
|
+
if (pair[0].startsWith("_")) {
|
|
205
|
+
const [domain, key] = this.decodeKey(pair[0]);
|
|
206
|
+
const value = this.decodeValue(pair[1]);
|
|
207
|
+
result.push([domain, key, value]);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Write multiple values to localStorage in batch
|
|
214
|
+
* @param domain Domain the values are under, e.g. "https://example.com" or "app://obsidian.md"
|
|
215
|
+
* @param data key/value mapping to write
|
|
216
|
+
*/
|
|
217
|
+
async setItems(domain, data) {
|
|
218
|
+
await this.db.batch(
|
|
219
|
+
Object.entries(data).map(([key, value]) => ({
|
|
220
|
+
type: "put",
|
|
221
|
+
key: this.encodeKey(domain, key),
|
|
222
|
+
value: this.encodeValue(value)
|
|
223
|
+
}))
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Close the localStorage database.
|
|
228
|
+
*/
|
|
229
|
+
async close() {
|
|
230
|
+
await this.db.close();
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// src/launcherUtils.ts
|
|
235
|
+
import fsAsync3 from "fs/promises";
|
|
236
|
+
import path3 from "path";
|
|
237
|
+
import { promisify } from "util";
|
|
238
|
+
import child_process from "child_process";
|
|
239
|
+
import which from "which";
|
|
240
|
+
import semver from "semver";
|
|
241
|
+
import _2 from "lodash";
|
|
242
|
+
var execFile = promisify(child_process.execFile);
|
|
243
|
+
function normalizeGitHubRepo(repo) {
|
|
244
|
+
return repo.replace(/^(https?:\/\/)?(github.com\/)/, "");
|
|
245
|
+
}
|
|
246
|
+
async function extractObsidianAppImage(appImage, dest) {
|
|
247
|
+
await withTmpDir(dest, async (tmpDir) => {
|
|
248
|
+
await fsAsync3.chmod(appImage, 493);
|
|
249
|
+
await execFile(appImage, ["--appimage-extract"], { cwd: tmpDir });
|
|
250
|
+
return path3.join(tmpDir, "squashfs-root");
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
async function extractObsidianExe(exe, appArch, dest) {
|
|
254
|
+
const path7z = await which("7z", { nothrow: true });
|
|
255
|
+
if (!path7z) {
|
|
256
|
+
throw new Error(
|
|
257
|
+
"Downloading Obsidian for Windows requires 7zip to be installed and available on the PATH. You install it from https://www.7-zip.org and then add the install location to the PATH."
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
exe = path3.resolve(exe);
|
|
261
|
+
const subArchive = path3.join("$PLUGINSDIR", appArch + ".7z");
|
|
262
|
+
dest = path3.resolve(dest);
|
|
263
|
+
await withTmpDir(dest, async (tmpDir) => {
|
|
264
|
+
const extractedInstaller = path3.join(tmpDir, "installer");
|
|
265
|
+
await execFile(path7z, ["x", "-o" + extractedInstaller, exe, subArchive]);
|
|
266
|
+
const extractedObsidian = path3.join(tmpDir, "obsidian");
|
|
267
|
+
await execFile(path7z, ["x", "-o" + extractedObsidian, path3.join(extractedInstaller, subArchive)]);
|
|
268
|
+
return extractedObsidian;
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
async function extractObsidianDmg(dmg, dest) {
|
|
272
|
+
dest = path3.resolve(dest);
|
|
273
|
+
await withTmpDir(dest, async (tmpDir) => {
|
|
274
|
+
const proc = await execFile("hdiutil", ["attach", "-nobrowse", "-readonly", dmg]);
|
|
275
|
+
const volume = proc.stdout.match(/\/Volumes\/.*$/m)[0];
|
|
276
|
+
const obsidianApp = path3.join(volume, "Obsidian.app");
|
|
277
|
+
try {
|
|
278
|
+
await fsAsync3.cp(obsidianApp, tmpDir, { recursive: true, verbatimSymlinks: true });
|
|
279
|
+
} finally {
|
|
280
|
+
await execFile("hdiutil", ["detach", volume]);
|
|
281
|
+
}
|
|
282
|
+
return tmpDir;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
function parseObsidianDesktopRelease(fileRelease, isBeta) {
|
|
286
|
+
return {
|
|
287
|
+
version: fileRelease.latestVersion,
|
|
288
|
+
minInstallerVersion: fileRelease.minimumVersion,
|
|
289
|
+
maxInstallerVersion: "",
|
|
290
|
+
// Will be set later
|
|
291
|
+
isBeta,
|
|
292
|
+
downloads: {
|
|
293
|
+
asar: fileRelease.downloadUrl
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function parseObsidianGithubRelease(gitHubRelease) {
|
|
298
|
+
const version = gitHubRelease.name;
|
|
299
|
+
const assets = gitHubRelease.assets.map((a) => a.browser_download_url);
|
|
300
|
+
const downloads = {
|
|
301
|
+
appImage: assets.find((u) => u.match(`${version}.AppImage$`)),
|
|
302
|
+
appImageArm: assets.find((u) => u.match(`${version}-arm64.AppImage$`)),
|
|
303
|
+
apk: assets.find((u) => u.match(`${version}.apk$`)),
|
|
304
|
+
asar: assets.find((u) => u.match(`${version}.asar.gz$`)),
|
|
305
|
+
dmg: assets.find((u) => u.match(`${version}(-universal)?.dmg$`)),
|
|
306
|
+
exe: assets.find((u) => u.match(`${version}.exe$`))
|
|
307
|
+
};
|
|
308
|
+
return {
|
|
309
|
+
version,
|
|
310
|
+
gitHubRelease: gitHubRelease.html_url,
|
|
311
|
+
downloads: _2.pickBy(downloads, (x) => x !== void 0)
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
function correctObsidianVersionInfo(versionInfo) {
|
|
315
|
+
const corrections = {};
|
|
316
|
+
if (semver.gte(versionInfo.version, "1.5.3") && semver.lt(versionInfo.minInstallerVersion, "1.1.9")) {
|
|
317
|
+
corrections.minInstallerVersion = "1.1.9";
|
|
318
|
+
}
|
|
319
|
+
return corrections;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/launcher.ts
|
|
323
|
+
import _3 from "lodash";
|
|
324
|
+
var ObsidianLauncher = class {
|
|
325
|
+
/**
|
|
326
|
+
* Construct an ObsidianLauncher.
|
|
327
|
+
* @param cacheDir Path to the cache directory. Defaults to "OBSIDIAN_CACHE" env var or ".obsidian-cache".
|
|
328
|
+
* @param versionsUrl Custom `obsidian-versions.json` url. Can be a file URL.
|
|
329
|
+
* @param communityPluginsUrl Custom `community-plugins.json` url. Can be a file URL.
|
|
330
|
+
* @param communityThemes Custom `community-css-themes.json` url. Can be a file URL.
|
|
331
|
+
*/
|
|
332
|
+
constructor(options = {}) {
|
|
333
|
+
this.cacheDir = path4.resolve(options.cacheDir ?? process.env.OBSIDIAN_CACHE ?? "./.obsidian-cache");
|
|
334
|
+
const defaultVersionsUrl = "https://raw.githubusercontent.com/jesse-r-s-hines/wdio-obsidian-service/HEAD/obsidian-versions.json";
|
|
335
|
+
this.versionsUrl = options.versionsUrl ?? defaultVersionsUrl;
|
|
336
|
+
const defaultCommunityPluginsUrl = "https://raw.githubusercontent.com/obsidianmd/obsidian-releases/HEAD/community-plugins.json";
|
|
337
|
+
this.communityPluginsUrl = options.communityPluginsUrl ?? defaultCommunityPluginsUrl;
|
|
338
|
+
const defaultCommunityThemesUrl = "https://raw.githubusercontent.com/obsidianmd/obsidian-releases/HEAD/community-css-themes.json";
|
|
339
|
+
this.communityThemesUrl = options.communityThemesUrl ?? defaultCommunityThemesUrl;
|
|
340
|
+
this.metadataCache = {};
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Returns file content fetched from url as JSON. Caches content to dest and uses that cache if its more recent than
|
|
344
|
+
* cacheDuration ms or if there are network errors.
|
|
345
|
+
*/
|
|
346
|
+
async cachedFetch(url, dest, { cacheDuration = 30 * 60 * 1e3 } = {}) {
|
|
347
|
+
dest = path4.resolve(dest);
|
|
348
|
+
if (!(dest in this.metadataCache)) {
|
|
349
|
+
let fileContent;
|
|
350
|
+
const mtime = await fileExists(dest) ? (await fsAsync4.stat(dest)).mtime : void 0;
|
|
351
|
+
if (mtime && (/* @__PURE__ */ new Date()).getTime() - mtime.getTime() < cacheDuration) {
|
|
352
|
+
fileContent = await fsAsync4.readFile(dest, "utf-8");
|
|
353
|
+
} else {
|
|
354
|
+
const request = await maybe(fetchWithFileUrl(url));
|
|
355
|
+
if (request.success) {
|
|
356
|
+
await fsAsync4.mkdir(path4.dirname(dest), { recursive: true });
|
|
357
|
+
await withTmpDir(dest, async (tmpDir) => {
|
|
358
|
+
await fsAsync4.writeFile(path4.join(tmpDir, "download.json"), request.result);
|
|
359
|
+
return path4.join(tmpDir, "download.json");
|
|
360
|
+
});
|
|
361
|
+
fileContent = request.result;
|
|
362
|
+
} else if (await fileExists(dest)) {
|
|
363
|
+
console.warn(request.error);
|
|
364
|
+
console.warn(`Unable to download ${dest}, using cached file.`);
|
|
365
|
+
fileContent = await fsAsync4.readFile(dest, "utf-8");
|
|
366
|
+
} else {
|
|
367
|
+
throw request.error;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
this.metadataCache[dest] = JSON.parse(fileContent);
|
|
371
|
+
}
|
|
372
|
+
return this.metadataCache[dest];
|
|
373
|
+
}
|
|
374
|
+
/** Get information about all available Obsidian versions. */
|
|
375
|
+
async getVersions() {
|
|
376
|
+
const dest = path4.join(this.cacheDir, "obsidian-versions.json");
|
|
377
|
+
return (await this.cachedFetch(this.versionsUrl, dest)).versions;
|
|
378
|
+
}
|
|
379
|
+
/** Get information about all available community plugins. */
|
|
380
|
+
async getCommunityPlugins() {
|
|
381
|
+
const dest = path4.join(this.cacheDir, "obsidian-community-plugins.json");
|
|
382
|
+
return await this.cachedFetch(this.communityPluginsUrl, dest);
|
|
383
|
+
}
|
|
384
|
+
/** Get information about all available community themes. */
|
|
385
|
+
async getCommunityThemes() {
|
|
386
|
+
const dest = path4.join(this.cacheDir, "obsidian-community-css-themes.json");
|
|
387
|
+
return await this.cachedFetch(this.communityThemesUrl, dest);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Resolves Obsidian version strings to absolute obsidian versions.
|
|
391
|
+
* @param appVersion Obsidian version string or "latest" or "latest-beta"
|
|
392
|
+
* @param installerVersion Obsidian version string or "latest" or "earliest"
|
|
393
|
+
* @returns [appVersion, installerVersion] with any "latest" etc. resolved to specific versions.
|
|
394
|
+
*/
|
|
395
|
+
async resolveVersions(appVersion, installerVersion = "latest") {
|
|
396
|
+
const versions = await this.getVersions();
|
|
397
|
+
if (appVersion == "latest") {
|
|
398
|
+
appVersion = versions.filter((v) => !v.isBeta).at(-1).version;
|
|
399
|
+
} else if (appVersion == "latest-beta") {
|
|
400
|
+
appVersion = versions.at(-1).version;
|
|
401
|
+
} else {
|
|
402
|
+
appVersion = semver2.valid(appVersion) ?? appVersion;
|
|
403
|
+
}
|
|
404
|
+
const appVersionInfo = versions.find((v) => v.version == appVersion);
|
|
405
|
+
if (!appVersionInfo) {
|
|
406
|
+
throw Error(`No Obsidian version ${appVersion} found`);
|
|
407
|
+
}
|
|
408
|
+
if (installerVersion == "latest") {
|
|
409
|
+
installerVersion = appVersionInfo.maxInstallerVersion;
|
|
410
|
+
} else if (installerVersion == "earliest") {
|
|
411
|
+
installerVersion = appVersionInfo.minInstallerVersion;
|
|
412
|
+
} else {
|
|
413
|
+
installerVersion = semver2.valid(installerVersion) ?? installerVersion;
|
|
414
|
+
}
|
|
415
|
+
const installerVersionInfo = versions.find((v) => v.version == installerVersion);
|
|
416
|
+
if (!installerVersionInfo || !installerVersionInfo.chromeVersion) {
|
|
417
|
+
throw Error(`No Obsidian installer for version ${installerVersion} found`);
|
|
418
|
+
}
|
|
419
|
+
if (semver2.lt(installerVersionInfo.version, appVersionInfo.minInstallerVersion)) {
|
|
420
|
+
throw Error(
|
|
421
|
+
`Installer and app versions incompatible: minInstallerVersion of v${appVersionInfo.version} is ${appVersionInfo.minInstallerVersion}, but v${installerVersionInfo.version} specified`
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
return [appVersionInfo.version, installerVersionInfo.version];
|
|
425
|
+
}
|
|
426
|
+
/** Gets details about an Obsidian version */
|
|
427
|
+
async getVersionInfo(version) {
|
|
428
|
+
version = (await this.resolveVersions(version))[0];
|
|
429
|
+
const result = (await this.getVersions()).find((v) => v.version == version);
|
|
430
|
+
if (!result) {
|
|
431
|
+
throw Error(`No Obsidian version ${version} found`);
|
|
432
|
+
}
|
|
433
|
+
return result;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Downloads the Obsidian installer for the given version and platform. Returns the file path.
|
|
437
|
+
* @param installerVersion Version to download.
|
|
438
|
+
*/
|
|
439
|
+
async downloadInstaller(installerVersion) {
|
|
440
|
+
const installerVersionInfo = await this.getVersionInfo(installerVersion);
|
|
441
|
+
return await this.downloadInstallerFromVersionInfo(installerVersionInfo);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Helper for downloadInstaller that doesn't require the obsidian-versions.json file so it can be used in
|
|
445
|
+
* updateObsidianVersionInfos
|
|
446
|
+
*/
|
|
447
|
+
async downloadInstallerFromVersionInfo(versionInfo) {
|
|
448
|
+
const installerVersion = versionInfo.version;
|
|
449
|
+
const { platform, arch } = process;
|
|
450
|
+
const cacheDir = path4.join(this.cacheDir, `obsidian-installer/${platform}-${arch}/Obsidian-${installerVersion}`);
|
|
451
|
+
let installerPath;
|
|
452
|
+
let downloader;
|
|
453
|
+
if (platform == "linux") {
|
|
454
|
+
installerPath = path4.join(cacheDir, "obsidian");
|
|
455
|
+
let installerUrl;
|
|
456
|
+
if (arch.startsWith("arm")) {
|
|
457
|
+
installerUrl = versionInfo.downloads.appImageArm;
|
|
458
|
+
} else {
|
|
459
|
+
installerUrl = versionInfo.downloads.appImage;
|
|
460
|
+
}
|
|
461
|
+
if (installerUrl) {
|
|
462
|
+
downloader = async (tmpDir) => {
|
|
463
|
+
const appImage = path4.join(tmpDir, "Obsidian.AppImage");
|
|
464
|
+
await fsAsync4.writeFile(appImage, (await fetch2(installerUrl)).body);
|
|
465
|
+
const obsidianFolder = path4.join(tmpDir, "Obsidian");
|
|
466
|
+
await extractObsidianAppImage(appImage, obsidianFolder);
|
|
467
|
+
return obsidianFolder;
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
} else if (platform == "win32") {
|
|
471
|
+
installerPath = path4.join(cacheDir, "Obsidian.exe");
|
|
472
|
+
const installerUrl = versionInfo.downloads.exe;
|
|
473
|
+
let appArch;
|
|
474
|
+
if (arch == "x64") {
|
|
475
|
+
appArch = "app-64";
|
|
476
|
+
} else if (arch == "ia32") {
|
|
477
|
+
appArch = "app-32";
|
|
478
|
+
} else if (arch.startsWith("arm")) {
|
|
479
|
+
appArch = "app-arm64";
|
|
480
|
+
}
|
|
481
|
+
if (installerUrl && appArch) {
|
|
482
|
+
downloader = async (tmpDir) => {
|
|
483
|
+
const installerExecutable = path4.join(tmpDir, "Obsidian.exe");
|
|
484
|
+
await fsAsync4.writeFile(installerExecutable, (await fetch2(installerUrl)).body);
|
|
485
|
+
const obsidianFolder = path4.join(tmpDir, "Obsidian");
|
|
486
|
+
await extractObsidianExe(installerExecutable, appArch, obsidianFolder);
|
|
487
|
+
return obsidianFolder;
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
} else if (platform == "darwin") {
|
|
491
|
+
installerPath = path4.join(cacheDir, "Contents/MacOS/Obsidian");
|
|
492
|
+
const installerUrl = versionInfo.downloads.dmg;
|
|
493
|
+
if (installerUrl) {
|
|
494
|
+
downloader = async (tmpDir) => {
|
|
495
|
+
const dmg = path4.join(tmpDir, "Obsidian.dmg");
|
|
496
|
+
await fsAsync4.writeFile(dmg, (await fetch2(installerUrl)).body);
|
|
497
|
+
const obsidianFolder = path4.join(tmpDir, "Obsidian");
|
|
498
|
+
await extractObsidianDmg(dmg, obsidianFolder);
|
|
499
|
+
return obsidianFolder;
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
} else {
|
|
503
|
+
throw Error(`Unsupported platform ${platform}`);
|
|
504
|
+
}
|
|
505
|
+
if (!downloader) {
|
|
506
|
+
throw Error(`No Obsidian installer download available for v${installerVersion} ${platform} ${arch}`);
|
|
507
|
+
}
|
|
508
|
+
if (!await fileExists(installerPath)) {
|
|
509
|
+
console.log(`Downloading Obsidian installer v${installerVersion}...`);
|
|
510
|
+
await fsAsync4.mkdir(path4.dirname(cacheDir), { recursive: true });
|
|
511
|
+
await withTmpDir(cacheDir, downloader);
|
|
512
|
+
}
|
|
513
|
+
return installerPath;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Downloads the Obsidian asar for the given version and platform. Returns the file path.
|
|
517
|
+
* @param appVersion Version to download.
|
|
518
|
+
*/
|
|
519
|
+
async downloadApp(appVersion) {
|
|
520
|
+
const appVersionInfo = await this.getVersionInfo(appVersion);
|
|
521
|
+
const appUrl = appVersionInfo.downloads.asar;
|
|
522
|
+
if (!appUrl) {
|
|
523
|
+
throw Error(`No asar found for Obsidian version ${appVersion}`);
|
|
524
|
+
}
|
|
525
|
+
const appPath = path4.join(this.cacheDir, "obsidian-app", `obsidian-${appVersionInfo.version}.asar`);
|
|
526
|
+
if (!await fileExists(appPath)) {
|
|
527
|
+
console.log(`Downloading Obsidian app v${appVersion} ...`);
|
|
528
|
+
await fsAsync4.mkdir(path4.dirname(appPath), { recursive: true });
|
|
529
|
+
await withTmpDir(appPath, async (tmpDir) => {
|
|
530
|
+
const isInsidersBuild = new URL(appUrl).hostname.endsWith(".obsidian.md");
|
|
531
|
+
const response = isInsidersBuild ? await fetchObsidianAPI(appUrl) : await fetch2(appUrl);
|
|
532
|
+
const archive = path4.join(tmpDir, "app.asar.gz");
|
|
533
|
+
const asar = path4.join(tmpDir, "app.asar");
|
|
534
|
+
await fsAsync4.writeFile(archive, response.body);
|
|
535
|
+
await pipeline(fs.createReadStream(archive), zlib.createGunzip(), fs.createWriteStream(asar));
|
|
536
|
+
return asar;
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
return appPath;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Downloads chromedriver for the given Obsidian version.
|
|
543
|
+
*
|
|
544
|
+
* wdio will download chromedriver from the Chrome for Testing API automatically (see
|
|
545
|
+
* https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints). However, Google has only put
|
|
546
|
+
* chromedriver since v115.0.5763.0 in that API, so wdio can't download older versions of chromedriver. As of
|
|
547
|
+
* Obsidian v1.7.7, minInstallerVersion is v0.14.5 which runs on chromium v100.0.4896.75. Here we download
|
|
548
|
+
* chromedriver for older versions ourselves using the @electron/get package which fetches it from
|
|
549
|
+
* https://github.com/electron/electron/releases.
|
|
550
|
+
*/
|
|
551
|
+
async downloadChromedriver(installerVersion) {
|
|
552
|
+
const versionInfo = await this.getVersionInfo(installerVersion);
|
|
553
|
+
const electronVersion = versionInfo.electronVersion;
|
|
554
|
+
if (!electronVersion) {
|
|
555
|
+
throw Error(`${installerVersion} is not an Obsidian installer version.`);
|
|
556
|
+
}
|
|
557
|
+
const chromedriverZipPath = await downloadArtifact({
|
|
558
|
+
version: electronVersion,
|
|
559
|
+
artifactName: "chromedriver",
|
|
560
|
+
cacheRoot: path4.join(this.cacheDir, "chromedriver-legacy"),
|
|
561
|
+
unsafelyDisableChecksums: true
|
|
562
|
+
// the checksums are slow and run even on cache hit.
|
|
563
|
+
});
|
|
564
|
+
let chromedriverPath;
|
|
565
|
+
if (process.platform == "win32") {
|
|
566
|
+
chromedriverPath = path4.join(path4.dirname(chromedriverZipPath), "chromedriver.exe");
|
|
567
|
+
} else {
|
|
568
|
+
chromedriverPath = path4.join(path4.dirname(chromedriverZipPath), "chromedriver");
|
|
569
|
+
}
|
|
570
|
+
if (!await fileExists(chromedriverPath)) {
|
|
571
|
+
console.log(`Downloading legacy chromedriver for electron ${electronVersion} ...`);
|
|
572
|
+
await withTmpDir(chromedriverPath, async (tmpDir) => {
|
|
573
|
+
await extractZip(chromedriverZipPath, { dir: tmpDir });
|
|
574
|
+
return path4.join(tmpDir, path4.basename(chromedriverPath));
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
return chromedriverPath;
|
|
578
|
+
}
|
|
579
|
+
/** Gets the latest version of a plugin. */
|
|
580
|
+
async getLatestPluginVersion(repo) {
|
|
581
|
+
repo = normalizeGitHubRepo(repo);
|
|
582
|
+
const manifestUrl = `https://raw.githubusercontent.com/${repo}/HEAD/manifest.json`;
|
|
583
|
+
const cacheDest = path4.join(this.cacheDir, "obsidian-plugins", repo, "latest.json");
|
|
584
|
+
const manifest = await this.cachedFetch(manifestUrl, cacheDest);
|
|
585
|
+
return manifest.version;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Downloads a plugin from a GitHub repo to the cache.
|
|
589
|
+
* @param repo Repo
|
|
590
|
+
* @param version Version of the plugin to install or "latest"
|
|
591
|
+
* @returns path to the downloaded plugin
|
|
592
|
+
*/
|
|
593
|
+
async downloadGitHubPlugin(repo, version = "latest") {
|
|
594
|
+
repo = normalizeGitHubRepo(repo);
|
|
595
|
+
if (version == "latest") {
|
|
596
|
+
version = await this.getLatestPluginVersion(repo);
|
|
597
|
+
}
|
|
598
|
+
if (!semver2.valid(version)) {
|
|
599
|
+
throw Error(`Invalid version "${version}"`);
|
|
600
|
+
}
|
|
601
|
+
version = semver2.valid(version);
|
|
602
|
+
const pluginDir = path4.join(this.cacheDir, "obsidian-plugins", repo, version);
|
|
603
|
+
if (!await fileExists(pluginDir)) {
|
|
604
|
+
await fsAsync4.mkdir(path4.dirname(pluginDir), { recursive: true });
|
|
605
|
+
await withTmpDir(pluginDir, async (tmpDir) => {
|
|
606
|
+
const assetsToDownload = { "manifest.json": true, "main.js": true, "styles.css": false };
|
|
607
|
+
await Promise.all(
|
|
608
|
+
Object.entries(assetsToDownload).map(async ([file, required]) => {
|
|
609
|
+
const url = `https://github.com/${repo}/releases/download/${version}/${file}`;
|
|
610
|
+
const response = await fetch2(url);
|
|
611
|
+
if (response.ok) {
|
|
612
|
+
await fsAsync4.writeFile(path4.join(tmpDir, file), response.body);
|
|
613
|
+
} else if (required) {
|
|
614
|
+
throw Error(`No ${file} found for ${repo} version ${version}`);
|
|
615
|
+
}
|
|
616
|
+
})
|
|
617
|
+
);
|
|
618
|
+
return tmpDir;
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
return pluginDir;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Downloads a community plugin to the cache.
|
|
625
|
+
* @param id Id of the plugin
|
|
626
|
+
* @param version Version of the plugin to install, or "latest"
|
|
627
|
+
* @returns path to the downloaded plugin
|
|
628
|
+
*/
|
|
629
|
+
async downloadCommunityPlugin(id, version = "latest") {
|
|
630
|
+
const communityPlugins = await this.getCommunityPlugins();
|
|
631
|
+
const pluginInfo = communityPlugins.find((p) => p.id == id);
|
|
632
|
+
if (!pluginInfo) {
|
|
633
|
+
throw Error(`No plugin with id ${id} found.`);
|
|
634
|
+
}
|
|
635
|
+
return await this.downloadGitHubPlugin(pluginInfo.repo, version);
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Downloads a list of plugins to the cache and returns a list of LocalPluginEntry with the downloaded paths.
|
|
639
|
+
* Also adds the `id` property to the plugins based on the manifest.
|
|
640
|
+
*
|
|
641
|
+
* You can download plugins from GitHub using `{repo: "org/repo"}` and community plugins using `{id: 'plugin-id'}`.
|
|
642
|
+
* Local plugins will just be passed through.
|
|
643
|
+
*/
|
|
644
|
+
async downloadPlugins(plugins) {
|
|
645
|
+
return await Promise.all(
|
|
646
|
+
plugins.map(async (plugin) => {
|
|
647
|
+
if (typeof plugin == "object" && "originalType" in plugin) {
|
|
648
|
+
return { ...plugin };
|
|
649
|
+
}
|
|
650
|
+
let pluginPath;
|
|
651
|
+
let originalType;
|
|
652
|
+
if (typeof plugin == "string") {
|
|
653
|
+
pluginPath = plugin;
|
|
654
|
+
originalType = "local";
|
|
655
|
+
} else if ("path" in plugin) {
|
|
656
|
+
;
|
|
657
|
+
pluginPath = plugin.path;
|
|
658
|
+
originalType = "local";
|
|
659
|
+
} else if ("repo" in plugin) {
|
|
660
|
+
pluginPath = await this.downloadGitHubPlugin(plugin.repo, plugin.version);
|
|
661
|
+
originalType = "github";
|
|
662
|
+
} else if ("id" in plugin) {
|
|
663
|
+
pluginPath = await this.downloadCommunityPlugin(plugin.id, plugin.version);
|
|
664
|
+
originalType = "community";
|
|
665
|
+
} else {
|
|
666
|
+
throw Error("You must specify one of plugin path, repo, or id");
|
|
667
|
+
}
|
|
668
|
+
let pluginId = typeof plugin == "object" && "id" in plugin ? plugin.id : void 0;
|
|
669
|
+
if (!pluginId) {
|
|
670
|
+
const manifestPath = path4.join(pluginPath, "manifest.json");
|
|
671
|
+
pluginId = JSON.parse(await fsAsync4.readFile(manifestPath, "utf8").catch(() => "{}")).id;
|
|
672
|
+
if (!pluginId) {
|
|
673
|
+
throw Error(`${pluginPath}/manifest.json missing or malformed.`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
const enabled = typeof plugin == "string" ? true : plugin.enabled;
|
|
677
|
+
return { path: pluginPath, id: pluginId, enabled, originalType };
|
|
678
|
+
})
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
/** Gets the latest version of a theme. */
|
|
682
|
+
async getLatestThemeVersion(repo) {
|
|
683
|
+
repo = normalizeGitHubRepo(repo);
|
|
684
|
+
const manifestUrl = `https://raw.githubusercontent.com/${repo}/HEAD/manifest.json`;
|
|
685
|
+
const cacheDest = path4.join(this.cacheDir, "obsidian-themes", repo, "latest.json");
|
|
686
|
+
const manifest = await this.cachedFetch(manifestUrl, cacheDest);
|
|
687
|
+
return manifest.version;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Downloads a theme from a GitHub repo to the cache.
|
|
691
|
+
* @param repo Repo
|
|
692
|
+
* @returns path to the downloaded theme
|
|
693
|
+
*/
|
|
694
|
+
async downloadGitHubTheme(repo) {
|
|
695
|
+
repo = normalizeGitHubRepo(repo);
|
|
696
|
+
const version = await this.getLatestThemeVersion(repo);
|
|
697
|
+
const themeDir = path4.join(this.cacheDir, "obsidian-themes", repo, version);
|
|
698
|
+
if (!await fileExists(themeDir)) {
|
|
699
|
+
await fsAsync4.mkdir(path4.dirname(themeDir), { recursive: true });
|
|
700
|
+
await withTmpDir(themeDir, async (tmpDir) => {
|
|
701
|
+
const assetsToDownload = ["manifest.json", "theme.css"];
|
|
702
|
+
await Promise.all(
|
|
703
|
+
assetsToDownload.map(async (file) => {
|
|
704
|
+
const url = `https://raw.githubusercontent.com/${repo}/HEAD/${file}`;
|
|
705
|
+
const response = await fetch2(url);
|
|
706
|
+
if (response.ok) {
|
|
707
|
+
await fsAsync4.writeFile(path4.join(tmpDir, file), response.body);
|
|
708
|
+
} else {
|
|
709
|
+
throw Error(`No ${file} found for ${repo}`);
|
|
710
|
+
}
|
|
711
|
+
})
|
|
712
|
+
);
|
|
713
|
+
return tmpDir;
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
return themeDir;
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Downloads a community theme to the cache.
|
|
720
|
+
* @param name name of the theme
|
|
721
|
+
* @returns path to the downloaded theme
|
|
722
|
+
*/
|
|
723
|
+
async downloadCommunityTheme(name) {
|
|
724
|
+
const communityThemes = await this.getCommunityThemes();
|
|
725
|
+
const themeInfo = communityThemes.find((p) => p.name == name);
|
|
726
|
+
if (!themeInfo) {
|
|
727
|
+
throw Error(`No theme with name ${name} found.`);
|
|
728
|
+
}
|
|
729
|
+
return await this.downloadGitHubTheme(themeInfo.repo);
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Downloads a list of themes to the cache and returns a list of LocalThemeEntry with the downloaded paths.
|
|
733
|
+
* Also adds the `name` property to the plugins based on the manifest.
|
|
734
|
+
*
|
|
735
|
+
* You can download themes from GitHub using `{repo: "org/repo"}` and community themes using `{name: 'theme-name'}`.
|
|
736
|
+
* Local themes will just be passed through.
|
|
737
|
+
*/
|
|
738
|
+
async downloadThemes(themes) {
|
|
739
|
+
return await Promise.all(
|
|
740
|
+
themes.map(async (theme) => {
|
|
741
|
+
if (typeof theme == "object" && "originalType" in theme) {
|
|
742
|
+
return { ...theme };
|
|
743
|
+
}
|
|
744
|
+
let themePath;
|
|
745
|
+
let originalType;
|
|
746
|
+
if (typeof theme == "string") {
|
|
747
|
+
themePath = theme;
|
|
748
|
+
originalType = "local";
|
|
749
|
+
} else if ("path" in theme) {
|
|
750
|
+
;
|
|
751
|
+
themePath = theme.path;
|
|
752
|
+
originalType = "local";
|
|
753
|
+
} else if ("repo" in theme) {
|
|
754
|
+
themePath = await this.downloadGitHubTheme(theme.repo);
|
|
755
|
+
originalType = "github";
|
|
756
|
+
} else if ("name" in theme) {
|
|
757
|
+
themePath = await this.downloadCommunityTheme(theme.name);
|
|
758
|
+
originalType = "community";
|
|
759
|
+
} else {
|
|
760
|
+
throw Error("You must specify one of theme path, repo, or name");
|
|
761
|
+
}
|
|
762
|
+
let themeName = typeof theme == "object" && "name" in theme ? theme.name : void 0;
|
|
763
|
+
if (!themeName) {
|
|
764
|
+
const manifestPath = path4.join(themePath, "manifest.json");
|
|
765
|
+
themeName = JSON.parse(await fsAsync4.readFile(manifestPath, "utf8").catch(() => "{}")).name;
|
|
766
|
+
if (!themeName) {
|
|
767
|
+
throw Error(`${themePath}/manifest.json missing or malformed.`);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
const enabled = typeof theme == "string" ? true : theme.enabled;
|
|
771
|
+
return { path: themePath, name: themeName, enabled, originalType };
|
|
772
|
+
})
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Installs plugins into an Obsidian vault.
|
|
777
|
+
* @param vault Path to the vault to install the plugin in.
|
|
778
|
+
* @param plugins List plugins paths to install.
|
|
779
|
+
*/
|
|
780
|
+
async installPlugins(vault, plugins) {
|
|
781
|
+
const downloadedPlugins = await this.downloadPlugins(plugins);
|
|
782
|
+
const obsidianDir = path4.join(vault, ".obsidian");
|
|
783
|
+
await fsAsync4.mkdir(obsidianDir, { recursive: true });
|
|
784
|
+
const enabledPluginsPath = path4.join(obsidianDir, "community-plugins.json");
|
|
785
|
+
let originalEnabledPlugins = [];
|
|
786
|
+
if (await fileExists(enabledPluginsPath)) {
|
|
787
|
+
originalEnabledPlugins = JSON.parse(await fsAsync4.readFile(enabledPluginsPath, "utf-8"));
|
|
788
|
+
}
|
|
789
|
+
let enabledPlugins = [...originalEnabledPlugins];
|
|
790
|
+
for (const { path: pluginPath, enabled = true, originalType } of downloadedPlugins) {
|
|
791
|
+
const manifestPath = path4.join(pluginPath, "manifest.json");
|
|
792
|
+
const pluginId = JSON.parse(await fsAsync4.readFile(manifestPath, "utf8").catch(() => "{}")).id;
|
|
793
|
+
if (!pluginId) {
|
|
794
|
+
throw Error(`${manifestPath} missing or malformed.`);
|
|
795
|
+
}
|
|
796
|
+
const pluginDest = path4.join(obsidianDir, "plugins", pluginId);
|
|
797
|
+
await fsAsync4.rm(pluginDest, { recursive: true, force: true });
|
|
798
|
+
await fsAsync4.mkdir(pluginDest, { recursive: true });
|
|
799
|
+
const files = {
|
|
800
|
+
"manifest.json": true,
|
|
801
|
+
"main.js": true,
|
|
802
|
+
"styles.css": false,
|
|
803
|
+
"data.json": false
|
|
804
|
+
};
|
|
805
|
+
for (const [file, required] of Object.entries(files)) {
|
|
806
|
+
if (await fileExists(path4.join(pluginPath, file))) {
|
|
807
|
+
await linkOrCp(path4.join(pluginPath, file), path4.join(pluginDest, file));
|
|
808
|
+
} else if (required) {
|
|
809
|
+
throw Error(`${pluginPath}/${file} missing.`);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
const pluginAlreadyListed = enabledPlugins.includes(pluginId);
|
|
813
|
+
if (enabled && !pluginAlreadyListed) {
|
|
814
|
+
enabledPlugins.push(pluginId);
|
|
815
|
+
} else if (!enabled && pluginAlreadyListed) {
|
|
816
|
+
enabledPlugins = enabledPlugins.filter((p) => p != pluginId);
|
|
817
|
+
}
|
|
818
|
+
if (originalType == "local") {
|
|
819
|
+
await fsAsync4.writeFile(path4.join(pluginDest, ".hotreload"), "");
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (!_3.isEqual(enabledPlugins, originalEnabledPlugins)) {
|
|
823
|
+
await fsAsync4.writeFile(enabledPluginsPath, JSON.stringify(enabledPlugins, void 0, 2));
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Installs themes into an obsidian vault.
|
|
828
|
+
* @param vault Path to the theme to install the plugin in.
|
|
829
|
+
* @param themes: List of themes to install.
|
|
830
|
+
*/
|
|
831
|
+
async installThemes(vault, themes) {
|
|
832
|
+
const downloadedThemes = await this.downloadThemes(themes);
|
|
833
|
+
const obsidianDir = path4.join(vault, ".obsidian");
|
|
834
|
+
await fsAsync4.mkdir(obsidianDir, { recursive: true });
|
|
835
|
+
let enabledTheme = void 0;
|
|
836
|
+
for (const { path: themePath, enabled = true } of downloadedThemes) {
|
|
837
|
+
const manifestPath = path4.join(themePath, "manifest.json");
|
|
838
|
+
const cssPath = path4.join(themePath, "theme.css");
|
|
839
|
+
const themeName = JSON.parse(await fsAsync4.readFile(manifestPath, "utf8").catch(() => "{}")).name;
|
|
840
|
+
if (!themeName) {
|
|
841
|
+
throw Error(`${manifestPath} missing or malformed.`);
|
|
842
|
+
}
|
|
843
|
+
if (!await fileExists(cssPath)) {
|
|
844
|
+
throw Error(`${cssPath} missing.`);
|
|
845
|
+
}
|
|
846
|
+
const themeDest = path4.join(obsidianDir, "themes", themeName);
|
|
847
|
+
await fsAsync4.rm(themeDest, { recursive: true, force: true });
|
|
848
|
+
await fsAsync4.mkdir(themeDest, { recursive: true });
|
|
849
|
+
await linkOrCp(manifestPath, path4.join(themeDest, "manifest.json"));
|
|
850
|
+
await linkOrCp(cssPath, path4.join(themeDest, "theme.css"));
|
|
851
|
+
if (enabledTheme && enabled) {
|
|
852
|
+
throw Error("You can only have one enabled theme.");
|
|
853
|
+
} else if (enabled) {
|
|
854
|
+
enabledTheme = themeName;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (themes.length > 0) {
|
|
858
|
+
const appearancePath = path4.join(obsidianDir, "appearance.json");
|
|
859
|
+
let appearance = {};
|
|
860
|
+
if (await fileExists(appearancePath)) {
|
|
861
|
+
appearance = JSON.parse(await fsAsync4.readFile(appearancePath, "utf-8"));
|
|
862
|
+
}
|
|
863
|
+
appearance.cssTheme = enabledTheme ?? "";
|
|
864
|
+
await fsAsync4.writeFile(appearancePath, JSON.stringify(appearance, void 0, 2));
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Sets up the config dir to use for the --user-data-dir in obsidian. Returns the path to the created config dir.
|
|
869
|
+
*
|
|
870
|
+
* @param appVersion Obsidian version string.
|
|
871
|
+
* @param installerVersion Obsidian version string.
|
|
872
|
+
* @param appPath Path to the asar file to install.
|
|
873
|
+
* @param vault Path to the vault to open in Obsidian.
|
|
874
|
+
* @param dest Destination path for the config dir. If omitted it will create it under `/tmp`.
|
|
875
|
+
*/
|
|
876
|
+
async setupConfigDir(params) {
|
|
877
|
+
const [appVersion, installerVersion] = await this.resolveVersions(params.appVersion, params.installerVersion);
|
|
878
|
+
const configDir = params.dest ?? await fsAsync4.mkdtemp(path4.join(os.tmpdir(), "obs-launcher-config-"));
|
|
879
|
+
let obsidianJson = {
|
|
880
|
+
updateDisabled: true
|
|
881
|
+
// Prevents Obsidian trying to auto-update on boot.
|
|
882
|
+
};
|
|
883
|
+
let localStorageData = {
|
|
884
|
+
"most-recently-installed-version": appVersion
|
|
885
|
+
// prevents the changelog page on boot
|
|
886
|
+
};
|
|
887
|
+
if (params.vault !== void 0) {
|
|
888
|
+
const vaultId = crypto.randomBytes(8).toString("hex");
|
|
889
|
+
obsidianJson = {
|
|
890
|
+
...obsidianJson,
|
|
891
|
+
vaults: {
|
|
892
|
+
[vaultId]: {
|
|
893
|
+
path: path4.resolve(params.vault),
|
|
894
|
+
ts: (/* @__PURE__ */ new Date()).getTime(),
|
|
895
|
+
open: true
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
localStorageData = {
|
|
900
|
+
...localStorageData,
|
|
901
|
+
[`enable-plugin-${vaultId}`]: "true"
|
|
902
|
+
// Disable "safe mode" and enable plugins
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
await fsAsync4.writeFile(path4.join(configDir, "obsidian.json"), JSON.stringify(obsidianJson));
|
|
906
|
+
if (params.appPath) {
|
|
907
|
+
await linkOrCp(params.appPath, path4.join(configDir, path4.basename(params.appPath)));
|
|
908
|
+
} else if (appVersion != installerVersion) {
|
|
909
|
+
throw Error("You must specify app path if appVersion != installerVersion");
|
|
910
|
+
}
|
|
911
|
+
const localStorage = new ChromeLocalStorage(configDir);
|
|
912
|
+
await localStorage.setItems("app://obsidian.md", localStorageData);
|
|
913
|
+
await localStorage.close();
|
|
914
|
+
return configDir;
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Sets up a vault for Obsidian, installing plugins and themes and optionally copying the vault to a temporary
|
|
918
|
+
* directory first.
|
|
919
|
+
* @param vault Path to the vault to open in Obsidian.
|
|
920
|
+
* @param copy Whether to copy the vault to a tmpdir first. Default false.
|
|
921
|
+
* @param plugins List of plugins to install in the vault.
|
|
922
|
+
* @param themes List of themes to install in the vault.
|
|
923
|
+
* @returns Path to the copied vault (or just the path to the vault if copy is false)
|
|
924
|
+
*/
|
|
925
|
+
async setupVault(params) {
|
|
926
|
+
let vault = params.vault;
|
|
927
|
+
if (params.copy) {
|
|
928
|
+
const dest = await fsAsync4.mkdtemp(path4.join(os.tmpdir(), "obs-launcher-vault-"));
|
|
929
|
+
await fsAsync4.cp(vault, dest, { recursive: true });
|
|
930
|
+
vault = dest;
|
|
931
|
+
}
|
|
932
|
+
await this.installPlugins(vault, params.plugins ?? []);
|
|
933
|
+
await this.installThemes(vault, params.themes ?? []);
|
|
934
|
+
return vault;
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Downloads and launches Obsidian with a sandboxed config dir and a specifc vault open. Optionally install plugins
|
|
938
|
+
* and themes first.
|
|
939
|
+
*
|
|
940
|
+
* This is just a shortcut for calling downloadApp, downloadInstaller, setupVault and setupConfDir.
|
|
941
|
+
*
|
|
942
|
+
* @param appVersion Obsidian version string.
|
|
943
|
+
* @param installerVersion Obsidian version string.
|
|
944
|
+
* @param vault Path to the vault to open in Obsidian.
|
|
945
|
+
* @param copy Whether to copy the vault to a tmpdir first. Default false.
|
|
946
|
+
* @param plugins List of plugins to install in the vault.
|
|
947
|
+
* @param themes List of themes to install in the vault.
|
|
948
|
+
* @param args CLI args to pass to Obsidian
|
|
949
|
+
* @param spawnOptions Options to pass to `spawn`.
|
|
950
|
+
* @returns The launched child process and the created tmpdirs.
|
|
951
|
+
*/
|
|
952
|
+
async launch(params) {
|
|
953
|
+
const [appVersion, installerVersion] = await this.resolveVersions(params.appVersion, params.installerVersion);
|
|
954
|
+
const appPath = await this.downloadApp(appVersion);
|
|
955
|
+
const installerPath = await this.downloadInstaller(installerVersion);
|
|
956
|
+
let vault = params.vault;
|
|
957
|
+
if (vault) {
|
|
958
|
+
vault = await this.setupVault({
|
|
959
|
+
vault,
|
|
960
|
+
copy: params.copy ?? false,
|
|
961
|
+
plugins: params.plugins,
|
|
962
|
+
themes: params.themes
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
const configDir = await this.setupConfigDir({ appVersion, installerVersion, appPath, vault });
|
|
966
|
+
const proc = child_process2.spawn(installerPath, [
|
|
967
|
+
`--user-data-dir=${configDir}`,
|
|
968
|
+
...params.args ?? []
|
|
969
|
+
], {
|
|
970
|
+
...params.spawnOptions
|
|
971
|
+
});
|
|
972
|
+
return { proc, configDir, vault };
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Extract electron and chrome versions for an Obsidian version.
|
|
976
|
+
*/
|
|
977
|
+
async getDependencyVersions(versionInfo) {
|
|
978
|
+
const binary = await this.downloadInstallerFromVersionInfo(versionInfo);
|
|
979
|
+
console.log(`${versionInfo.version}: Extracting electron & chrome versions...`);
|
|
980
|
+
const configDir = await fsAsync4.mkdtemp(path4.join(os.tmpdir(), `fetch-obsidian-versions-`));
|
|
981
|
+
const proc = child_process2.spawn(binary, [
|
|
982
|
+
`--remote-debugging-port=0`,
|
|
983
|
+
// 0 will make it choose a random available port
|
|
984
|
+
"--test-type=webdriver",
|
|
985
|
+
`--user-data-dir=${configDir}`,
|
|
986
|
+
"--no-sandbox",
|
|
987
|
+
// Workaround for SUID issue, see https://github.com/electron/electron/issues/42510
|
|
988
|
+
"--headless"
|
|
989
|
+
]);
|
|
990
|
+
const procExit = new Promise((resolve) => proc.on("exit", (code) => resolve(code ?? -1)));
|
|
991
|
+
let dependencyVersions;
|
|
992
|
+
try {
|
|
993
|
+
const portPromise = new Promise((resolve, reject) => {
|
|
994
|
+
procExit.then(() => reject("Processed ended without opening a port"));
|
|
995
|
+
proc.stderr.on("data", (data) => {
|
|
996
|
+
const port2 = data.toString().match(/ws:\/\/[\w.]+?:(\d+)/)?.[1];
|
|
997
|
+
if (port2) {
|
|
998
|
+
resolve(Number(port2));
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
const port = await maybe(withTimeout(portPromise, 10 * 1e3));
|
|
1003
|
+
if (!port.success) {
|
|
1004
|
+
throw new Error("Timed out waiting for Chrome DevTools protocol port");
|
|
1005
|
+
}
|
|
1006
|
+
const client = await CDP({ port: port.result });
|
|
1007
|
+
const response = await client.Runtime.evaluate({ expression: "JSON.stringify(process.versions)" });
|
|
1008
|
+
dependencyVersions = JSON.parse(response.result.value);
|
|
1009
|
+
await client.close();
|
|
1010
|
+
} finally {
|
|
1011
|
+
proc.kill("SIGTERM");
|
|
1012
|
+
const timeout = await maybe(withTimeout(procExit, 4 * 1e3));
|
|
1013
|
+
if (!timeout.success) {
|
|
1014
|
+
console.log(`${versionInfo.version}: Stuck process ${proc.pid}, using SIGKILL`);
|
|
1015
|
+
proc.kill("SIGKILL");
|
|
1016
|
+
}
|
|
1017
|
+
await procExit;
|
|
1018
|
+
await sleep(1e3);
|
|
1019
|
+
await fsAsync4.rm(configDir, { recursive: true, force: true });
|
|
1020
|
+
}
|
|
1021
|
+
if (!dependencyVersions?.electron || !dependencyVersions?.chrome) {
|
|
1022
|
+
throw Error(`Failed to extract electron and chrome versions for ${versionInfo.version}`);
|
|
1023
|
+
}
|
|
1024
|
+
return {
|
|
1025
|
+
...versionInfo,
|
|
1026
|
+
electronVersion: dependencyVersions.electron,
|
|
1027
|
+
chromeVersion: dependencyVersions.chrome,
|
|
1028
|
+
nodeVersion: dependencyVersions.node
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Updates the info obsidian-versions.json. The obsidian-versions.json file is used in other launcher commands
|
|
1033
|
+
* and in wdio-obsidian-service to get metadata about Obsidian versions in one place such as minInstallerVersion and
|
|
1034
|
+
* the internal electron version.
|
|
1035
|
+
*/
|
|
1036
|
+
async updateObsidianVersionInfos(original, { maxInstances = 1 } = {}) {
|
|
1037
|
+
const repo = "obsidianmd/obsidian-releases";
|
|
1038
|
+
let commitHistory = await fetchGitHubAPIPaginated(`repos/${repo}/commits`, {
|
|
1039
|
+
path: "desktop-releases.json",
|
|
1040
|
+
since: original?.metadata.commit_date
|
|
1041
|
+
});
|
|
1042
|
+
commitHistory.reverse();
|
|
1043
|
+
if (original) {
|
|
1044
|
+
commitHistory = _3.takeRightWhile(commitHistory, (c) => c.sha != original.metadata.commit_sha);
|
|
1045
|
+
}
|
|
1046
|
+
const fileHistory = await pool(
|
|
1047
|
+
8,
|
|
1048
|
+
commitHistory,
|
|
1049
|
+
(commit) => fetch2(`https://raw.githubusercontent.com/${repo}/${commit.sha}/desktop-releases.json`).then((r) => r.json())
|
|
1050
|
+
);
|
|
1051
|
+
const githubReleases = await fetchGitHubAPIPaginated(`repos/${repo}/releases`);
|
|
1052
|
+
const versionMap = _3.keyBy(
|
|
1053
|
+
original?.versions ?? [],
|
|
1054
|
+
(v) => v.version
|
|
1055
|
+
);
|
|
1056
|
+
for (const { beta, ...current } of fileHistory) {
|
|
1057
|
+
if (beta && (!versionMap[beta.latestVersion] || versionMap[beta.latestVersion].isBeta)) {
|
|
1058
|
+
versionMap[beta.latestVersion] = _3.merge(
|
|
1059
|
+
{},
|
|
1060
|
+
versionMap[beta.latestVersion],
|
|
1061
|
+
parseObsidianDesktopRelease(beta, true)
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
versionMap[current.latestVersion] = _3.merge(
|
|
1065
|
+
{},
|
|
1066
|
+
versionMap[current.latestVersion],
|
|
1067
|
+
parseObsidianDesktopRelease(current, false)
|
|
1068
|
+
);
|
|
1069
|
+
}
|
|
1070
|
+
for (const release of githubReleases) {
|
|
1071
|
+
if (versionMap.hasOwnProperty(release.name)) {
|
|
1072
|
+
versionMap[release.name] = _3.merge({}, versionMap[release.name], parseObsidianGithubRelease(release));
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
const dependencyVersions = await pool(
|
|
1076
|
+
maxInstances,
|
|
1077
|
+
Object.values(versionMap).filter((v) => v.downloads?.appImage && !v.chromeVersion),
|
|
1078
|
+
(v) => this.getDependencyVersions(v)
|
|
1079
|
+
);
|
|
1080
|
+
for (const deps of dependencyVersions) {
|
|
1081
|
+
versionMap[deps.version] = _3.merge({}, versionMap[deps.version], deps);
|
|
1082
|
+
}
|
|
1083
|
+
let maxInstallerVersion = "0.0.0";
|
|
1084
|
+
for (const version of Object.keys(versionMap).sort(semver2.compare)) {
|
|
1085
|
+
if (versionMap[version].downloads.appImage) {
|
|
1086
|
+
maxInstallerVersion = version;
|
|
1087
|
+
}
|
|
1088
|
+
versionMap[version] = _3.merge(
|
|
1089
|
+
{},
|
|
1090
|
+
versionMap[version],
|
|
1091
|
+
correctObsidianVersionInfo(versionMap[version]),
|
|
1092
|
+
{ maxInstallerVersion }
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
const versionInfos = Object.values(versionMap);
|
|
1096
|
+
versionInfos.sort((a, b) => semver2.compare(a.version, b.version));
|
|
1097
|
+
const result = {
|
|
1098
|
+
metadata: {
|
|
1099
|
+
commit_date: commitHistory.at(-1)?.commit.committer.date ?? original?.metadata.commit_date,
|
|
1100
|
+
commit_sha: commitHistory.at(-1)?.sha ?? original?.metadata.commit_sha,
|
|
1101
|
+
timestamp: original?.metadata.timestamp ?? ""
|
|
1102
|
+
// set down below
|
|
1103
|
+
},
|
|
1104
|
+
versions: versionInfos
|
|
1105
|
+
};
|
|
1106
|
+
const dayMs = 24 * 60 * 60 * 1e3;
|
|
1107
|
+
const timeSinceLastUpdate = (/* @__PURE__ */ new Date()).getTime() - new Date(original?.metadata.timestamp ?? 0).getTime();
|
|
1108
|
+
if (!_3.isEqual(original, result) || timeSinceLastUpdate > 29 * dayMs) {
|
|
1109
|
+
result.metadata.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
1110
|
+
}
|
|
1111
|
+
return result;
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Returns true if the Obsidian version is already in the cache.
|
|
1115
|
+
*/
|
|
1116
|
+
async isInCache(type, version) {
|
|
1117
|
+
version = (await this.resolveVersions(version))[0];
|
|
1118
|
+
let dest;
|
|
1119
|
+
if (type == "app") {
|
|
1120
|
+
dest = `obsidian-app/obsidian-${version}.asar`;
|
|
1121
|
+
} else {
|
|
1122
|
+
const { platform, arch } = process;
|
|
1123
|
+
dest = `obsidian-installer/${platform}-${arch}/Obsidian-${version}`;
|
|
1124
|
+
}
|
|
1125
|
+
return await fileExists(path4.join(this.cacheDir, dest));
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Returns true if we either have the credentails to download the version or it's already in cache.
|
|
1129
|
+
* This is only relevant for Obsidian beta versions, as they require Obsidian insider credentials to download.
|
|
1130
|
+
*/
|
|
1131
|
+
async isAvailable(version) {
|
|
1132
|
+
const versionInfo = await this.getVersionInfo(version);
|
|
1133
|
+
if (versionInfo.isBeta) {
|
|
1134
|
+
const hasCreds = !!(process.env["OBSIDIAN_USERNAME"] && process.env["OBSIDIAN_PASSWORD"]);
|
|
1135
|
+
const inCache = await this.isInCache("app", versionInfo.version);
|
|
1136
|
+
return hasCreds || inCache;
|
|
1137
|
+
} else {
|
|
1138
|
+
return !!versionInfo.downloads.asar;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
};
|
|
1142
|
+
|
|
1143
|
+
export {
|
|
1144
|
+
ObsidianLauncher
|
|
1145
|
+
};
|
|
1146
|
+
//# sourceMappingURL=chunk-HKIRRZUO.js.map
|