logify-alert
Version:
Node.js client for Logify Alert
206 lines (165 loc) • 8.67 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _compositeCollector2 = require("./compositeCollector.js");
var _compositeCollector3 = _interopRequireDefault(_compositeCollector2);
var _nodeVersionCollector = require("./nodeVersionCollector.js");
var _nodeVersionCollector2 = _interopRequireDefault(_nodeVersionCollector);
var _platformEnvironmentCollector = require("./platformEnvironmentCollector.js");
var _platformEnvironmentCollector2 = _interopRequireDefault(_platformEnvironmentCollector);
var _logifyInfoCollector = require("./logifyInfoCollector.js");
var _logifyInfoCollector2 = _interopRequireDefault(_logifyInfoCollector);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var nodeCollector = function (_compositeCollector) {
_inherits(nodeCollector, _compositeCollector);
function nodeCollector() {
_classCallCheck(this, nodeCollector);
var _this = _possibleConstructorReturn(this, (nodeCollector.__proto__ || Object.getPrototypeOf(nodeCollector)).call(this));
_this.applicationName = undefined;
_this.applicationVersion = undefined;
_this.userId = undefined;
_this._reportData = new Object();
_this._parentProjectJsonFile = null;
_this.collectors.push(new _nodeVersionCollector2.default());
_this.collectors.push(new _platformEnvironmentCollector2.default());
_this.collectors.push(new _logifyInfoCollector2.default());
return _this;
}
_createClass(nodeCollector, [{
key: "process",
value: function process(report) {
_get(nodeCollector.prototype.__proto__ || Object.getPrototypeOf(nodeCollector.prototype), "process", this).call(this, report);
}
}, {
key: "handleException",
value: function handleException(error) {
this._reportData.error = new Object();
this._reportData.error.message = error.message;
this._reportData.error.stack = error.stack;
this.collectData();
}
}, {
key: "handleRejection",
value: function handleRejection(reason) {
this._reportData.rejection = new Object();
if (reason.message != undefined) {
this._reportData.rejection.error = new Object();
this._reportData.rejection.error.message = reason.message;
if (reason.stack != undefined) {
this._reportData.rejection.error.stack = reason.stack;
}
} else {
this._reportData.rejection.reason = reason;
}
this.collectData();
}
}, {
key: "collectData",
value: function collectData() {
this.fillAppData();
this.process(this._reportData);
}
}, {
key: "getApplicationName",
value: function getApplicationName() {
if (this.applicationName != undefined) return this.applicationName;
var process = require("process");
if (process.env.npm_package_name != undefined) return process.env.npm_package_name;
this.getPackageJson();
if (this._parentProjectJsonFile != null) return this._parentProjectJsonFile.name;
return "";
}
}, {
key: "getApplicationVersion",
value: function getApplicationVersion() {
if (this.applicationVersion != undefined) return this.applicationVersion;
var process = require("process");
if (process.env.npm_package_version != undefined) return process.env.npm_package_version;
this.getPackageJson();
if (this._parentProjectJsonFile != null) return this._parentProjectJsonFile.version;
return "";
}
}, {
key: "getUserId",
value: function getUserId() {
return this.userId == undefined ? "" : this.userId;
}
}, {
key: "fillAppData",
value: function fillAppData() {
if (this._reportData.logifyApp === undefined) this._reportData.logifyApp = new Object();
this._reportData.logifyApp.name = this.getApplicationName();
this._reportData.logifyApp.version = this.getApplicationVersion();
this._reportData.logifyApp.userId = this.getUserId();
}
}, {
key: "getPackageJson",
value: function getPackageJson() {
if (this._parentProjectJsonFile != null) return;
var pjsonPath = this.findPathToNodeModules();
if (pjsonPath == null) return null;
pjsonPath = this.findPackageJsonFilePath(pjsonPath);
if (pjsonPath == null) return null;
this._parentProjectJsonFile = require(pjsonPath);
}
}, {
key: "findPathToNodeModules",
value: function findPathToNodeModules() {
var path = require("path");
var dir = path.join(__dirname);
if (dir.indexOf("\\node_modules\\") == -1) return dir;
var result = this.findParentFolderByCondition(dir, function (d) {
return !d.endsWith("node_modules\\");
});
if (result == null) return null;
return path.join(result, '../');
}
}, {
key: "findPackageJsonFilePath",
value: function findPackageJsonFilePath(dir) {
var _this2 = this;
if (dir.charAt(dir.length - 1) != '\\') dir += '\\';
var result = this.findParentFolderByCondition(dir, function (d) {
return !_this2.isFileExist(d + 'package.json');
});
if (result == null) return null;
return result + 'package.json';
}
}, {
key: "findParentFolderByCondition",
value: function findParentFolderByCondition(dir, condition) {
var path = require("path");
var previousDir = "";
while (condition(dir)) {
dir = path.join(dir, '../');
if (dir == previousDir) return null;
previousDir = dir;
}
return dir;
}
}, {
key: "isFileExist",
value: function isFileExist(filePath) {
var fs = require('fs');
try {
fs.accessSync(filePath);
return true;
} catch (e) {
return false;
}
}
}, {
key: "reportData",
get: function get() {
return this._reportData;
}
}]);
return nodeCollector;
}(_compositeCollector3.default);
exports.default = nodeCollector;