trm-core
Version:
TRM (Transport Request Manager) Core
60 lines (59 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNodePackage = getNodePackage;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const trm_commons_1 = require("trm-commons");
function getNodePackage(globalPath, packageName) {
if (!globalPath) {
globalPath = (0, trm_commons_1.getGlobalNodeModules)();
}
if (__dirname.includes(globalPath)) {
const parts = __dirname.split(node_path_1.sep);
const index = parts.lastIndexOf("node_modules");
if (packageName) {
try {
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(parts.slice(0, index + 1).join(node_path_1.sep), packageName, 'package.json'), 'utf8'));
}
catch (_a) { }
try {
const data = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)((0, node_path_1.resolve)(parts.slice(0, index + 1).join(node_path_1.sep), '..'), 'package.json'), 'utf8'));
if (data.name === packageName) {
return data;
}
}
catch (_b) { }
}
else {
try {
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(parts.slice(0, index + 1).join(node_path_1.sep), 'trm-core', 'package.json'), 'utf8'));
}
catch (_c) { }
}
}
else {
if (!packageName) {
packageName = 'trm-core';
}
try {
const data = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(process.cwd(), 'package.json'), 'utf8'));
if (data.name === packageName) {
return data;
}
}
catch (_d) { }
try {
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(process.cwd(), 'node_modules', packageName, 'package.json'), 'utf8'));
}
catch (_e) { }
try {
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)((0, node_path_1.resolve)(process.cwd(), ".."), packageName, 'package.json'), 'utf8'));
}
catch (_f) { }
try {
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)((0, node_path_1.resolve)(process.cwd(), ".."), 'node_modules', packageName, 'package.json'), 'utf8'));
}
catch (_g) { }
}
throw new Error(`Couldn't find "${packageName}" package.json!`);
}