-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-variable.js
More file actions
67 lines (58 loc) · 2.36 KB
/
read-variable.js
File metadata and controls
67 lines (58 loc) · 2.36 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
const os = require('os')
console.log(`================================================================================`)
console.log(`Platform: ${os.platform()}-${os.arch()}`)
console.log(`================================================================================`)
console.log(`variables matching our key`)
console.log(`--------------------------------------------------------------------------------`)
logMatchingVariableKeys('MY_CUSTOM_VARIABLE')
console.log(`--------------------------------------------------------------------------------`)
const getEnvLower = getEnv('my_custom_variable')
const getEnvUpper = getEnv('MY_CUSTOM_VARIABLE')
let structuredTableData = [
{
variable: 'MY_CUSTOM_VARIABLE',
lowercase: process.env.my_custom_variable,
uppercase: process.env.MY_CUSTOM_VARIABLE,
getEnvLower: getEnvLower,
getEnvUpper: getEnvUpper
},
{
variable: 'npm_config_MY_CUSTOM_VARIABLE',
lowercase: process.env.npm_config_my_custom_variable,
uppercase: process.env.npm_config_MY_CUSTOM_VARIABLE,
getEnvLower: getEnvLower,
getEnvUpper: getEnvUpper
},
{
variable: 'npm_package_config_MY_CUSTOM_VARIABLE',
lowercase: process.env.npm_package_config_my_custom_variable,
uppercase: process.env.npm_package_config_MY_CUSTOM_VARIABLE,
getEnvLower: getEnvLower,
getEnvUpper: getEnvUpper
}
]
console.table(structuredTableData)
function logMatchingVariableKeys(keyToMatch) {
let env = process.env
for (let key in env) {
if (key.endsWith(keyToMatch.toUpperCase()) || key.endsWith(keyToMatch.toLowerCase())) {
console.log(`${key}=${env[key]}`)
}
}
}
function getEnv(varName) {
const configVarName = `npm_config_${varName}`
const packageConfigVarName = `npm_package_config_${varName}`
let result
if (process.env.hasOwnProperty(varName)) {
console.log(`Using ${varName} from environment variable`)
result = process.env[varName]
} else if (process.env.hasOwnProperty(configVarName)) {
console.log(`Using ${varName} from npm config`)
result = process.env[configVarName]
} else if (process.env.hasOwnProperty(packageConfigVarName)) {
console.log(`Using ${varName} from package.json config`)
result = process.env[packageConfigVarName]
}
return result
}