All files / utils yang-utils.ts

93.24% Statements 69/74
82.22% Branches 37/45
100% Functions 13/13
93.24% Lines 69/74

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  13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 13x                             13x 64x 1920x 563x 563x     1920x         13x 124x     13x 370x 366x 64x     366x 366x   366x       366x       13x 211x 210x 74x   210x 210x   210x       13x 114x 114x   114x       114x       13x 114x 114x       13x 132x 77x     55x 13x     42x 42x 42x   42x 25x 6x     25x     17x     42x       13x 206x 206x     1x         1x           13x 191x 191x 2205x     191x       13x 7x 5x        
import {Path} from '@angular-devkit/core';
import {FileEntry, forEach, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {getWorkspace} from '@schematics/angular/utility/config';
import {getWorkspace as getWorkspaceDefinition} from '@schematics/angular/utility/workspace';
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
import {findModuleFromOptions, ModuleOptions} from '@schematics/angular/utility/find-module';
 
export class YangUtils {
  static MAIN_FILE = '/src/main.ts';
  static CORE_MODULE_FILE = '/src/app/core/core.module.ts';
  static SHARED_MODULE_FILE = '/src/app/shared/shared.module.ts';
  static FEATURES_MODULE_FILE = '/src/app/features/features.module.ts';
  static ROUTING_MODULE_FILE = '/src/app/app-routing.module.ts';
}
 
export interface PathOptions {
  project?: string;
  module?: string;
  name: string;
  flat?: boolean;
  path?: string;
}
 
export interface ProjectOptions {
  project?: string;
}
 
export function forceOverwrite(host: Tree): Rule {
  return forEach((entry: FileEntry) => {
    if (host.exists(entry.path)) {
      host.overwrite(entry.path, Buffer.from(''));
      host.overwrite(entry.path, Buffer.from(entry.content));
    }
 
    return entry;
  });
}
 
 
export function getProjectName(host: Tree, options: any): string {
  return options.project ?? getWorkspace(host).defaultProject;
}
 
export function getProjectRoot(host: Tree, options: any, suffix = false): string {
  const workspace = getWorkspace(host);
  if (!options.project) {
    options.project = workspace.defaultProject;
  }
 
  const project = workspace.projects[options.project as string];
  let projectRoot = project.root ?? '';
 
  Iif (suffix && projectRoot) {
    projectRoot += '/';
  }
 
  return projectRoot;
}
 
 
export function getSourceRoot(host: Tree, options: PathOptions): string {
  const workspace = getWorkspace(host);
  if (!options.project) {
    options.project = workspace.defaultProject;
  }
  const project = workspace.projects[options.project as string];
  const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
 
  return `/${project.root}/src/${projectDirName}`;
}
 
 
export async function getProjectSchematics(host: Tree, options: ProjectOptions): Promise<any> {
  const workspace = await getWorkspaceDefinition(host);
  const project = workspace.projects.get(options.project as string);
 
  Iif (!project) {
    return;
  }
 
  return project.extensions.schematics ?? {};
}
 
 
export async function getProjectSchematic(host: Tree, options: ProjectOptions, schematicName: string): Promise<any> {
  const schematics = await getProjectSchematics(host, options);
  return schematics[schematicName] ?? {};
}
 
 
export function smartPath(rootPath: string, options: PathOptions, sharedSubFolder: string): void {
  if (options.path) {
    return;
  }
 
  if (!options.name.includes('/')) {
    return;
  }
 
  const nameArgs: string[] = options.name.split('/');
  const classifier: string = nameArgs.shift() as string;
  options.name = nameArgs.pop() as string;
 
  if ('shared' === classifier) {
    if (sharedSubFolder && nameArgs.length === 0) {
      nameArgs.unshift(sharedSubFolder);
    }
 
    nameArgs.unshift('shared');
  }
  else {
    nameArgs.unshift('features', classifier);
  }
 
  options.path = `${rootPath}/${nameArgs.join('/')}`;
}
 
 
export function findClosestModule(host: Tree, options: ModuleOptions, defaultModule?: string): Path | undefined {
  try {
    return findModuleFromOptions(host, options);
  }
  catch (err) {
    Iif (!options.module) {
      const opt = {...options, module: defaultModule};
      return findModuleFromOptions(host, opt);
    }
    else {
      throw err;
    }
  }
}
 
 
export function sortByKey(unsorted: any): any {
  const ordered: any = {};
  Object.keys(unsorted).sort().forEach((key) => {
    ordered[key] = unsorted[key];
  });
 
  return ordered;
}
 
 
export function installDeps(skipInstall?: boolean): (host: Tree, context: SchematicContext) => void {
  return (host: Tree, context: SchematicContext) => {
    Iif (!skipInstall)
      context.addTask(new NodePackageInstallTask());
  };
}