forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeModuleCollector.ts
More file actions
79 lines (62 loc) · 1.92 KB
/
NodeModuleCollector.ts
File metadata and controls
79 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { spawnSync } from "child_process";
import { dirname, join, resolve } from "path";
import { argv } from "process";
import { IModuleCollector, ModuleInfo } from "@exceptionless/core";
export class NodeModuleCollector implements IModuleCollector {
private initialized: boolean = false;
private installedModules: { [id: string]: ModuleInfo } = {};
public getModules(): ModuleInfo[] {
if (argv && argv.length < 2) {
return [];
}
this.initialize();
// TODO: Cache this lookup
const modulePath = resolve(join(dirname(argv[1]), "node_modules"));
// TODO: What to do if this doesn't exist..
console.log(modulePath);
const pathLength = modulePath.length;
// TODO: Figure out how to remove require
const loadedKeys: string[] = Object.keys(require.cache);
const loadedModules: { [id: string]: boolean } = {};
loadedKeys.forEach((key) => {
let id = key.substr(pathLength);
id = id.substr(0, id.indexOf("/"));
loadedModules[id] = true;
});
console.log(loadedKeys, loadedModules, module);
return Object.keys(loadedModules)
.map((key) => this.installedModules[key])
.filter((m) => m !== undefined);
}
private initialize() {
if (this.initialized) {
return;
}
this.initialized = true;
let json: { dependencies?: { version: string }[] };
try {
const output = spawnSync("npm", ["ls", "--depth=0", "--json"]).stdout;
if (!output) {
return;
}
json = JSON.parse(output.toString());
} catch (e) {
return;
}
const items = json.dependencies;
if (!items) {
return;
}
let id = 0;
this.installedModules = {};
Object.keys(items).forEach((key) => {
const item = items[key];
const theModule: ModuleInfo = {
module_id: id++,
name: key,
version: item.version,
};
this.installedModules[key] = theModule;
});
}
}