-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']); |
There was a problem hiding this comment.
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 */)
?There was a problem hiding this comment.
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.