Skip to content

Commit 1d3a4e7

Browse files
alan-agius4vikerman
authored andcommitted
refactor(@angular/cli): use isJsonObject helper in to read config
1 parent a342189 commit 1d3a4e7

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

packages/angular/cli/utilities/config.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
JsonObject,
1212
JsonParseMode,
1313
experimental,
14+
isJsonObject,
1415
normalize,
1516
parseJson,
1617
parseJsonAst,
@@ -197,7 +198,7 @@ export function migrateLegacyGlobalConfig(): boolean {
197198
if (existsSync(legacyGlobalConfigPath)) {
198199
const content = readFileSync(legacyGlobalConfigPath, 'utf-8');
199200
const legacy = parseJson(content, JsonParseMode.Loose);
200-
if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
201+
if (!isJsonObject(legacy)) {
201202
return false;
202203
}
203204

@@ -208,16 +209,13 @@ export function migrateLegacyGlobalConfig(): boolean {
208209
cli['packageManager'] = legacy.packageManager;
209210
}
210211

211-
if (legacy.defaults && typeof legacy.defaults == 'object' && !Array.isArray(legacy.defaults)
212-
&& legacy.defaults.schematics && typeof legacy.defaults.schematics == 'object'
213-
&& !Array.isArray(legacy.defaults.schematics)
214-
&& typeof legacy.defaults.schematics.collection == 'string') {
212+
if (isJsonObject(legacy.defaults)
213+
&& isJsonObject(legacy.defaults.schematics)
214+
&& typeof legacy.defaults.schematics.collection == 'string') {
215215
cli['defaultCollection'] = legacy.defaults.schematics.collection;
216216
}
217217

218-
if (legacy.warnings && typeof legacy.warnings == 'object'
219-
&& !Array.isArray(legacy.warnings)) {
220-
218+
if (isJsonObject(legacy.warnings)) {
221219
const warnings: JsonObject = {};
222220
if (typeof legacy.warnings.versionMismatch == 'boolean') {
223221
warnings['versionMismatch'] = legacy.warnings.versionMismatch;
@@ -249,7 +247,7 @@ function getLegacyPackageManager(): string | null {
249247
const content = readFileSync(legacyGlobalConfigPath, 'utf-8');
250248

251249
const legacy = parseJson(content, JsonParseMode.Loose);
252-
if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
250+
if (!isJsonObject(legacy)) {
253251
return null;
254252
}
255253

@@ -278,7 +276,7 @@ export async function getSchematicDefaults(
278276
result = { ...result, ...(schematicObject as {}) };
279277
}
280278
const collectionObject = workspace.getSchematics()[collection];
281-
if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
279+
if (isJsonObject(collectionObject)) {
282280
result = { ...result, ...(collectionObject[schematic] as {}) };
283281
}
284282

@@ -293,7 +291,7 @@ export async function getSchematicDefaults(
293291
result = { ...result, ...(schematicObject as {}) };
294292
}
295293
const collectionObject = workspace.getSchematics()[collection];
296-
if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
294+
if (isJsonObject(collectionObject)) {
297295
result = { ...result, ...(collectionObject[schematic] as {}) };
298296
}
299297
}
@@ -305,7 +303,7 @@ export async function getSchematicDefaults(
305303
result = { ...result, ...(schematicObject as {}) };
306304
}
307305
const collectionObject = workspace.getProjectSchematics(project)[collection];
308-
if (typeof collectionObject == 'object' && !Array.isArray(collectionObject)) {
306+
if (isJsonObject(collectionObject)) {
309307
result = { ...result, ...(collectionObject[schematic] as {}) };
310308
}
311309
}
@@ -321,7 +319,7 @@ export async function isWarningEnabled(warning: string): Promise<boolean> {
321319
const project = getProjectByCwd(workspace);
322320
if (project && workspace.getProjectCli(project)) {
323321
const warnings = workspace.getProjectCli(project)['warnings'];
324-
if (typeof warnings == 'object' && !Array.isArray(warnings)) {
322+
if (isJsonObject(warnings)) {
325323
const value = warnings[warning];
326324
if (typeof value == 'boolean') {
327325
return value;
@@ -330,7 +328,7 @@ export async function isWarningEnabled(warning: string): Promise<boolean> {
330328
}
331329
if (workspace.getCli()) {
332330
const warnings = workspace.getCli()['warnings'];
333-
if (typeof warnings == 'object' && !Array.isArray(warnings)) {
331+
if (isJsonObject(warnings)) {
334332
const value = warnings[warning];
335333
if (typeof value == 'boolean') {
336334
return value;
@@ -342,7 +340,7 @@ export async function isWarningEnabled(warning: string): Promise<boolean> {
342340
workspace = await getWorkspace('global');
343341
if (workspace && workspace.getCli()) {
344342
const warnings = workspace.getCli()['warnings'];
345-
if (typeof warnings == 'object' && !Array.isArray(warnings)) {
343+
if (isJsonObject(warnings)) {
346344
const value = warnings[warning];
347345
if (typeof value == 'boolean') {
348346
return value;

0 commit comments

Comments
 (0)