Skip to content

Commit b6b2578

Browse files
committed
refactor(@angular/cli): centralize environment variable parsing
Refactors the environment options utility to improve maintainability and clarity. This change introduces a centralized `parseTristate` function to handle all boolean-like parsing from environment variables, removing the previous `isEnabled`, `isDisabled`, and `optional` helpers.
1 parent c8b62f1 commit b6b2578

File tree

2 files changed

+60
-26
lines changed

2 files changed

+60
-26
lines changed

packages/angular/cli/src/utilities/environment-options.ts

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,63 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

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']);
1211

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']);
1614

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 !== '';
1922
}
2023

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 {
2231
if (!isPresent(variable)) {
2332
return undefined;
2433
}
2534

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;
2744
}
2845

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']);

packages/angular/cli/src/utilities/tty.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
function _isTruthy(value: undefined | string): boolean {
10-
// Returns true if value is a string that is anything but 0 or false.
11-
return value !== undefined && value !== '0' && value.toUpperCase() !== 'FALSE';
12-
}
9+
import { forceTty, isCI } from './environment-options';
1310

11+
/**
12+
* Determines if the `stream` is a TTY.
13+
*
14+
* @param stream A NodeJS stream to check. Defaults to `process.stdout`.
15+
* @returns `true` if the `stream` is a TTY, `false` otherwise. This detection is overridden
16+
* by the `NG_FORCE_TTY` environment variable. In a CI environment, this will also be `false`
17+
* unless `NG_FORCE_TTY` is set.
18+
*/
1419
export function isTTY(stream: NodeJS.WriteStream = process.stdout): boolean {
15-
// If we force TTY, we always return true.
16-
const force = process.env['NG_FORCE_TTY'];
17-
if (force !== undefined) {
18-
return _isTruthy(force);
19-
}
20-
21-
return !!stream.isTTY && !_isTruthy(process.env['CI']);
20+
return forceTty ?? (!!stream.isTTY && !isCI);
2221
}

0 commit comments

Comments
 (0)