@@ -17,6 +17,7 @@ import { type CliCommand, type InternalCliCommand, type Arg, Strategy, registerC
1717import { getErrorMessage } from './errors.js' ;
1818import { log } from './logger.js' ;
1919import type { ManifestEntry } from './build-manifest.js' ;
20+ import { findPackageRoot , getCliManifestPath , getFetchAdaptersScriptPath } from './package-paths.js' ;
2021
2122/** User runtime directory: ~/.opencli */
2223export const USER_OPENCLI_DIR = path . join ( os . homedir ( ) , '.opencli' ) ;
@@ -37,18 +38,7 @@ function parseStrategy(rawStrategy: string | undefined, fallback: Strategy = Str
3738
3839import { isRecord } from './utils.js' ;
3940
40- /**
41- * Find the package root (directory containing package.json).
42- * Dev: import.meta.url is in src/ → one level up.
43- * Prod: import.meta.url is in dist/src/ → two levels up.
44- */
45- function findPackageRoot ( ) : string {
46- let dir = path . resolve ( path . dirname ( fileURLToPath ( import . meta. url ) ) , '..' ) ;
47- if ( ! fs . existsSync ( path . join ( dir , 'package.json' ) ) ) {
48- dir = path . resolve ( dir , '..' ) ;
49- }
50- return dir ;
51- }
41+ const PACKAGE_ROOT = findPackageRoot ( fileURLToPath ( import . meta. url ) ) ;
5242
5343/**
5444 * Ensure ~/.opencli/node_modules/@jackwener/opencli symlink exists so that
@@ -72,7 +62,7 @@ export async function ensureUserCliCompatShims(baseDir: string = USER_OPENCLI_DI
7262 }
7363
7464 // Create node_modules/@jackwener /opencli symlink pointing to the installed package root.
75- const opencliRoot = findPackageRoot ( ) ;
65+ const opencliRoot = PACKAGE_ROOT ;
7666 const symlinkDir = path . join ( baseDir , 'node_modules' , '@jackwener' ) ;
7767 const symlinkPath = path . join ( symlinkDir , 'opencli' ) ;
7868 try {
@@ -118,7 +108,7 @@ export async function ensureUserAdapters(): Promise<void> {
118108 log . info ( 'First run detected — copying adapters (one-time setup)...' ) ;
119109 try {
120110 const { execFileSync } = await import ( 'node:child_process' ) ;
121- const scriptPath = path . join ( findPackageRoot ( ) , 'scripts' , 'fetch-adapters.js' ) ;
111+ const scriptPath = getFetchAdaptersScriptPath ( PACKAGE_ROOT ) ;
122112 execFileSync ( process . execPath , [ scriptPath ] , {
123113 stdio : 'inherit' ,
124114 env : { ...process . env , _OPENCLI_FIRST_RUN : '1' } ,
@@ -137,7 +127,7 @@ export async function ensureUserAdapters(): Promise<void> {
137127export async function discoverClis ( ...dirs : string [ ] ) : Promise < void > {
138128 // Fast path: try manifest first (production / post-build)
139129 for ( const dir of dirs ) {
140- const manifestPath = path . resolve ( dir , '..' , 'cli-manifest.json' ) ;
130+ const manifestPath = getCliManifestPath ( dir ) ;
141131 try {
142132 await fs . promises . access ( manifestPath ) ;
143133 const loaded = await loadFromManifest ( manifestPath , dir ) ;
0 commit comments