|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFile, spawn } from "node:child_process"; |
| 3 | +import path from "node:path"; |
| 4 | +import process from "node:process"; |
| 5 | +import { promisify } from "node:util"; |
| 6 | +import { provisionVault } from "./provision-obsidian-e2e-vault.mjs"; |
| 7 | +import { |
| 8 | + launchObsidianInstance, |
| 9 | + parseArgs as parseInstanceArgs, |
| 10 | + prepareObsidianProfile, |
| 11 | + resolveInstanceOptions, |
| 12 | + trustVaultAndVerifyPodNotes, |
| 13 | + waitForInstanceReady, |
| 14 | +} from "./start-obsidian-e2e-instance.mjs"; |
| 15 | + |
| 16 | +const execFileAsync = promisify(execFile); |
| 17 | +const VALUE_OPTIONS = new Set([ |
| 18 | + "--vault", |
| 19 | + "--root", |
| 20 | + "--worktree", |
| 21 | + "--data", |
| 22 | + "--profile-root", |
| 23 | + "--obsidian-app", |
| 24 | + "--obsidian-bin", |
| 25 | +]); |
| 26 | +const BOOLEAN_OPTIONS = new Set(["--force"]); |
| 27 | +const DEFAULT_COMMAND = ["eval", "code=app.vault.getName()"]; |
| 28 | + |
| 29 | +function printUsage() { |
| 30 | + console.log(`Usage: node scripts/obsidian-e2e-cli.mjs [instance options] [--] <obsidian command...> |
| 31 | +
|
| 32 | +Examples: |
| 33 | + npm run obsidian:e2e -- eval code=app.vault.getName() |
| 34 | + npm run obsidian:e2e -- dev:errors |
| 35 | + npm run obsidian:e2e -- --vault podnotes-my-worktree eval code='app.plugins.plugins.podnotes?.manifest?.version' |
| 36 | +
|
| 37 | +Instance options: |
| 38 | + --vault <name> Vault/profile name. Defaults to podnotes-<worktree>. |
| 39 | + --root <path> Directory that contains provisioned vaults. Defaults to .obsidian-e2e-vaults. |
| 40 | + --worktree <path> PodNotes worktree to link plugin files from. Defaults to cwd. |
| 41 | + --data <path> Optional PodNotes data.json seed to copy on first provision. |
| 42 | + --profile-root <path> Directory for per-vault Obsidian HOME profiles. Defaults to /tmp/podnotes-obsidian-e2e. |
| 43 | + --obsidian-app <name> Obsidian app name for macOS open. Defaults to Obsidian. |
| 44 | + --obsidian-bin <path> Obsidian CLI executable. Defaults to obsidian. |
| 45 | + --force Recreate plugin symlinks if they already exist. |
| 46 | + --help Show this help. |
| 47 | +`); |
| 48 | +} |
| 49 | + |
| 50 | +export function parseArgs(argv) { |
| 51 | + const instanceArgs = []; |
| 52 | + const commandArgs = []; |
| 53 | + |
| 54 | + for (let index = 0; index < argv.length; index += 1) { |
| 55 | + const arg = argv[index]; |
| 56 | + if (arg === "--") { |
| 57 | + const next = argv[index + 1]; |
| 58 | + if ( |
| 59 | + index === 0 && |
| 60 | + (next === "--help" || |
| 61 | + BOOLEAN_OPTIONS.has(next) || |
| 62 | + VALUE_OPTIONS.has(next)) |
| 63 | + ) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + commandArgs.push(...argv.slice(index + 1)); |
| 67 | + break; |
| 68 | + } |
| 69 | + if (arg === "--help") { |
| 70 | + return { help: true, instanceArgs, commandArgs }; |
| 71 | + } |
| 72 | + if (BOOLEAN_OPTIONS.has(arg)) { |
| 73 | + instanceArgs.push(arg); |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (VALUE_OPTIONS.has(arg)) { |
| 77 | + const value = argv[index + 1]; |
| 78 | + if (!value || value.startsWith("--")) { |
| 79 | + throw new Error(`${arg} requires a value.`); |
| 80 | + } |
| 81 | + instanceArgs.push(arg, value); |
| 82 | + index += 1; |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + commandArgs.push(...argv.slice(index)); |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + help: false, |
| 92 | + instanceArgs, |
| 93 | + commandArgs: commandArgs.length > 0 ? commandArgs : [...DEFAULT_COMMAND], |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +export function obsidianEnv(options) { |
| 98 | + return { |
| 99 | + ...process.env, |
| 100 | + HOME: options.obsidianHome, |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +export function obsidianCommandArgs(options, commandArgs) { |
| 105 | + return [`vault=${options.vaultName}`, ...commandArgs]; |
| 106 | +} |
| 107 | + |
| 108 | +async function isInstanceReady(options) { |
| 109 | + try { |
| 110 | + const { stdout } = await execFileAsync( |
| 111 | + options.obsidianBin, |
| 112 | + obsidianCommandArgs(options, ["vault", "info=path"]), |
| 113 | + { |
| 114 | + env: obsidianEnv(options), |
| 115 | + timeout: 5_000, |
| 116 | + }, |
| 117 | + ); |
| 118 | + return path.resolve(stdout.trim()) === path.resolve(options.vaultPath); |
| 119 | + } catch { |
| 120 | + return false; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export async function ensureObsidianInstance(options) { |
| 125 | + const provisionResult = await provisionVault(options); |
| 126 | + const profileResult = await prepareObsidianProfile(options); |
| 127 | + options.userDataPath = profileResult.userDataPath; |
| 128 | + |
| 129 | + if (!(await isInstanceReady(options))) { |
| 130 | + await launchObsidianInstance(options); |
| 131 | + await waitForInstanceReady(options); |
| 132 | + } |
| 133 | + |
| 134 | + await trustVaultAndVerifyPodNotes(options); |
| 135 | + |
| 136 | + return { |
| 137 | + ...provisionResult, |
| 138 | + ...profileResult, |
| 139 | + obsidianHome: options.obsidianHome, |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +function spawnObsidian(options, commandArgs) { |
| 144 | + return new Promise((resolve) => { |
| 145 | + const child = spawn( |
| 146 | + options.obsidianBin, |
| 147 | + obsidianCommandArgs(options, commandArgs), |
| 148 | + { |
| 149 | + env: obsidianEnv(options), |
| 150 | + stdio: "inherit", |
| 151 | + }, |
| 152 | + ); |
| 153 | + child.on("close", (code, signal) => { |
| 154 | + if (signal) { |
| 155 | + process.kill(process.pid, signal); |
| 156 | + return; |
| 157 | + } |
| 158 | + resolve(code ?? 1); |
| 159 | + }); |
| 160 | + child.on("error", (error) => { |
| 161 | + console.error(error instanceof Error ? error.message : error); |
| 162 | + resolve(1); |
| 163 | + }); |
| 164 | + }); |
| 165 | +} |
| 166 | + |
| 167 | +async function main() { |
| 168 | + const parsed = parseArgs(process.argv.slice(2)); |
| 169 | + if (parsed.help) { |
| 170 | + printUsage(); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + const options = resolveInstanceOptions( |
| 175 | + parseInstanceArgs(parsed.instanceArgs), |
| 176 | + ); |
| 177 | + await ensureObsidianInstance(options); |
| 178 | + process.exitCode = await spawnObsidian(options, parsed.commandArgs); |
| 179 | +} |
| 180 | + |
| 181 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 182 | + main().catch((error) => { |
| 183 | + console.error(error instanceof Error ? error.message : error); |
| 184 | + process.exitCode = 1; |
| 185 | + }); |
| 186 | +} |
0 commit comments