mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-22 23:15:56 +08:00
refactor(@angular/cli): remove hasAnalyticsConfig
analytics logic
With this change we clean up further the analytics code and re-use the `getAnalytics` to determine if the config is set or not. Also, this change inclused a refactor to the `createAnalytics` method to make it more readable.
This commit is contained in:
parent
1a36fd94a8
commit
a497d12a6b
@ -17,7 +17,7 @@ try {
|
|||||||
var analytics = require('../../src/analytics/analytics');
|
var analytics = require('../../src/analytics/analytics');
|
||||||
|
|
||||||
analytics
|
analytics
|
||||||
.hasAnalyticsConfig('global')
|
.getAnalytics('global')
|
||||||
.then((hasGlobalConfig) => {
|
.then((hasGlobalConfig) => {
|
||||||
if (!hasGlobalConfig) {
|
if (!hasGlobalConfig) {
|
||||||
return analytics.promptAnalytics(true /** global */);
|
return analytics.promptAnalytics(true /** global */);
|
||||||
|
@ -152,16 +152,21 @@ export async function promptAnalytics(global: boolean, force = false): Promise<b
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the analytics object for the user.
|
* Get the analytics object for the user.
|
||||||
|
*
|
||||||
|
* @returns
|
||||||
|
* - `AnalyticsCollector` when enabled.
|
||||||
|
* - `analytics.NoopAnalytics` when disabled.
|
||||||
|
* - `undefined` when not configured.
|
||||||
*/
|
*/
|
||||||
export async function getAnalytics(
|
export async function getAnalytics(
|
||||||
level: 'local' | 'global',
|
level: 'local' | 'global',
|
||||||
): Promise<AnalyticsCollector | undefined> {
|
): Promise<AnalyticsCollector | analytics.NoopAnalytics | undefined> {
|
||||||
analyticsDebug('getAnalytics');
|
analyticsDebug('getAnalytics');
|
||||||
|
|
||||||
if (analyticsDisabled) {
|
if (analyticsDisabled) {
|
||||||
analyticsDebug('NG_CLI_ANALYTICS is false');
|
analyticsDebug('NG_CLI_ANALYTICS is false');
|
||||||
|
|
||||||
return undefined;
|
return new analytics.NoopAnalytics();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -170,7 +175,9 @@ export async function getAnalytics(
|
|||||||
workspace?.getCli()['analytics'];
|
workspace?.getCli()['analytics'];
|
||||||
analyticsDebug('Workspace Analytics config found: %j', analyticsConfig);
|
analyticsDebug('Workspace Analytics config found: %j', analyticsConfig);
|
||||||
|
|
||||||
if (!analyticsConfig) {
|
if (analyticsConfig === false) {
|
||||||
|
return new analytics.NoopAnalytics();
|
||||||
|
} else if (analyticsConfig === undefined || analyticsConfig === null) {
|
||||||
return undefined;
|
return undefined;
|
||||||
} else {
|
} else {
|
||||||
let uid: string | undefined = undefined;
|
let uid: string | undefined = undefined;
|
||||||
@ -231,33 +238,49 @@ export async function createAnalytics(
|
|||||||
workspace: boolean,
|
workspace: boolean,
|
||||||
skipPrompt = false,
|
skipPrompt = false,
|
||||||
): Promise<analytics.Analytics> {
|
): Promise<analytics.Analytics> {
|
||||||
let config: analytics.Analytics | undefined;
|
// Global config takes precedence over local config only for the disabled check.
|
||||||
const isDisabledGlobally = (await getWorkspace('global'))?.getCli()['analytics'] === false;
|
// IE:
|
||||||
// If in workspace and global analytics is enabled, defer to workspace level
|
// global: disabled & local: enabled = disabled
|
||||||
if (workspace && !isDisabledGlobally) {
|
// global: id: 123 & local: id: 456 = 456
|
||||||
const skipAnalytics = skipPrompt || analyticsDisabled;
|
|
||||||
|
// check global
|
||||||
|
const globalConfig = await getAnalytics('global');
|
||||||
|
if (globalConfig instanceof analytics.NoopAnalytics) {
|
||||||
|
return globalConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = globalConfig;
|
||||||
|
// Not disabled globally, check locally.
|
||||||
|
if (workspace) {
|
||||||
|
let localConfig = await getAnalytics('local');
|
||||||
|
if (localConfig === undefined) {
|
||||||
|
if (!skipPrompt) {
|
||||||
|
// local is not unset, prompt user.
|
||||||
|
|
||||||
// TODO: This should honor the `no-interactive` option.
|
// TODO: This should honor the `no-interactive` option.
|
||||||
// It is currently not an `ng` option but rather only an option for specific commands.
|
// It is currently not an `ng` option but rather only an option for specific commands.
|
||||||
// The concept of `ng`-wide options are needed to cleanly handle this.
|
// The concept of `ng`-wide options are needed to cleanly handle this.
|
||||||
if (!skipAnalytics && !(await hasAnalyticsConfig('local'))) {
|
|
||||||
await promptAnalytics(false);
|
await promptAnalytics(false);
|
||||||
|
localConfig = await getAnalytics('local');
|
||||||
}
|
}
|
||||||
config = await getAnalytics('local');
|
|
||||||
} else {
|
|
||||||
config = await getAnalytics('global');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (localConfig instanceof analytics.NoopAnalytics) {
|
||||||
|
return localConfig;
|
||||||
|
} else if (localConfig) {
|
||||||
|
// Favor local settings over global when defined.
|
||||||
|
config = localConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get shared analytics
|
||||||
|
// TODO: evalute if this should be completly removed.
|
||||||
const maybeSharedAnalytics = await getSharedAnalytics();
|
const maybeSharedAnalytics = await getSharedAnalytics();
|
||||||
|
|
||||||
if (config && maybeSharedAnalytics) {
|
if (config && maybeSharedAnalytics) {
|
||||||
return new analytics.MultiAnalytics([config, maybeSharedAnalytics]);
|
return new analytics.MultiAnalytics([config, maybeSharedAnalytics]);
|
||||||
} else if (config) {
|
|
||||||
return config;
|
|
||||||
} else if (maybeSharedAnalytics) {
|
|
||||||
return maybeSharedAnalytics;
|
|
||||||
} else {
|
|
||||||
return new analytics.NoopAnalytics();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return config ?? maybeSharedAnalytics ?? new analytics.NoopAnalytics();
|
||||||
}
|
}
|
||||||
|
|
||||||
function analyticsConfigValueToHumanFormat(value: unknown): 'enabled' | 'disabled' | 'not set' {
|
function analyticsConfigValueToHumanFormat(value: unknown): 'enabled' | 'disabled' | 'not set' {
|
||||||
@ -295,14 +318,3 @@ export async function getAnalyticsInfoString(): Promise<string> {
|
|||||||
` + '\n'
|
` + '\n'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function hasAnalyticsConfig(level: 'local' | 'global'): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const workspace = await getWorkspace(level);
|
|
||||||
if (workspace?.getCli()['analytics'] !== undefined) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user