Skip to content

Commit

Permalink
ref: Remove lsmod dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Feb 26, 2019
1 parent 38a12ff commit 051ce83
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@sentry/hub": "4.6.3",
"@sentry/types": "4.5.3",
"@sentry/utils": "4.6.3",
"lsmod": "1.0.0",
"tslib": "^1.9.3"
},
"devDependencies": {
Expand Down
56 changes: 54 additions & 2 deletions packages/integrations/src/modules.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,61 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { Integration } from '@sentry/types';
import * as lsmod from 'lsmod';
import { existsSync, readFileSync } from 'fs';
import { dirname, join } from 'path';

let moduleCache: { [key: string]: string };

/** Extract information about package.json modules */
function collectModules(): {
[name: string]: string;
} {
const mainPaths = (require.main && require.main.paths) || [];
const paths = require.cache ? Object.keys(require.cache as {}) : [];
const infos: {
[name: string]: string;
} = {};
const seen: {
[path: string]: boolean;
} = {};

paths.forEach(path => {
let dir = path;

/** Traverse directories upward in the search of package.json file */
const updir = (): void | (() => void) => {
const orig = dir;
dir = dirname(orig);

if (!dir || orig === dir || seen[orig]) {
return undefined;
} else 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')) as {
name: string;
version: string;
};
infos[info.name] = info.version;
} catch (_oO) {
// no-empty
}
};

updir();
});

return infos;
}

/** Add node modules / packages to the event */
export class Modules implements Integration {
/**
Expand Down Expand Up @@ -34,7 +86,7 @@ export class Modules implements Integration {
private getModules(): { [key: string]: string } {
if (!moduleCache) {
// tslint:disable-next-line:no-unsafe-any
moduleCache = lsmod();
moduleCache = collectModules();
}
return moduleCache;
}
Expand Down

0 comments on commit 051ce83

Please sign in to comment.