Skip to content

Commit 8eaa680

Browse files
alan-agius4dgp1130
authored andcommitted
refactor(@angular/cli): remove most of getWorkspaceRaw usages
This changes removes most of the usage of `getWorkspaceRaw` in the Angular CLI. This is needed to eventually drop the direct dependency on `jsonc-parser`.
1 parent cb6dd19 commit 8eaa680

File tree

5 files changed

+55
-49
lines changed

5 files changed

+55
-49
lines changed

packages/angular/cli/src/analytics/analytics.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { analytics, json, tags } from '@angular-devkit/core';
1010
import debug from 'debug';
1111
import { v4 as uuidV4 } from 'uuid';
1212
import { colors } from '../utilities/color';
13-
import { getWorkspace, getWorkspaceRaw } from '../utilities/config';
13+
import { AngularWorkspace, getWorkspace } from '../utilities/config';
1414
import { analyticsDisabled, analyticsShareDisabled } from '../utilities/environment-options';
1515
import { isTTY } from '../utilities/tty';
1616
import { VERSION } from '../utilities/version';
@@ -64,27 +64,21 @@ export function isPackageNameSafeForAnalytics(name: string): boolean {
6464
* @param global Which config to use. "global" for user-level, and "local" for project-level.
6565
* @param value Either a user ID, true to generate a new User ID, or false to disable analytics.
6666
*/
67-
export function setAnalyticsConfig(global: boolean, value: string | boolean): void {
67+
export async function setAnalyticsConfig(global: boolean, value: string | boolean): Promise<void> {
6868
const level = global ? 'global' : 'local';
6969
analyticsDebug('setting %s level analytics to: %s', level, value);
70-
const [config, configPath] = getWorkspaceRaw(level);
71-
if (!config || !configPath) {
70+
const workspace = await getWorkspace(level);
71+
if (!workspace) {
7272
throw new Error(`Could not find ${level} workspace.`);
7373
}
7474

75-
const cli = config.get(['cli']);
76-
77-
if (cli !== undefined && !json.isJsonObject(cli as json.JsonValue)) {
78-
throw new Error(`Invalid config found at ${configPath}. CLI should be an object.`);
79-
}
80-
81-
if (value === true) {
82-
value = uuidV4();
75+
const cli = (workspace.extensions['cli'] ??= {});
76+
if (!workspace || !json.isJsonObject(cli)) {
77+
throw new Error(`Invalid config found at ${workspace.filePath}. CLI should be an object.`);
8378
}
8479

85-
config.modify(['cli', 'analytics'], value);
86-
config.save();
87-
80+
cli.analytics = value === true ? uuidV4() : value;
81+
await workspace.save();
8882
analyticsDebug('done');
8983
}
9084

@@ -96,8 +90,8 @@ export function setAnalyticsConfig(global: boolean, value: string | boolean): vo
9690
export async function promptAnalytics(global: boolean, force = false): Promise<boolean> {
9791
analyticsDebug('prompting user');
9892
const level = global ? 'global' : 'local';
99-
const [config, configPath] = getWorkspaceRaw(level);
100-
if (!config || !configPath) {
93+
const workspace = await getWorkspace(level);
94+
if (!workspace) {
10195
throw new Error(`Could not find a ${level} workspace. Are you in a project?`);
10296
}
10397

@@ -117,7 +111,7 @@ export async function promptAnalytics(global: boolean, force = false): Promise<b
117111
},
118112
]);
119113

120-
setAnalyticsConfig(global, answers.analytics);
114+
await setAnalyticsConfig(global, answers.analytics);
121115

122116
if (answers.analytics) {
123117
console.log('');
@@ -172,7 +166,7 @@ export async function getAnalytics(
172166
try {
173167
const workspace = await getWorkspace(level);
174168
const analyticsConfig: string | undefined | null | { uid?: string } =
175-
workspace?.getCli()['analytics'];
169+
workspace?.getCli()?.['analytics'];
176170
analyticsDebug('Workspace Analytics config found: %j', analyticsConfig);
177171

178172
if (analyticsConfig === false) {
@@ -218,7 +212,7 @@ export async function getSharedAnalytics(): Promise<AnalyticsCollector | undefin
218212
// If anything happens we just keep the NOOP analytics.
219213
try {
220214
const globalWorkspace = await getWorkspace('global');
221-
const analyticsConfig = globalWorkspace?.getCli()['analyticsSharing'];
215+
const analyticsConfig = globalWorkspace?.getCli()?.['analyticsSharing'];
222216

223217
if (!analyticsConfig || !analyticsConfig.tracking || !analyticsConfig.uuid) {
224218
return undefined;
@@ -294,10 +288,10 @@ function analyticsConfigValueToHumanFormat(value: unknown): 'enabled' | 'disable
294288
}
295289

296290
export async function getAnalyticsInfoString(): Promise<string> {
297-
const [globalWorkspace] = getWorkspaceRaw('global');
298-
const [localWorkspace] = getWorkspaceRaw('local');
299-
const globalSetting = globalWorkspace?.get(['cli', 'analytics']);
300-
const localSetting = localWorkspace?.get(['cli', 'analytics']);
291+
const globalWorkspace = await getWorkspace('global');
292+
const localWorkspace = await getWorkspace('local');
293+
const globalSetting = globalWorkspace?.getCli()?.['analytics'];
294+
const localSetting = localWorkspace?.getCli()?.['analytics'];
301295

302296
const analyticsInstance = await createAnalytics(
303297
!!localWorkspace /** workspace */,

packages/angular/cli/src/commands/analytics/settings/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class AnalyticsDisableModule
5151
describe = 'Disables analytics gathering and reporting for the user.';
5252

5353
async run({ global }: Options<AnalyticsCommandArgs>): Promise<void> {
54-
setAnalyticsConfig(global, false);
54+
await setAnalyticsConfig(global, false);
5555
process.stderr.write(await getAnalyticsInfoString());
5656
}
5757
}
@@ -64,7 +64,7 @@ export class AnalyticsEnableModule
6464
aliases = 'on';
6565
describe = 'Enables analytics gathering and reporting for the user.';
6666
async run({ global }: Options<AnalyticsCommandArgs>): Promise<void> {
67-
setAnalyticsConfig(global, true);
67+
await setAnalyticsConfig(global, true);
6868
process.stderr.write(await getAnalyticsInfoString());
6969
}
7070
}

packages/angular/cli/src/commands/cache/settings/cli.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class CacheDisableModule extends CommandModule implements CommandModuleIm
2525
return localYargs;
2626
}
2727

28-
run(): void {
29-
updateCacheConfig('enabled', false);
28+
run(): Promise<void> {
29+
return updateCacheConfig(this.getWorkspaceOrThrow(), 'enabled', false);
3030
}
3131
}
3232

@@ -41,7 +41,7 @@ export class CacheEnableModule extends CommandModule implements CommandModuleImp
4141
return localYargs;
4242
}
4343

44-
run(): void {
45-
updateCacheConfig('enabled', true);
44+
run(): Promise<void> {
45+
return updateCacheConfig(this.getWorkspaceOrThrow(), 'enabled', true);
4646
}
4747
}

packages/angular/cli/src/commands/cache/utilities.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
import { isJsonObject } from '@angular-devkit/core';
1010
import { resolve } from 'path';
1111
import { Cache, Environment } from '../../../lib/config/workspace-schema';
12-
import { AngularWorkspace, getWorkspaceRaw } from '../../utilities/config';
13-
14-
export function updateCacheConfig<K extends keyof Cache>(key: K, value: Cache[K]): void {
15-
const [localWorkspace] = getWorkspaceRaw('local');
16-
if (!localWorkspace) {
17-
throw new Error('Cannot find workspace configuration file.');
18-
}
19-
20-
localWorkspace.modify(['cli', 'cache', key], value);
21-
localWorkspace.save();
12+
import { AngularWorkspace } from '../../utilities/config';
13+
14+
export function updateCacheConfig<K extends keyof Cache>(
15+
workspace: AngularWorkspace,
16+
key: K,
17+
value: Cache[K],
18+
): Promise<void> {
19+
const cli = (workspace.extensions['cli'] ??= {}) as Record<string, Record<string, unknown>>;
20+
const cache = (cli['cache'] ??= {});
21+
cache[key] = value;
22+
23+
return workspace.save();
2224
}
2325

2426
export function getCacheConfig(workspace: AngularWorkspace | undefined): Required<Cache> {

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ function globalFilePath(): string | null {
117117
export class AngularWorkspace {
118118
readonly basePath: string;
119119

120-
constructor(private workspace: workspaces.WorkspaceDefinition, readonly filePath: string) {
120+
constructor(
121+
private readonly workspace: workspaces.WorkspaceDefinition,
122+
readonly filePath: string,
123+
) {
121124
this.basePath = path.dirname(filePath);
122125
}
123126

@@ -132,15 +135,19 @@ export class AngularWorkspace {
132135
// Temporary helper functions to support refactoring
133136

134137
// eslint-disable-next-line @typescript-eslint/no-explicit-any
135-
getCli(): Record<string, any> {
136-
return (this.workspace.extensions['cli'] as Record<string, unknown>) || {};
138+
getCli(): Record<string, any> | undefined {
139+
return this.workspace.extensions['cli'] as Record<string, unknown>;
137140
}
138141

139142
// eslint-disable-next-line @typescript-eslint/no-explicit-any
140-
getProjectCli(projectName: string): Record<string, any> {
143+
getProjectCli(projectName: string): Record<string, any> | undefined {
141144
const project = this.workspace.projects.get(projectName);
142145

143-
return (project?.extensions['cli'] as Record<string, unknown>) || {};
146+
return project?.extensions['cli'] as Record<string, unknown>;
147+
}
148+
149+
save(): Promise<void> {
150+
return workspaces.writeWorkspace(this.workspace, createWorkspaceHost(), this.filePath);
144151
}
145152

146153
static async load(workspaceFilePath: string): Promise<AngularWorkspace> {
@@ -162,12 +169,15 @@ export async function getWorkspace(
162169
return cachedWorkspaces.get(level);
163170
}
164171

165-
const configPath = level === 'local' ? projectFilePath() : globalFilePath();
166-
172+
let configPath = level === 'local' ? projectFilePath() : globalFilePath();
167173
if (!configPath) {
168-
cachedWorkspaces.set(level, undefined);
174+
if (level === 'local') {
175+
cachedWorkspaces.set(level, undefined);
176+
177+
return undefined;
178+
}
169179

170-
return undefined;
180+
configPath = createGlobalSettings();
171181
}
172182

173183
try {

0 commit comments

Comments
 (0)