All files / feature index.ts

94.44% Statements 34/36
78.57% Branches 11/14
80% Functions 4/5
94.44% Lines 34/36

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  12x 12x 12x 12x 12x   12x 79x 79x   79x 2x   2x 2x     79x   79x 79x   79x     79x                 79x                         79x                   79x 79x     79x 79x   79x 77x 77x     2x 2x       79x 79x 79x   79x       79x 79x      
import {Schema as FeatureOptions} 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 {findClosestModule, getSourceRoot, YangUtils} from '../utils/yang-utils';
import {CodeUtils} from '../utils/code-utils';
import {parseName} from '@schematics/angular/utility/parse-name';
 
export default function (options: FeatureOptions): Rule {
  return (host: Tree) => {
    const rootPath = getSourceRoot(host, options);
 
    if (options.name.includes('/')) {
      let nameArgs: string[] = options.name.split('/');
 
      options.name = nameArgs.pop() as string;
      options.path = nameArgs.join('/');
    }
 
    options.path = `${rootPath}/features/${options.path || ''}/${strings.dasherize(options.name)}`;
 
    const parsedPath = parseName(options.path, options.name);
    options.path = parsedPath.path;
 
    options.module = findClosestModule(host, options);
 
 
    const templateSource = apply(url('./files'), [
      options.skipTests ? noop() : filter(path => !path.endsWith('.spec.ts')),
      template({
        ...strings,
        ...options
      }),
      move(options.path)
    ]);
 
    const createComp = options.component ? schematic('component', {
      project: options.project,
      name: options.name,
      path: options.path,
      routing: true,
      route: '',
      flat: true,
      style: options.style,
      inlineStyle: options.inlineStyle,
      inlineTemplate: options.inlineTemplate
    }) : noop();
 
 
    return chain([
      mergeWith(templateSource),
      createComp,
      updateRouting(options)
    ]);
  };
}
 
 
function updateRouting(options: FeatureOptions): (host: Tree) => Tree {
  return (host: Tree) => {
    Iif (!options.module)
      return host;
 
    let file = '';
    let varName = '';
 
    if (options.module.endsWith(YangUtils.FEATURES_MODULE_FILE)) {
      file = options.module;
      varName = 'FEATURES_ROUTES';
    }
    else {
      file = options.module.replace('.module.ts', '-routing.module.ts');
      varName = 'ROUTES';
    }
 
    // Ajouter la route
    const sourceFile = CodeUtils.readSourceFile(host, file);
    let routePath = (options.path || '').replace('/src/app/', '@app/');
    routePath = routePath.substring(routePath.indexOf('@app/'));
 
    CodeUtils.insertInVariableArray(sourceFile, varName,
      `{ path: '${strings.dasherize(options.name)}', loadChildren: () => import('${routePath}/${strings.dasherize(options.name)}.module').then(m => m.${strings.classify(options.name)}Module) }`
    );
 
    CodeUtils.writeSourceFile(host, file, sourceFile);
    return host;
  };
}