rethink vault opening
This commit is contained in:
parent
676cbe4843
commit
76e14fe125
BIN
.yarn/install-state.gz
vendored
BIN
.yarn/install-state.gz
vendored
Binary file not shown.
@ -3,16 +3,17 @@ import { _electron as electron } from "playwright";
|
||||
import { Fixtures } from "@playwright/test";
|
||||
import path from "path";
|
||||
import { ObsidianTestFixtures } from "./fixtures.js";
|
||||
import { existsSync } from "fs";
|
||||
import { existsSync, readFileSync, writeFileSync } from "fs";
|
||||
import { getApp, waitForIndexingComplete } from "./util.js";
|
||||
import { execSync } from "child_process";
|
||||
import { Page } from "playwright";
|
||||
import { randomBytes } from "crypto";
|
||||
|
||||
export interface ObsidianTestingConfig {
|
||||
vault?: string;
|
||||
}
|
||||
|
||||
export function getExe(): string {
|
||||
checkToy();
|
||||
if (process.platform == "win32") {
|
||||
return path.join(
|
||||
process.env.LOCALAPPDATA!,
|
||||
@ -21,9 +22,7 @@ export function getExe(): string {
|
||||
"app.asar"
|
||||
);
|
||||
}
|
||||
if (process.platform == "darwin") {
|
||||
throw new Error("use a non-toy operating system, dumbass");
|
||||
}
|
||||
|
||||
const possibleDirs = [
|
||||
"/opt/Obsidian",
|
||||
"/usr/lib/Obsidian",
|
||||
@ -41,28 +40,48 @@ export function getExe(): string {
|
||||
return "";
|
||||
}
|
||||
|
||||
async function selectNewVault(page: Page, vaultPath: string) {
|
||||
try {
|
||||
let btn = await page.locator("button", { hasText: "Open" });
|
||||
const promise = page.waitForEvent("filechooser");
|
||||
await btn.click();
|
||||
const chooser = await promise;
|
||||
let el = chooser.element();
|
||||
console.log("chooser el", el);
|
||||
el.setInputFiles(vaultPath);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
console.debug(
|
||||
"congratulations, you don't have to choose a vault manually!"
|
||||
);
|
||||
function checkToy() {
|
||||
if (process.platform == "darwin") {
|
||||
throw new Error("use a non-toy operating system, dumbass");
|
||||
}
|
||||
}
|
||||
|
||||
function generateVaultConfig(vault: string) {
|
||||
const vaultHash = randomBytes(16).toString("hex").toLocaleLowerCase();
|
||||
let configLocation;
|
||||
checkToy();
|
||||
if (process.platform == "win32") {
|
||||
configLocation = path.join(`${process.env.LOCALAPPDATA}`, "Obsidian");
|
||||
} else {
|
||||
configLocation = path.join(`${process.env.XDG_CONFIG_HOME}`, "obsidian");
|
||||
}
|
||||
const obsidianConfigFile = path.join(configLocation, "obsidian.json");
|
||||
const json: {
|
||||
vaults: {
|
||||
[key: string]: {
|
||||
path: string;
|
||||
ts: number;
|
||||
open?: boolean;
|
||||
};
|
||||
};
|
||||
} = JSON.parse(readFileSync(obsidianConfigFile).toString());
|
||||
|
||||
if (!Object.values(json.vaults).some((a) => a.path === vault)) {
|
||||
json.vaults[vaultHash] = {
|
||||
path: vault,
|
||||
ts: Date.now(),
|
||||
};
|
||||
writeFileSync(obsidianConfigFile, JSON.stringify(json));
|
||||
writeFileSync(path.join(configLocation, `${vaultHash}.json`), "{}");
|
||||
}
|
||||
}
|
||||
|
||||
const obsidianTestFixtures: Fixtures<ObsidianTestFixtures> = {
|
||||
electronApp: [
|
||||
async ({}, run) => {
|
||||
async ({ obsidian: { vault } }, run) => {
|
||||
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
|
||||
console.log("asar located at:", getExe());
|
||||
if (vault) generateVaultConfig(vault);
|
||||
const electronApp = await electron.launch({
|
||||
timeout: 60000,
|
||||
args: [getExe()].filter((a) => !!a) as string[],
|
||||
@ -79,13 +98,12 @@ const obsidianTestFixtures: Fixtures<ObsidianTestFixtures> = {
|
||||
{ timeout: 60000 },
|
||||
],
|
||||
page: [
|
||||
async ({ electronApp, obsidian: { vault } }, run) => {
|
||||
async ({ electronApp }, run) => {
|
||||
const windows = electronApp.windows();
|
||||
// console.log("windows", windows);
|
||||
let page = windows[windows.length - 1]!;
|
||||
await page.waitForEvent("load");
|
||||
await page.waitForLoadState("domcontentloaded");
|
||||
await selectNewVault(page, vault!);
|
||||
page = electronApp.windows()[electronApp.windows().length - 1]!;
|
||||
await page.waitForEvent("load");
|
||||
await page.waitForLoadState("domcontentloaded");
|
||||
|
Loading…
Reference in New Issue
Block a user