|
9 | 9 | import path from 'path'; |
10 | 10 | import fs from 'fs'; |
11 | 11 | import net from 'net'; |
| 12 | +import { createRequire } from 'module'; |
12 | 13 | import { spawn, type ChildProcess } from 'child_process'; |
13 | 14 | import chalk from 'chalk'; |
14 | 15 |
|
@@ -49,14 +50,32 @@ export function resolveStudioPath(): string | null { |
49 | 50 | } |
50 | 51 |
|
51 | 52 | // Fallback: resolve from node_modules |
52 | | - try { |
53 | | - const { createRequire } = require('module'); |
54 | | - const req = createRequire(import.meta.url); |
55 | | - const resolved = req.resolve('@objectstack/studio/package.json'); |
56 | | - return path.dirname(resolved); |
57 | | - } catch { |
58 | | - return null; |
| 53 | + // Try resolving from the consumer's working directory first (important for |
| 54 | + // pnpm strict isolation where the CLI cannot see the consumer's dependencies), |
| 55 | + // then fall back to resolving from the CLI's own location. |
| 56 | + const resolutionBases = [ |
| 57 | + path.join(cwd, '__placeholder__.js'), // consumer workspace |
| 58 | + import.meta.url, // CLI package itself |
| 59 | + ]; |
| 60 | + |
| 61 | + for (const base of resolutionBases) { |
| 62 | + try { |
| 63 | + const req = createRequire(base); |
| 64 | + const resolved = req.resolve('@objectstack/studio/package.json'); |
| 65 | + return path.dirname(resolved); |
| 66 | + } catch (e) { |
| 67 | + // Not resolvable from this base — try next |
| 68 | + if (process.env.DEBUG) console.error(` [studio] resolve from ${base} failed:`, (e as Error).message); |
| 69 | + } |
59 | 70 | } |
| 71 | + |
| 72 | + // Last resort: direct filesystem check in node_modules |
| 73 | + const directPath = path.join(cwd, 'node_modules', '@objectstack', 'studio'); |
| 74 | + if (fs.existsSync(path.join(directPath, 'package.json'))) { |
| 75 | + return directPath; |
| 76 | + } |
| 77 | + |
| 78 | + return null; |
60 | 79 | } |
61 | 80 |
|
62 | 81 | /** |
|
0 commit comments