From bcd9ac04a7b33aa92bf412b5a326afa679ef5e8e Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Tue, 25 Nov 2025 14:40:58 +0000 Subject: [PATCH] fix: use require.resolve to find ghostty-web package This properly handles npx installations where dependencies may be hoisted to various locations in the npm cache. --- demo/bin/demo.js | 49 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/demo/bin/demo.js b/demo/bin/demo.js index 75d2c58e..6faebb5c 100644 --- a/demo/bin/demo.js +++ b/demo/bin/demo.js @@ -27,29 +27,31 @@ const WS_PORT = 3001; // Locate ghostty-web assets // ============================================================================ +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); + function findGhosttyWeb() { - const possiblePaths = [ - // Development: running from repo root (demo/bin/demo.js -> ../../dist) - path.join(__dirname, '..', '..', 'dist'), - // When installed as dependency (demo/node_modules/ghostty-web/dist) - path.join(__dirname, '..', 'node_modules', 'ghostty-web', 'dist'), - // When in a monorepo or hoisted - path.join(__dirname, '..', '..', 'node_modules', 'ghostty-web', 'dist'), - path.join(__dirname, '..', '..', '..', 'node_modules', 'ghostty-web', 'dist'), - ]; - - for (const p of possiblePaths) { - const jsPath = path.join(p, 'ghostty-web.js'); - if (fs.existsSync(jsPath)) { - // Find WASM file - check both dist/ and parent directory - let wasmPath = path.join(p, 'ghostty-vt.wasm'); - if (!fs.existsSync(wasmPath)) { - wasmPath = path.join(path.dirname(p), 'ghostty-vt.wasm'); - } - if (fs.existsSync(wasmPath)) { - return { distPath: p, wasmPath }; - } + // First, check for local development (repo root dist/) + const localDist = path.join(__dirname, '..', '..', 'dist'); + const localJs = path.join(localDist, 'ghostty-web.js'); + const localWasm = path.join(__dirname, '..', '..', 'ghostty-vt.wasm'); + + if (fs.existsSync(localJs) && fs.existsSync(localWasm)) { + return { distPath: localDist, wasmPath: localWasm, isDev: true }; + } + + // Use require.resolve to find the installed ghostty-web package + try { + const ghosttyWebMain = require.resolve('ghostty-web'); + const ghosttyWebRoot = path.dirname(ghosttyWebMain.replace(/[/\\]dist[/\\].*$/, '')); + const distPath = path.join(ghosttyWebRoot, 'dist'); + const wasmPath = path.join(ghosttyWebRoot, 'ghostty-vt.wasm'); + + if (fs.existsSync(path.join(distPath, 'ghostty-web.js')) && fs.existsSync(wasmPath)) { + return { distPath, wasmPath, isDev: false }; } + } catch (e) { + // require.resolve failed, package not found } console.error('Error: Could not find ghostty-web package.'); @@ -59,10 +61,7 @@ function findGhosttyWeb() { process.exit(1); } -const { distPath, wasmPath } = findGhosttyWeb(); -const isDev = - distPath.includes(path.join('demo', '..', 'dist')) || - distPath === path.join(__dirname, '..', '..', 'dist'); +const { distPath, wasmPath, isDev } = findGhosttyWeb(); // ============================================================================ // HTML Template