-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathenvironment.js
More file actions
92 lines (85 loc) · 3.07 KB
/
Copy pathenvironment.js
File metadata and controls
92 lines (85 loc) · 3.07 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
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
/**
* Compares environment variable names using the child platform's semantics.
*
* @param {string} left first variable name
* @param {string} right second variable name
* @param {string} [platform] target platform
* @returns {boolean} whether the names are equivalent
*/
function environmentNamesEqual (left, right, platform = process.platform) {
return platform === 'win32' ? left.toUpperCase() === right.toUpperCase() : left === right
}
/**
* Finds an environment entry without inventing a second spelling on Windows.
*
* @param {Record<string, string|undefined>} environment environment object
* @param {string} name variable name
* @param {string} [platform] target platform
* @returns {[string, string|undefined]|undefined} matching entry
*/
function findEnvironmentEntry (environment, name, platform = process.platform) {
return Object.entries(environment || {}).find(([candidate]) => {
return environmentNamesEqual(candidate, name, platform)
})
}
/**
* Reads an environment value using platform name semantics.
*
* @param {Record<string, string|undefined>} environment environment object
* @param {string} name variable name
* @param {string} [platform] target platform
* @returns {string|undefined} environment value
*/
function getEnvironmentValue (environment, name, platform = process.platform) {
return findEnvironmentEntry(environment, name, platform)?.[1]
}
/**
* Replaces every equivalent spelling with one canonical entry.
*
* @param {Record<string, string|undefined>} environment environment object
* @param {string} name canonical variable name
* @param {string|undefined} value environment value
* @param {string} [platform] target platform
* @returns {void}
*/
function setEnvironmentValue (environment, name, value, platform = process.platform) {
for (const candidate of Object.keys(environment)) {
if (environmentNamesEqual(candidate, name, platform)) delete environment[candidate]
}
environment[name] = value
}
/**
* Applies environment entries with Windows-compatible replacement semantics.
*
* @param {Record<string, string|undefined>} target destination environment
* @param {Record<string, string|undefined>} source overriding entries
* @param {string} [platform] target platform
* @returns {void}
*/
function mergeEnvironment (target, source, platform = process.platform) {
if (source) {
for (const [name, value] of Object.entries(source)) {
setEnvironmentValue(target, name, value, platform)
}
}
}
/**
* Reports whether a name belongs to the private or public Datadog environment namespace.
*
* @param {string} name variable name
* @param {string} [platform] target platform
* @returns {boolean} whether this is a Datadog variable
*/
function isDatadogEnvironmentName (name, platform = process.platform) {
const normalized = platform === 'win32' ? name.toUpperCase() : name
return normalized.startsWith('DD_') || normalized.startsWith('_DD_')
}
module.exports = {
environmentNamesEqual,
findEnvironmentEntry,
getEnvironmentValue,
isDatadogEnvironmentName,
mergeEnvironment,
setEnvironmentValue,
}