forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentConfiguration.ts
More file actions
105 lines (90 loc) · 3.56 KB
/
EnvironmentConfiguration.ts
File metadata and controls
105 lines (90 loc) · 3.56 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
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as os from 'os';
/**
* Names of environment variables used by Rush.
* @public
*/
export const enum EnvironmentVariableNames {
/**
* This variable overrides the temporary folder used by Rush.
* The default value is "common/temp" under the repository root.
*/
RUSH_TEMP_FOLDER = 'RUSH_TEMP_FOLDER',
/**
* This variable overrides the version of Rush that will be installed by
* the version selector. The default value is determined by the "rushVersion"
* field from rush.json.
*/
RUSH_PREVIEW_VERSION = 'RUSH_PREVIEW_VERSION',
/**
* This variable selects a specific installation variant for Rush to use when installing
* and linking package dependencies. For more information, see this article:
* https://rushjs.io/pages/advanced/installation_variants/
*/
RUSH_VARIANT = 'RUSH_VARIANT'
}
/**
* Provides Rush-specific environment variable data. All Rush environment variables must start with "RUSH_". This class
* is designed to be used by RushConfiguration.
*
* @remarks
* Initialize will throw if any unknown parameters are present.
*/
export class EnvironmentConfiguration {
private static _hasBeenInitialized: boolean = false;
private static _rushTempFolderOverride: string | undefined;
/**
* An override for the common/temp folder path.
*/
public static get rushTempFolderOverride(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
return EnvironmentConfiguration._rushTempFolderOverride;
}
/**
* Reads and validates environment variables. If any are invalid, this function will throw.
*/
public static initialize(): void {
EnvironmentConfiguration.reset();
const unknownEnvVariables: string[] = [];
for (const envVarName in process.env) {
if (process.env.hasOwnProperty(envVarName) && envVarName.match(/^RUSH_/i)) {
const value: string | undefined = process.env[envVarName];
// Environment variables are only case-insensitive on Windows
const normalizedEnvVarName: string = os.platform() === 'win32' ? envVarName.toUpperCase() : envVarName;
switch (normalizedEnvVarName) {
case EnvironmentVariableNames.RUSH_TEMP_FOLDER:
EnvironmentConfiguration._rushTempFolderOverride = value;
break;
case EnvironmentVariableNames.RUSH_PREVIEW_VERSION:
case EnvironmentVariableNames.RUSH_VARIANT:
// Handled by @microsoft/rush front end
break;
default:
unknownEnvVariables.push(envVarName);
break;
}
}
}
// This strictness intends to catch mistakes where variables are misspelled or not used correctly.
if (unknownEnvVariables.length > 0) {
throw new Error(
'The following environment variables were found with the "RUSH_" prefix, but they are not ' +
`recognized by this version of Rush: ${unknownEnvVariables.join(', ')}`
);
}
EnvironmentConfiguration._hasBeenInitialized = true;
}
/**
* Resets EnvironmentConfiguration into an un-initialized state.
*/
public static reset(): void {
EnvironmentConfiguration._rushTempFolderOverride = undefined;
EnvironmentConfiguration._hasBeenInitialized = false;
}
private static _ensureInitialized(): void {
if (!EnvironmentConfiguration._hasBeenInitialized) {
throw new Error('The EnvironmentConfiguration must be initialized before values can be accessed.');
}
}
}