|
6 | 6 | * found in the LICENSE file at https://angular.dev/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -function isPresent(variable: string | undefined): variable is string { |
10 |
| - return typeof variable === 'string' && variable !== ''; |
11 |
| -} |
| 9 | +/** A set of strings that are considered "truthy" when parsing environment variables. */ |
| 10 | +const TRUTHY_VALUES = new Set(['1', 'true']); |
12 | 11 |
|
13 |
| -function isDisabled(variable: string | undefined): boolean { |
14 |
| - return isPresent(variable) && (variable === '0' || variable.toLowerCase() === 'false'); |
15 |
| -} |
| 12 | +/** A set of strings that are considered "falsy" when parsing environment variables. */ |
| 13 | +const FALSY_VALUES = new Set(['0', 'false']); |
16 | 14 |
|
17 |
| -function isEnabled(variable: string | undefined): boolean { |
18 |
| - return isPresent(variable) && (variable === '1' || variable.toLowerCase() === 'true'); |
| 15 | +/** |
| 16 | + * Checks if an environment variable is present and has a non-empty value. |
| 17 | + * @param variable The environment variable to check. |
| 18 | + * @returns `true` if the variable is a non-empty string. |
| 19 | + */ |
| 20 | +function isPresent(variable: string | undefined): variable is string { |
| 21 | + return typeof variable === 'string' && variable !== ''; |
19 | 22 | }
|
20 | 23 |
|
21 |
| -function optional(variable: string | undefined): boolean | undefined { |
| 24 | +/** |
| 25 | + * Parses an environment variable into a boolean or undefined. |
| 26 | + * @returns `true` if the variable is truthy ('1', 'true'). |
| 27 | + * @returns `false` if the variable is falsy ('0', 'false'). |
| 28 | + * @returns `undefined` if the variable is not present or has an unknown value. |
| 29 | + */ |
| 30 | +function parseTristate(variable: string | undefined): boolean | undefined { |
22 | 31 | if (!isPresent(variable)) {
|
23 | 32 | return undefined;
|
24 | 33 | }
|
25 | 34 |
|
26 |
| - return isEnabled(variable); |
| 35 | + const value = variable.toLowerCase(); |
| 36 | + if (TRUTHY_VALUES.has(value)) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + if (FALSY_VALUES.has(value)) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + return undefined; |
27 | 44 | }
|
28 | 45 |
|
29 |
| -export const analyticsDisabled = isDisabled(process.env['NG_CLI_ANALYTICS']); |
30 |
| -export const isCI = isEnabled(process.env['CI']); |
31 |
| -export const disableVersionCheck = isEnabled(process.env['NG_DISABLE_VERSION_CHECK']); |
32 |
| -export const ngDebug = isEnabled(process.env['NG_DEBUG']); |
33 |
| -export const forceAutocomplete = optional(process.env['NG_FORCE_AUTOCOMPLETE']); |
| 46 | +/** Disables all analytics reporting when the `NG_CLI_ANALYTICS` environment variable is set to '0' or 'false'. */ |
| 47 | +export const analyticsDisabled = parseTristate(process.env['NG_CLI_ANALYTICS']) === false; |
| 48 | + |
| 49 | +/** Identifies when the CLI is running in a Continuous Integration environment. */ |
| 50 | +export const isCI = parseTristate(process.env['CI']) === true; |
| 51 | + |
| 52 | +/** Disables the automatic version check when the `NG_DISABLE_VERSION_CHECK` environment variable is enabled. */ |
| 53 | +export const disableVersionCheck = parseTristate(process.env['NG_DISABLE_VERSION_CHECK']) === true; |
| 54 | + |
| 55 | +/** Enables debugging messages when the `NG_DEBUG` environment variable is enabled. */ |
| 56 | +export const ngDebug = parseTristate(process.env['NG_DEBUG']) === true; |
| 57 | + |
| 58 | +/** |
| 59 | + * Forces the autocomplete script to be generated. |
| 60 | + * The `NG_FORCE_AUTOCOMPLETE` environment variable can be 'true', 'false', or undefined (for default behavior). |
| 61 | + */ |
| 62 | +export const forceAutocomplete = parseTristate(process.env['NG_FORCE_AUTOCOMPLETE']); |
| 63 | + |
| 64 | +/** |
| 65 | + * When enabled, forces TTY mode. |
| 66 | + * The `NG_FORCE_TTY` environment variable can be 'true', 'false', or undefined (for default behavior). |
| 67 | + */ |
| 68 | +export const forceTty = parseTristate(process.env['NG_FORCE_TTY']); |
0 commit comments