json.ts (1187B)
1 /** 2 * @license 3 * Copyright 2022 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 import {SchematicsException, type Tree} from '@angular-devkit/schematics'; 8 9 import type {AngularJson, AngularProject} from './types.js'; 10 11 export function getJsonFileAsObject( 12 tree: Tree, 13 path: string, 14 ): Record<string, unknown> { 15 try { 16 const buffer = tree.read(path) as Buffer; 17 const content = buffer.toString(); 18 return JSON.parse(content); 19 } catch { 20 throw new SchematicsException(`Unable to retrieve file at ${path}.`); 21 } 22 } 23 24 export function getObjectAsJson(object: Record<string, unknown>): string { 25 return JSON.stringify(object, null, 2); 26 } 27 28 export function getAngularConfig(tree: Tree): AngularJson { 29 return getJsonFileAsObject(tree, './angular.json') as unknown as AngularJson; 30 } 31 32 export function getApplicationProjects( 33 tree: Tree, 34 ): Record<string, AngularProject> { 35 const {projects} = getAngularConfig(tree); 36 37 const applications: Record<string, AngularProject> = {}; 38 for (const key in projects) { 39 const project = projects[key]!; 40 if (project.projectType === 'application') { 41 applications[key] = project; 42 } 43 } 44 return applications; 45 }