/
configuration.ts
142 lines (120 loc) · 3.76 KB
/
configuration.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Debug from 'debug'
const debug = Debug('axm:configuration')
import { ServiceManager } from './serviceManager'
import Autocast from './utils/autocast'
import * as path from 'path'
import * as fs from 'fs'
import * as util from 'util'
export default class Configuration {
static configureModule (opts) {
if (ServiceManager.get('transport')) ServiceManager.get('transport').setOptions(opts)
}
static findPackageJson () {
try {
require.main = Configuration.getMain()
} catch (_e) {
// Ignore error when getter is set on require.main, but no setter
}
if (!require.main) {
return
}
if (!require.main.paths) {
return
}
let pkgPath = path.resolve(path.dirname(require.main.filename), 'package.json')
try {
fs.statSync(pkgPath)
} catch (e) {
try {
pkgPath = path.resolve(path.dirname(require.main.filename), '..', 'package.json')
fs.statSync(pkgPath)
} catch (e) {
debug('Cannot find package.json')
try {
pkgPath = path.resolve(path.dirname(require.main.filename), '..', '..', 'package.json')
fs.statSync(pkgPath)
} catch (e) {
debug('Cannot find package.json')
return null
}
}
return pkgPath
}
return pkgPath
}
static init (conf, doNotTellPm2?) {
const packageFilepath = Configuration.findPackageJson()
let packageJson
if (!conf.module_conf) {
conf.module_conf = {}
}
conf.apm = {
type: 'node',
version: null
}
try {
const prefix = __dirname.replace(/\\/g,'/').indexOf('/build/') >= 0 ? '../../' : '../'
const pkg = require(prefix + 'package.json')
conf.apm.version = pkg.version || null
} catch (err) {
debug('Failed to fetch current apm version: ', err.message)
}
if (conf.isModule === true) {
/**
* Merge package.json metadata
*/
try {
packageJson = require(packageFilepath || '')
conf.module_version = packageJson.version
conf.module_name = packageJson.name
conf.description = packageJson.description
if (packageJson.config) {
conf = util['_extend'](conf, packageJson.config)
conf.module_conf = packageJson.config
}
} catch (e) {
throw new Error(e)
}
} else {
conf.module_name = process.env.name || 'outside-pm2'
try {
packageJson = require(packageFilepath || '')
conf.module_version = packageJson.version
if (packageJson.config) {
conf = util['_extend'](conf, packageJson.config)
conf.module_conf = packageJson.config
}
} catch (e) {
debug(e.message)
}
}
/**
* If custom variables has been set, merge with returned configuration
*/
try {
if (process.env[conf.module_name]) {
const castedConf = new Autocast().autocast(JSON.parse(process.env[conf.module_name] || ''))
conf = util['_extend'](conf, castedConf)
// Do not display probe configuration in Keymetrics
delete castedConf.probes
// This is the configuration variable modifiable from keymetrics
conf.module_conf = JSON.parse(JSON.stringify(util['_extend'](conf.module_conf, castedConf)))
// Obfuscate passwords
Object.keys(conf.module_conf).forEach(function (key) {
if ((key === 'password' || key === 'passwd') &&
conf.module_conf[key].length >= 1) {
conf.module_conf[key] = 'Password hidden'
}
})
}
} catch (e) {
debug(e)
}
if (doNotTellPm2 === true) return conf
Configuration.configureModule(conf)
return conf
}
static getMain (): any {
return require.main || { filename: './somefile.js' }
}
}