All files / src/utils/backbase package-utils.ts

70.1% Statements 68/97
41.66% Branches 10/24
72.72% Functions 24/33
70.96% Lines 66/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 19922x 22x 22x                   22x 22x 22x                         22x 9x     22x 6x     22x 29x       29x 29x       29x     22x 26x 26x     26x     22x 12x 12x               12x 12x     22x                     3x   3x         3x     22x             22x 129x 128x     128x     22x 39x                 22x 18x 9x       22x 9x 9x 9x 9x   22x       15x 28x 18x     22x 6x 17x 17x   22x 30x     22x 6x 6x     6x     22x 6x 6x     22x                       22x       22x                       22x   22x       22x          
import { toFileName, names } from '../nrwl/name-utils';
import * as ngWorkspaceUtilities from '../angular/workspaces';
import {
  Tree,
  Rule,
  apply,
  template,
  url,
  mergeWith,
  MergeStrategy,
  SchematicsException,
} from '@angular-devkit/schematics';
import { join, normalize, workspaces } from '@angular-devkit/core';
import * as strings from '../angular/strings';
import { getDefaultPrefix } from './eslint';
 
export interface AppContainerProject extends workspaces.ProjectDefinition {
  sourceRoot: string;
}
 
export interface WorkspaceDefinition extends workspaces.WorkspaceDefinition {
  __BB__WORKSPACE_DEFINITION: never;
  extensions: workspaces.WorkspaceDefinition['extensions'] & {
    defaultProject?: string;
  };
}
 
export function getAppPath(appName: string) {
  return `apps/${toFileName(appName)}/src`;
}
 
export function getLibPath(libName: string) {
  return `libs/${toFileName(libName)}`;
}
 
export function readFile(host: Tree, path: string): string | undefined {
  Iif (!host.exists(path)) {
    return undefined;
  }
 
  const source = host.read(path);
  Iif (!source) {
    return undefined;
  }
 
  return source.toString('utf-8');
}
 
export function readFileOrThrow(host: Tree, path: string): string {
  const result = readFile(host, path);
  Iif (result === undefined) {
    throw new Error(`Couldn't read file ${path}`);
  }
  return result;
}
 
export function addBackbaseTemplates<T>(options: T): Rule {
  const name = options['name'];
  const rule = template({
    ...names(name), // adds name, fileName, propertyName and className
    ...strings, // adds string utils like capitalize etc
    dot: '.',
    tmpl: '',
    sourceDir: 'src',
    ...(options as Object),
  });
  const source = apply(url('./files'), [rule]);
  return mergeWith(source, MergeStrategy.Overwrite);
}
 
export function addProjectToWorkspace(
  workspace: WorkspaceDefinition,
  name: string,
  project: {
    root: string;
    sourceRoot?: string;
    prefix?: string;
    targets?: Record<string, workspaces.TargetDefinition | undefined>;
    [key: string]: unknown;
  },
): Rule {
  workspace.projects.add({ name, ...project });
 
  Iif (!workspace.extensions.defaultProject && Object.keys(workspace.projects).length === 1) {
    // Make the new project the default one.
    workspace.extensions.defaultProject = name;
  }
 
  return updateWorkspace(workspace);
}
 
export function getWorkspacePath(host: Tree): string {
  const possibleFiles = ['/angular.json', '/.angular.json'];
  const path = possibleFiles.filter((p) => host.exists(p))[0];
 
  return path;
}
 
export async function getWorkspace(host: Tree): Promise<WorkspaceDefinition> {
  const ws = await ngWorkspaceUtilities.getWorkspace(host);
  Iif (ws.extensions.defaultProject !== undefined && typeof ws.extensions.defaultProject !== 'string') {
    throw new SchematicsException('defaultProject must be a string');
  }
  return ws as unknown as WorkspaceDefinition;
}
 
export function updateWorkspace(workspace: WorkspaceDefinition): Rule {
  return ngWorkspaceUtilities.updateWorkspace(workspace);
}
 
type AppFilter = (project: workspaces.ProjectDefinition) => boolean;
 
type AppType = {
  [key in 'app' | 'e2eApps' | 'allApps']: AppFilter;
};
 
export const appType: AppType = {
  app: (project) => project.targets.get('serve') !== undefined,
  e2eApps: (project) => project.targets.get('e2e') !== undefined,
  allApps: () => true,
};
 
export function getApps(workspace: workspaces.WorkspaceDefinition, filter: AppFilter = appType.app): Array<string> {
  return Array.from(workspace.projects.entries())
    .filter(([, project]) => project.extensions.projectType === 'application')
    .filter(([, project]) => filter(project))
    .map(([name]) => name);
}
export function getAppProjects(
  workspace: workspaces.WorkspaceDefinition,
  filter: AppFilter = appType.app,
): Array<workspaces.ProjectDefinition> {
  return Array.from(workspace.projects.values())
    .filter((project) => project.extensions.projectType === 'application')
    .filter((project) => filter(project));
}
 
export function getLibs(workspace: workspaces.WorkspaceDefinition): Array<string> {
  return Array.from(workspace.projects.entries())
    .filter(([, project]) => project.extensions.projectType === 'library')
    .map(([name]) => name);
}
export function getLibProjects(workspace: workspaces.WorkspaceDefinition): Array<workspaces.ProjectDefinition> {
  return Array.from(workspace.projects.values()).filter((project) => project.extensions.projectType === 'library');
}
 
export async function projectPrefix(host: Tree, projectName?: string): Promise<string> {
  const workspace = await getWorkspace(host);
  const addToProject: workspaces.ProjectDefinition | undefined = projectName
    ? workspace.projects.get(projectName)
    : undefined;
  return (addToProject && addToProject.prefix) || getDefaultPrefix(host);
}
 
export function nameWithSuffix(name: string, suffix = '-ang'): string {
  const nameSuffix = name.endsWith(suffix) ? '' : suffix;
  return `${name}${nameSuffix}`;
}
 
export async function getAppModuleFilePath(host: Tree, projectName: string): Promise<string> {
  const project = await getAppContainerProjectOrThrow(host, projectName);
 
  const appModuleFilePath = join(normalize(project.sourceRoot), 'app', 'app.module.ts');
  const existingAppModule = host.exists(appModuleFilePath);
  Iif (!existingAppModule) {
    throw new SchematicsException(`App module not found at ${appModuleFilePath}`);
  }
 
  return appModuleFilePath;
}
 
export function isAppContainerProject(project: workspaces.ProjectDefinition): project is AppContainerProject {
  return project.sourceRoot !== undefined;
}
 
export async function getAppContainerProjectOrThrow(host: Tree, projectName: string): Promise<AppContainerProject> {
  const workspace = await getWorkspace(host);
  const project = workspace.projects.get(projectName);
  Iif (!project) {
    throw new SchematicsException(`Project missing in angular.json: ${projectName}`);
  }
  Iif (!isAppContainerProject(project)) {
    throw new SchematicsException('Project missing required "sourceRoot" property in angular.json');
  }
  return project;
}
 
export const iconFile = (project: AppContainerProject) => join(normalize(project.sourceRoot), 'app', 'icon.png');
 
export function hasIcon(host: Tree, project: AppContainerProject): boolean {
  return host.exists(iconFile(project));
}
 
export function loadModelXml(host: Tree, project: AppContainerProject): string | undefined {
  const modelPath = join(normalize(project.sourceRoot), 'model.xml');
  const modelXml = host.read(modelPath);
  return modelXml ? modelXml.toString('utf-8') : undefined;
}