UNPKG

2.5 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var tslib_1 = require("tslib");
3var fs_1 = require("fs");
4var path_1 = require("path");
5var moduleCache;
6/** Extract information about paths */
7function getPaths() {
8 try {
9 return require.cache ? Object.keys(require.cache) : [];
10 }
11 catch (e) {
12 return [];
13 }
14}
15/** Extract information about package.json modules */
16function collectModules() {
17 var mainPaths = (require.main && require.main.paths) || [];
18 var paths = getPaths();
19 var infos = {};
20 var seen = {};
21 paths.forEach(function (path) {
22 var dir = path;
23 /** Traverse directories upward in the search of package.json file */
24 var updir = function () {
25 var orig = dir;
26 dir = path_1.dirname(orig);
27 if (!dir || orig === dir || seen[orig]) {
28 return undefined;
29 }
30 if (mainPaths.indexOf(dir) < 0) {
31 return updir();
32 }
33 var pkgfile = path_1.join(orig, 'package.json');
34 seen[orig] = true;
35 if (!fs_1.existsSync(pkgfile)) {
36 return updir();
37 }
38 try {
39 var info = JSON.parse(fs_1.readFileSync(pkgfile, 'utf8'));
40 infos[info.name] = info.version;
41 }
42 catch (_oO) {
43 // no-empty
44 }
45 };
46 updir();
47 });
48 return infos;
49}
50/** Add node modules / packages to the event */
51var Modules = /** @class */ (function () {
52 function Modules() {
53 /**
54 * @inheritDoc
55 */
56 this.name = Modules.id;
57 }
58 /**
59 * @inheritDoc
60 */
61 Modules.prototype.setupOnce = function (addGlobalEventProcessor, getCurrentHub) {
62 var _this = this;
63 addGlobalEventProcessor(function (event) {
64 if (!getCurrentHub().getIntegration(Modules)) {
65 return event;
66 }
67 return tslib_1.__assign(tslib_1.__assign({}, event), { modules: _this._getModules() });
68 });
69 };
70 /** Fetches the list of modules and the versions loaded by the entry file for your node.js app. */
71 Modules.prototype._getModules = function () {
72 if (!moduleCache) {
73 moduleCache = collectModules();
74 }
75 return moduleCache;
76 };
77 /**
78 * @inheritDoc
79 */
80 Modules.id = 'Modules';
81 return Modules;
82}());
83exports.Modules = Modules;
84//# sourceMappingURL=modules.js.map
\No newline at end of file