All files / module index.ts

96.77% Statements 30/31
75% Branches 9/12
100% Functions 6/6
96.77% Lines 30/31

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  5x 5x 5x 5x 5x 5x   5x 17x 17x 17x   17x 4x     17x 17x   17x   16x 32x     16x           16x               16x                   16x 16x     16x 16x   16x         16x   16x 16x   16x 16x      
import {Schema as ModuleOptions} from './schema';
import {apply, chain, filter, mergeWith, move, noop, Rule, schematic, template, Tree, url} from '@angular-devkit/schematics';
import {strings} from '@angular-devkit/core';
import {CodeUtils} from '../utils/code-utils';
import {buildRelativePath} from '@schematics/angular/utility/find-module';
import {parseName} from '@schematics/angular/utility/parse-name';
import {findClosestModule, getSourceRoot, smartPath} from '../utils/yang-utils';
 
export default function (options: ModuleOptions): Rule {
  return (host: Tree) => {
    const rootPath = getSourceRoot(host, options);
    smartPath(rootPath, options, '');
 
    if (!options.path) {
      options.path = `${rootPath}/shared`;
    }
 
    const parsedPath = parseName(options.path, options.name);
    options.path = parsedPath.path;
 
    options.module = findClosestModule(host, options, 'shared');
 
    const templateSource = apply(url('./files'), [
      options.routing ? noop() : filter(path => !path.endsWith('-routing.module.ts')),
      template({
        ...strings,
        'if-flat': (s: string) => options.flat ? '' : s,
        ...options
      }),
      move(options.path)
    ]);
 
    const createComp = options.component ? schematic('component', {
      project: options.project,
      name: options.name,
      path: options.path + '/' + options.name,
      routing: true,
      route: ''
    }) : noop();
 
    return chain([
      mergeWith(templateSource),
      createComp,
      addNgModule(options)
    ]);
  };
}
 
 
function addNgModule(options: ModuleOptions): (host: Tree) => Tree {
  return (host: Tree) => {
    Iif (!options.module)
      return host;
 
    const file = options.module;
    const sourceFile = CodeUtils.readSourceFile(host, file);
 
    const modulePath = `/${options.path}/`
      + (options.flat ? '' : strings.dasherize(options.name) + '/')
      + strings.dasherize(options.name)
      + '.module';
 
    const relativePath = buildRelativePath(options.module, modulePath);
 
    CodeUtils.addImport(sourceFile, `${strings.classify(options.name)}Module`, relativePath);
    CodeUtils.insertInVariableArray(sourceFile, "MODULES", `${strings.classify(options.name)}Module`);
 
    CodeUtils.writeSourceFile(host, file, sourceFile);
    return host;
  };
}