Skip to content

Commit 2d6057a

Browse files
committed
fix(contracts): add getCurrentModulePath function to resolve module paths safely
1 parent 6bc6878 commit 2d6057a

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

packages/contracts/dist/custom-network-signatures.cjs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5064,6 +5064,20 @@ function extractAbiMethods(networkCache, methodNames) {
50645064
}
50655065

50665066
// packages/contracts/src/custom-network-signatures.ts
5067+
function getCurrentModulePath() {
5068+
const moduleUrl = __import_meta__?.url;
5069+
if (typeof moduleUrl === "string") {
5070+
try {
5071+
return (0, import_url.fileURLToPath)(moduleUrl);
5072+
} catch (error) {
5073+
console.warn("Failed to resolve fileURLToPath from import.meta.url:", error);
5074+
}
5075+
}
5076+
if (typeof __filename !== "undefined") {
5077+
return __filename;
5078+
}
5079+
return void 0;
5080+
}
50675081
function getBaseDirectory(useScriptDirectory = false, callerPath) {
50685082
if (useScriptDirectory) {
50695083
if (callerPath) {
@@ -5075,9 +5089,14 @@ function getBaseDirectory(useScriptDirectory = false, callerPath) {
50755089
console.log("Using __dirname:", __dirname);
50765090
return __dirname;
50775091
}
5078-
const moduleDir = (0, import_path.dirname)((0, import_url.fileURLToPath)(__import_meta__.url));
5079-
console.log("Using module directory:", moduleDir);
5080-
return moduleDir;
5092+
const modulePath = getCurrentModulePath();
5093+
if (modulePath) {
5094+
const moduleDir = (0, import_path.dirname)(modulePath);
5095+
console.log("Using module directory:", moduleDir);
5096+
return moduleDir;
5097+
}
5098+
console.log("Using current working directory:", process.cwd());
5099+
return process.cwd();
50815100
}
50825101
const cwd = process.cwd();
50835102
console.log("Using current working directory:", cwd);
@@ -5230,8 +5249,9 @@ module.exports = {
52305249
}
52315250
}
52325251
var mainScriptPath = import_path.default.resolve(process.argv[1] || "");
5233-
var currentScriptPath = (0, import_url.fileURLToPath)(__import_meta__.url);
5234-
if (mainScriptPath === currentScriptPath) {
5252+
var currentModulePath = getCurrentModulePath();
5253+
var resolvedModulePath = currentModulePath ? import_path.default.resolve(currentModulePath) : void 0;
5254+
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
52355255
const jsonFilePath = process.argv[2];
52365256
const networkName = process.argv[3];
52375257
if (!jsonFilePath) {

packages/contracts/dist/custom-network-signatures.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5028,6 +5028,20 @@ function extractAbiMethods(networkCache, methodNames) {
50285028
}
50295029

50305030
// packages/contracts/src/custom-network-signatures.ts
5031+
function getCurrentModulePath() {
5032+
const moduleUrl = import.meta?.url;
5033+
if (typeof moduleUrl === "string") {
5034+
try {
5035+
return fileURLToPath(moduleUrl);
5036+
} catch (error) {
5037+
console.warn("Failed to resolve fileURLToPath from import.meta.url:", error);
5038+
}
5039+
}
5040+
if (typeof __filename !== "undefined") {
5041+
return __filename;
5042+
}
5043+
return void 0;
5044+
}
50315045
function getBaseDirectory(useScriptDirectory = false, callerPath) {
50325046
if (useScriptDirectory) {
50335047
if (callerPath) {
@@ -5039,9 +5053,14 @@ function getBaseDirectory(useScriptDirectory = false, callerPath) {
50395053
console.log("Using __dirname:", __dirname);
50405054
return __dirname;
50415055
}
5042-
const moduleDir = dirname(fileURLToPath(import.meta.url));
5043-
console.log("Using module directory:", moduleDir);
5044-
return moduleDir;
5056+
const modulePath = getCurrentModulePath();
5057+
if (modulePath) {
5058+
const moduleDir = dirname(modulePath);
5059+
console.log("Using module directory:", moduleDir);
5060+
return moduleDir;
5061+
}
5062+
console.log("Using current working directory:", process.cwd());
5063+
return process.cwd();
50455064
}
50465065
const cwd = process.cwd();
50475066
console.log("Using current working directory:", cwd);
@@ -5194,8 +5213,9 @@ module.exports = {
51945213
}
51955214
}
51965215
var mainScriptPath = path.resolve(process.argv[1] || "");
5197-
var currentScriptPath = fileURLToPath(import.meta.url);
5198-
if (mainScriptPath === currentScriptPath) {
5216+
var currentModulePath = getCurrentModulePath();
5217+
var resolvedModulePath = currentModulePath ? path.resolve(currentModulePath) : void 0;
5218+
if (resolvedModulePath && mainScriptPath === resolvedModulePath) {
51995219
const jsonFilePath = process.argv[2];
52005220
const networkName = process.argv[3];
52015221
if (!jsonFilePath) {

0 commit comments

Comments
 (0)