123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { existsSync, readFileSync } from 'fs';
- import { dirname, join } from 'path';
- import { defineIntegration, convertIntegrationFnToClass } from '@sentry/core';
- let moduleCache;
- const INTEGRATION_NAME = 'Modules';
- function getPaths() {
- try {
- return require.cache ? Object.keys(require.cache ) : [];
- } catch (e) {
- return [];
- }
- }
- function collectModules()
- {
- const mainPaths = (require.main && require.main.paths) || [];
- const paths = getPaths();
- const infos
- = {};
- const seen
- = {};
- paths.forEach(path => {
- let dir = path;
-
- const updir = () => {
- const orig = dir;
- dir = dirname(orig);
- if (!dir || orig === dir || seen[orig]) {
- return undefined;
- }
- if (mainPaths.indexOf(dir) < 0) {
- return updir();
- }
- const pkgfile = join(orig, 'package.json');
- seen[orig] = true;
- if (!existsSync(pkgfile)) {
- return updir();
- }
- try {
- const info = JSON.parse(readFileSync(pkgfile, 'utf8'))
- ;
- infos[info.name] = info.version;
- } catch (_oO) {
-
- }
- };
- updir();
- });
- return infos;
- }
- function _getModules() {
- if (!moduleCache) {
- moduleCache = collectModules();
- }
- return moduleCache;
- }
- const _modulesIntegration = (() => {
- return {
- name: INTEGRATION_NAME,
-
- setupOnce() {},
- processEvent(event) {
- event.modules = {
- ...event.modules,
- ..._getModules(),
- };
- return event;
- },
- };
- }) ;
- const modulesIntegration = defineIntegration(_modulesIntegration);
- const Modules = convertIntegrationFnToClass(INTEGRATION_NAME, modulesIntegration)
- ;
- export { Modules, modulesIntegration };
|