@@ -52,6 +52,44 @@ export interface BuildSignaturesFromContextResult {
5252 baseDirectory : string ;
5353}
5454
55+ /**
56+ * Resolves the on-disk path of this module in both ESM and CJS bundles.
57+ * Falls back to __filename when bundlers strip import.meta.url.
58+ */
59+ function getModulePathFromImportMeta ( ) : string | undefined {
60+ const moduleUrl = ( import . meta as unknown as { url ?: string } | undefined )
61+ ?. url ;
62+ if ( typeof moduleUrl === 'string' ) {
63+ try {
64+ return fileURLToPath ( moduleUrl ) ;
65+ } catch ( error ) {
66+ console . warn (
67+ 'Failed to resolve fileURLToPath from import.meta.url:' ,
68+ error
69+ ) ;
70+ }
71+ }
72+
73+ return undefined ;
74+ }
75+
76+ /**
77+ * Resolves the on-disk path of this module in both ESM and CJS bundles.
78+ * Falls back to __filename when bundlers strip import.meta.url.
79+ */
80+ function getCurrentModulePath ( ) : string | undefined {
81+ const modulePath = getModulePathFromImportMeta ( ) ;
82+ if ( modulePath ) {
83+ return modulePath ;
84+ }
85+
86+ if ( typeof __filename !== 'undefined' ) {
87+ return __filename ;
88+ }
89+
90+ return undefined ;
91+ }
92+
5593/**
5694 * Gets the base directory for resolving paths
5795 * @param useScriptDirectory - Whether to use script's directory or current working directory
@@ -75,9 +113,14 @@ function getBaseDirectory(
75113 return __dirname ;
76114 }
77115 // When running as module without callerPath
78- const moduleDir = dirname ( fileURLToPath ( import . meta. url ) ) ;
79- console . log ( 'Using module directory:' , moduleDir ) ;
80- return moduleDir ;
116+ const modulePath = getCurrentModulePath ( ) ;
117+ if ( modulePath ) {
118+ const moduleDir = dirname ( modulePath ) ;
119+ console . log ( 'Using module directory:' , moduleDir ) ;
120+ return moduleDir ;
121+ }
122+ console . log ( 'Using current working directory:' , process . cwd ( ) ) ;
123+ return process . cwd ( ) ;
81124 }
82125 // Use current working directory
83126 const cwd = process . cwd ( ) ;
@@ -165,6 +208,17 @@ function generateAbiSignatures(networkData: NetworkCache) {
165208 if ( methodsByContract . has ( contractName ) ) {
166209 const methods = methodsByContract . get ( contractName ) ! ;
167210 const contractMethods = extractAbiMethods ( networkData , methods ) ;
211+ const missingMethods = methods . filter (
212+ ( methodName ) => ! contractMethods [ methodName ]
213+ ) ;
214+
215+ if ( missingMethods . length > 0 ) {
216+ throw new Error (
217+ `Missing ABI definitions for ${ contractName } : ${ missingMethods . join (
218+ ', '
219+ ) } . ` + 'Ensure your networkContext.json includes these functions.'
220+ ) ;
221+ }
168222
169223 if ( Object . keys ( contractMethods ) . length > 0 ) {
170224 const address = contractGroup . contracts [ 0 ] . address_hash ;
@@ -218,6 +272,8 @@ export function buildSignaturesFromContext(
218272 console . log ( '📊 Generating signatures...' ) ;
219273 const signatures = generateAbiSignatures ( jsonData ) ;
220274
275+ console . log ( '✅ Signatures generated successfully for network:' , networkName ) ;
276+
221277 return {
222278 signatures,
223279 networkName,
@@ -308,9 +364,12 @@ module.exports = {
308364// process.argv[0] is the bun executable
309365// process.argv[1] is the script being run
310366const mainScriptPath = path . resolve ( process . argv [ 1 ] || '' ) ;
311- const currentScriptPath = fileURLToPath ( import . meta. url ) ;
367+ const modulePathFromMeta = getModulePathFromImportMeta ( ) ;
368+ const resolvedModulePath = modulePathFromMeta
369+ ? path . resolve ( modulePathFromMeta )
370+ : undefined ;
312371
313- if ( mainScriptPath === currentScriptPath ) {
372+ if ( resolvedModulePath && mainScriptPath === resolvedModulePath ) {
314373 // This means custom-network-signatures.ts was the script passed to `bun run`
315374 const jsonFilePath = process . argv [ 2 ] ;
316375 const networkName = process . argv [ 3 ] ;
0 commit comments