Skip to content

refactor(@angular/cli): centralize environment variable parsing #30825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions packages/angular/cli/src/utilities/environment-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,63 @@
* found in the LICENSE file at https://angular.dev/license
*/

function isPresent(variable: string | undefined): variable is string {
return typeof variable === 'string' && variable !== '';
}
/** A set of strings that are considered "truthy" when parsing environment variables. */
const TRUTHY_VALUES = new Set(['1', 'true']);

function isDisabled(variable: string | undefined): boolean {
return isPresent(variable) && (variable === '0' || variable.toLowerCase() === 'false');
}
/** A set of strings that are considered "falsy" when parsing environment variables. */
const FALSY_VALUES = new Set(['0', 'false']);

function isEnabled(variable: string | undefined): boolean {
return isPresent(variable) && (variable === '1' || variable.toLowerCase() === 'true');
/**
* Checks if an environment variable is present and has a non-empty value.
* @param variable The environment variable to check.
* @returns `true` if the variable is a non-empty string.
*/
function isPresent(variable: string | undefined): variable is string {
return typeof variable === 'string' && variable !== '';
}

function optional(variable: string | undefined): boolean | undefined {
/**
* Parses an environment variable into a boolean or undefined.
* @returns `true` if the variable is truthy ('1', 'true').
* @returns `false` if the variable is falsy ('0', 'false').
* @returns `undefined` if the variable is not present or has an unknown value.
*/
function parseTristate(variable: string | undefined): boolean | undefined {
if (!isPresent(variable)) {
return undefined;
}

return isEnabled(variable);
const value = variable.toLowerCase();
if (TRUTHY_VALUES.has(value)) {
return true;
}
if (FALSY_VALUES.has(value)) {
return false;
}

return undefined;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider: Could we remove the "tristate" concept by just providing a default value like parseEnvBoolean(process.env['FOO'], true /* default */)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of them don't have constant defaults. The third "not present" state is relevant downstream for them.


export const analyticsDisabled = isDisabled(process.env['NG_CLI_ANALYTICS']);
export const isCI = isEnabled(process.env['CI']);
export const disableVersionCheck = isEnabled(process.env['NG_DISABLE_VERSION_CHECK']);
export const ngDebug = isEnabled(process.env['NG_DEBUG']);
export const forceAutocomplete = optional(process.env['NG_FORCE_AUTOCOMPLETE']);
/** Disables all analytics reporting when the `NG_CLI_ANALYTICS` environment variable is set to '0' or 'false'. */
export const analyticsDisabled = parseTristate(process.env['NG_CLI_ANALYTICS']) === false;
Copy link
Collaborator

@dgp1130 dgp1130 Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider: Would it make more sense to frame these in the positive and then fall back to a default value with ?? rather than check equality with a specific boolean? I feel analyticsEnabled = parseTristate(...) ?? true would be more intuitive to me at least.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is more of "force analytics disabled" and analytics being enabled involves further logic. There is potential to further refactor the usages and adjust the naming but i wanted to avoid usage changes where possible here.


/** Identifies when the CLI is running in a Continuous Integration environment. */
export const isCI = parseTristate(process.env['CI']) === true;

/** Disables the automatic version check when the `NG_DISABLE_VERSION_CHECK` environment variable is enabled. */
export const disableVersionCheck = parseTristate(process.env['NG_DISABLE_VERSION_CHECK']) === true;

/** Enables debugging messages when the `NG_DEBUG` environment variable is enabled. */
export const ngDebug = parseTristate(process.env['NG_DEBUG']) === true;

/**
* Forces the autocomplete script to be generated.
* The `NG_FORCE_AUTOCOMPLETE` environment variable can be 'true', 'false', or undefined (for default behavior).
*/
export const forceAutocomplete = parseTristate(process.env['NG_FORCE_AUTOCOMPLETE']);

/**
* When enabled, forces TTY mode.
* The `NG_FORCE_TTY` environment variable can be 'true', 'false', or undefined (for default behavior).
*/
export const forceTty = parseTristate(process.env['NG_FORCE_TTY']);
21 changes: 10 additions & 11 deletions packages/angular/cli/src/utilities/tty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
* found in the LICENSE file at https://angular.dev/license
*/

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

/**
* Determines if the `stream` is a TTY.
*
* @param stream A NodeJS stream to check. Defaults to `process.stdout`.
* @returns `true` if the `stream` is a TTY, `false` otherwise. This detection is overridden
* by the `NG_FORCE_TTY` environment variable. In a CI environment, this will also be `false`
* unless `NG_FORCE_TTY` is set.
*/
export function isTTY(stream: NodeJS.WriteStream = process.stdout): boolean {
// If we force TTY, we always return true.
const force = process.env['NG_FORCE_TTY'];
if (force !== undefined) {
return _isTruthy(force);
}

return !!stream.isTTY && !_isTruthy(process.env['CI']);
return forceTty ?? (!!stream.isTTY && !isCI);
}