@kubiklabs/wasmkit
Version:
Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.
40 lines (39 loc) • 1.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClosestCallerPackage = void 0;
const find_up_1 = __importDefault(require("find-up"));
const path_1 = __importDefault(require("path"));
function findClosestPackageJson(file) {
return find_up_1.default.sync('package.json', { cwd: path_1.default.dirname(file) }) ?? null;
}
/**
* Returns the name of the closest package in the callstack that isn't this.
*/
function getClosestCallerPackage() {
const previousPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = (e, s) => s;
const error = new Error();
// eslint-disable-next-line
const stack = error.stack;
Error.prepareStackTrace = previousPrepareStackTrace;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const currentPackage = findClosestPackageJson(__filename);
for (const callSite of stack) {
const fileName = callSite.getFileName();
if (fileName !== null && path_1.default.isAbsolute(fileName)) {
const callerPackage = findClosestPackageJson(fileName);
if (callerPackage === currentPackage) {
continue;
}
if (callerPackage === null) {
return undefined;
}
return require(callerPackage).name; // eslint-disable-line @typescript-eslint/no-var-requires
}
}
return undefined;
}
exports.getClosestCallerPackage = getClosestCallerPackage;