22import * as yaml from 'js-yaml'
33import { readFile , writeFile , access } from 'fs/promises' ;
44import { join , resolve , relative , dirname } from 'path'
5- import { createRequire } from 'module'
65import chalk from 'chalk'
76
87export async function capsule ( {
@@ -220,7 +219,7 @@ export async function capsule({
220219}
221220capsule [ '#' ] = 't44/caps/WorkspaceConfigFile'
222221
223- function resolveExtendPath ( extendPath : string , configDir : string , workspaceRequire : NodeRequire ) : string {
222+ function resolveExtendPath ( extendPath : string , configDir : string ) : string {
224223 if ( extendPath . startsWith ( '.' ) ) {
225224 return resolve ( configDir , extendPath )
226225 } else {
@@ -243,9 +242,21 @@ function resolveExtendPath(extendPath: string, configDir: string, workspaceRequi
243242 filePath = parts . slice ( 1 ) . join ( '/' )
244243 }
245244
246- // Resolve the package's package.json to get its directory
247- const packageJsonPath = workspaceRequire . resolve ( `${ packageName } /package.json` )
248- const packageDir = join ( packageJsonPath , '..' )
245+ // Walk up from configDir looking for node_modules/<packageName>
246+ const { existsSync } = require ( 'fs' )
247+ let searchDir = configDir
248+ let packageDir = ''
249+ while ( searchDir !== dirname ( searchDir ) ) {
250+ const candidate = join ( searchDir , 'node_modules' , packageName )
251+ if ( existsSync ( join ( candidate , 'package.json' ) ) ) {
252+ packageDir = candidate
253+ break
254+ }
255+ searchDir = dirname ( searchDir )
256+ }
257+ if ( ! packageDir ) {
258+ throw new Error ( `Cannot resolve package '${ packageName } ' from '${ configDir } '. Ensure it is installed in node_modules.` )
259+ }
249260
250261 // Resolve the file path within the package
251262 return resolve ( packageDir , filePath )
@@ -257,9 +268,6 @@ async function loadConfigWithExtends(configPath: string, workspaceRootDir: strin
257268 const mainConfigDir = join ( resolve ( configPath ) , '..' )
258269 let configTree : any = null
259270
260- // Create a require function relative to workspace root for module resolution
261- const workspaceRequire = createRequire ( join ( workspaceRootDir , 'package.json' ) )
262-
263271 async function loadConfigRecursive ( currentPath : string , referencedFrom ?: string , chain : string [ ] = [ ] ) : Promise < any > {
264272 const absolutePath = resolve ( currentPath )
265273
@@ -484,7 +492,7 @@ async function loadConfigWithExtends(configPath: string, workspaceRootDir: strin
484492 if ( config . extends && Array . isArray ( config . extends ) ) {
485493 for ( const extendPath of config . extends ) {
486494 // Always use configDir for resolution - it's the directory of the file containing the extends
487- const resolvedExtendPath = resolveExtendPath ( extendPath , configDir , workspaceRequire )
495+ const resolvedExtendPath = resolveExtendPath ( extendPath , configDir )
488496 const childNode = await loadConfigRecursive ( resolvedExtendPath , absolutePath , currentChain )
489497 // Store the original extends value for display
490498 childNode . extendsValue = extendPath
0 commit comments