trm-core
Version:
TRM (Transport Request Manager) Core
103 lines (102 loc) • 3.87 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNodePackage = getNodePackage;
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const get_root_path_1 = require("get-root-path");
const trm_commons_1 = require("trm-commons");
const get_stack_trace_1 = require("get-stack-trace");
const _findPackageRoot = (startPath) => {
var currentPath = fs.statSync(startPath).isDirectory() ? startPath : path_1.default.dirname(startPath);
while (true) {
const packageJsonPath = path_1.default.join(currentPath, 'package.json');
if (fs.existsSync(packageJsonPath)) {
return currentPath;
}
const parentPath = path_1.default.dirname(currentPath);
if (parentPath === currentPath) {
return null;
}
currentPath = parentPath;
}
};
function getNodePackage() {
var file;
var packageData;
trm_commons_1.Logger.log(`root path: ${get_root_path_1.rootPath}`, true);
const stack = (0, get_stack_trace_1.getStackTrace)();
var modules = [];
stack.forEach(o => {
var searchModule = true;
modules.forEach(m => {
if (o.fileName.startsWith(m.packageRoot)) {
searchModule = false;
}
});
if (searchModule) {
try {
const packageRoot = _findPackageRoot(o.fileName);
const modulePackage = JSON.parse(fs.readFileSync(path_1.default.join(packageRoot, "package.json")).toString());
if (modulePackage.name) {
modules.push({
name: modulePackage.name,
packageRoot
});
}
}
catch (e) { }
}
});
try {
if (modules.length === 1) {
file = fs.readFileSync(path_1.default.join(get_root_path_1.rootPath, "package.json"));
}
else {
file = fs.readFileSync(path_1.default.join(modules[modules.length - 1].packageRoot, `/node_modules/trm-core/package.json`));
}
}
catch (e) { }
if (file) {
packageData = JSON.parse(file.toString());
}
if (!packageData || packageData.name !== 'trm-core') {
throw new Error(`package.json not found!`);
}
return packageData;
}