tia
Version:
Time is All (logs driven test engine with ExtJs support)
129 lines • 4.19 kB
JavaScript
;
/* global gIn */
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const util_1 = require("util");
const { engines } = require('../../package.json');
const semver = __importStar(require("semver"));
const _ = __importStar(require("lodash"));
/**
* Clears 'require' cache for specified node module.
* @param {String} resolvedModulePath
*/
function clearRequireCache(resolvedModulePath) {
delete require.cache[resolvedModulePath];
}
exports.clearRequireCache = clearRequireCache;
/**
* Wrapper for 'require'. Allows to clean cache.
*
* @param {String} modPath - path to module.
* @param {Boolean} clearCache - Set to true if cache should be deleted immediately.
* @returns {{res: *, resolvedModPath: String}}
* @throws {*} - Exceptions from 'require' calls.
*/
function requireEx(modPath, clearCache) {
const absFilePath = path.resolve(modPath);
gIn.tracer.msg3(`requireEx: absFilePath: ${absFilePath}.`);
const res = {
result: require(absFilePath),
resolvedModPath: require.resolve(absFilePath),
};
if (clearCache) {
clearRequireCache(res.resolvedModPath);
}
return res;
}
exports.requireEx = requireEx;
/**
* Wrapper for require, do not generate exception if path is absent.
* @param modPath - path to module.
* @return {*} - exports from existing module or empty object if module is absent.
*/
function requireIfExists(modPath) {
try {
return require(modPath);
gIn.tracer.msg3(`requireIfExists: Module found: ${modPath}.`);
}
catch (e) {
gIn.tracer.msg3(`requireIfExists: There is no module: ${modPath}.`);
return {};
}
}
exports.requireIfExists = requireIfExists;
function toMb(val) {
const mb = 1024 * 1024;
return (val / mb).toFixed(3);
}
function toMs(val) {
return (val / 1000).toFixed(3);
}
function getResourcesUsage(isTestLog) {
// gT.config.rssUsageThreshold
const mem = process.memoryUsage();
if (isTestLog && mem.rss < gT.config.rssUsageThreshold * 1e6) {
return '';
}
let str = `Memory MB: ${util_1.inspect({
rss: toMb(mem.rss),
heapTotal: toMb(mem.heapTotal),
heapUsed: toMb(mem.heapUsed),
})}`;
if (process.cpuUsage) {
const cpuU = process.cpuUsage();
str += `\nCPU ms: ${util_1.inspect({
user: toMs(cpuU.user),
system: toMs(cpuU.system),
})}`;
}
return str;
}
exports.getResourcesUsage = getResourcesUsage;
function getProcInfo() {
// Env: ${inspect(process.env)}
const str = `
Arch: ${process.arch}
Cwd: ${process.cwd()}
Proc Exec: ${process.execPath}
Proc Args: ${process.execArgv}
Proc Pid: ${process.pid}
Proc Platform: ${process.platform}
Proc Title: ${process.title}
Proc Uptime: ${process.uptime()}
Node release info: ${util_1.inspect(process.release)}
Node version info: ${util_1.inspect(process.versions)}
${getResourcesUsage()}`;
return str;
}
exports.getProcInfo = getProcInfo;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isPromise(p) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
return _.isObject(p) && _.isFunction(p.then);
}
exports.isPromise = isPromise;
function checkNodeJsVersion() {
const version = engines.node;
if (!semver.satisfies(process.version, version)) {
console.error(`Required node version ${version}, current version ${process.version}.`);
process.exit(1);
}
}
exports.checkNodeJsVersion = checkNodeJsVersion;
function requireArray(modules) {
modules.forEach((modulePath) => {
const modPath = path.resolve(gT.cLParams.rootDir, modulePath);
gIn.tracer.msg1(`requireArray: Requiring module: ${modPath}`);
require(modPath);
});
}
exports.requireArray = requireArray;
//# sourceMappingURL=nodejs-utils.js.map