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
206 lines (179 loc) · 7.79 KB
/
EnvironmentConfiguration.ts
File metadata and controls
206 lines (179 loc) · 7.79 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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';
import * as path from 'path';
import { trueCasePathSync } from 'true-case-path';
export interface IEnvironmentConfigurationInitializeOptions {
doNotNormalizePaths?: boolean;
}
/**
* 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',
/**
* If this variable is set to "true", Rush will not fail the build when running a version
* of Node that does not match the criteria specified in the "nodeSupportedVersionRange"
* field from rush.json.
*/
RUSH_ALLOW_UNSUPPORTED_NODEJS = 'RUSH_ALLOW_UNSUPPORTED_NODEJS',
/**
* 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',
/**
* If this variable is set to "true", Rush will create symlinks with absolute paths instead
* of relative paths. This can be necessary when a repository is moved during a build or
* if parts of a repository are moved into a sandbox.
*/
RUSH_ABSOLUTE_SYMLINKS = 'RUSH_ABSOLUTE_SYMLINKS'
}
/**
* 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;
private static _absoluteSymlinks: boolean = false;
private static _allowUnsupportedNodeVersion: boolean = false;
/**
* An override for the common/temp folder path.
*/
public static get rushTempFolderOverride(): string | undefined {
EnvironmentConfiguration._ensureInitialized();
return EnvironmentConfiguration._rushTempFolderOverride;
}
/**
* If "true", create symlinks with absolute paths instead of relative paths.
* See {@link EnvironmentVariableNames.RUSH_ABSOLUTE_SYMLINKS}
*/
public static get absoluteSymlinks(): boolean {
EnvironmentConfiguration._ensureInitialized();
return EnvironmentConfiguration._absoluteSymlinks;
}
/**
* If this environment variable is set to "true", the Node.js version check will print a warning
* instead of causing a hard error if the environment's Node.js version doesn't match the
* version specifier in `rush.json`'s "nodeSupportedVersionRange" property.
*
* See {@link EnvironmentVariableNames.RUSH_ALLOW_UNSUPPORTED_NODEJS}.
*/
public static get allowUnsupportedNodeVersion(): boolean {
EnvironmentConfiguration._ensureInitialized();
return EnvironmentConfiguration._allowUnsupportedNodeVersion;
}
/**
* Reads and validates environment variables. If any are invalid, this function will throw.
*/
public static initialize(options: IEnvironmentConfigurationInitializeOptions = {}): 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 && !options.doNotNormalizePaths)
? EnvironmentConfiguration._normalizeDeepestParentFolderPath(value) || value
: value;
break;
}
case EnvironmentVariableNames.RUSH_ABSOLUTE_SYMLINKS: {
EnvironmentConfiguration._absoluteSymlinks = value === 'true';
break;
}
case EnvironmentVariableNames.RUSH_ALLOW_UNSUPPORTED_NODEJS: {
EnvironmentConfiguration._allowUnsupportedNodeVersion = value === 'true';
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.');
}
}
/**
* Given a path to a folder (that may or may not exist), normalize the path, including casing,
* to the first existing parent folder in the path.
*
* If no existing path can be found (for example, if the root is a volume that doesn't exist),
* this function returns undefined.
*
* @example
* If the following path exists on disk: C:\Folder1\folder2\
* _normalizeFirstExistingFolderPath('c:\\folder1\\folder2\\temp\\subfolder')
* returns 'C:\\Folder1\\folder2\\temp\\subfolder'
*/
private static _normalizeDeepestParentFolderPath(folderPath: string): string | undefined {
folderPath = path.normalize(folderPath);
const endsWithSlash: boolean = folderPath.charAt(folderPath.length - 1) === path.sep;
const parsedPath: path.ParsedPath = path.parse(folderPath);
const pathRoot: string = parsedPath.root;
const pathWithoutRoot: String = parsedPath.dir.substr(pathRoot.length);
const pathParts: string[] = [...pathWithoutRoot.split(path.sep), parsedPath.name].filter((part) => !!part);
// Starting with all path sections, and eliminating one from the end during each loop iteration,
// run trueCasePathSync. If trueCasePathSync returns without exception, we've found a subset
// of the path that exists and we've now gotten the correct casing.
//
// Once we've found a parent folder that exists, append the path sections that didn't exist.
for (let i: number = pathParts.length; i >= 0; i--) {
const constructedPath: string = path.join(pathRoot, ...pathParts.slice(0, i));
try {
const normalizedConstructedPath: string = trueCasePathSync(constructedPath);
const result: string = path.join(normalizedConstructedPath, ...pathParts.slice(i));
if (endsWithSlash) {
return `${result}${path.sep}`;
} else {
return result;
}
} catch (e) {
// This path doesn't exist, continue to the next subpath
}
}
return undefined;
}
}