forked from brikteknologier/logginator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-name.js
42 lines (37 loc) · 1.18 KB
/
module-name.js
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
/*
* Tries to guesstimate the name of the module using logginator.
* Intended to be used in the cases where application name is not given.
*/
var fs = require('fs');
var path = require('path');
function moduleId(logginatorModule) {
if (logginatorModule && logginatorModule.parent && logginatorModule.parent.id) {
var id = logginatorModule.parent.id;
var moduleFile = path.basename(id);
var nameMatch = /(.*)\.js$/.exec(moduleFile);
if (nameMatch && nameMatch[1])
return nameMatch[1];
}
}
function packageDef(logginatorModule) {
var ppath = path.dirname(logginatorModule.parent.filename);
while (ppath && ppath !== '/') {
var packFile = path.join(ppath, "package.json");
if (fs.existsSync(packFile)) {
try {
var packageConfig = JSON.parse(fs.readFileSync(packFile));
return packageConfig.name;
} catch(_) { /* ignore */ }
}
ppath = path.dirname(ppath);
}
}
function filename(logginatorModule) {
return path.basename(logginatorModule.parent.filename);
}
module.exports = function (logginatorModule) {
return moduleId(logginatorModule) ||
packageDef(logginatorModule) ||
filename(logginatorModule) ||
"main";
};