All files / component index.ts

94.55% Statements 52/55
91.3% Branches 42/46
100% Functions 7/7
94.44% Lines 51/54

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  12x 12x 12x 12x 12x 12x   12x 91x 91x 91x   91x 1x     91x 91x 91x   91x 91x 91x 91x 91x   91x 91x     91x         91x                 91x 91x     91x     91x 3x 3x     88x   88x         88x 88x   88x 88x     88x   88x 82x 79x     88x             79x       79x 79x   79x         79x         79x   79x 79x       79x         3x    
import {Schema as ComponentOptions} from './schema';
import {chain, externalSchematic, Rule, Tree} 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, getProjectSchematic, getSourceRoot, smartPath} from '../utils/yang-utils';
 
export default function (options: ComponentOptions): Rule {
  return async (host: Tree) => {
    const rootPath = getSourceRoot(host, options);
    smartPath(rootPath, options, 'components');
 
    if (!options.path) {
      options.path = `${rootPath}/shared/components`;
    }
 
    const parsedPath = parseName(options.path, options.name);
    options.path = parsedPath.path;
    options.module = findClosestModule(host, options, 'shared');
 
    const schematic = await getProjectSchematic(host, options, '@schematics/angular:component');
    options.inlineStyle = options.inlineStyle ?? schematic.inlineStyle;
    options.inlineTemplate = options.inlineTemplate ?? schematic.inlineTemplate;
    options.flat = options.flat ?? schematic.flat ?? false;
    options.skipTests = options.skipTests ?? schematic.skipTests ?? false;
 
    Eif (!options.entryComponent) {
      delete options.entryComponent;
    }
 
    const ngOptions = {
      ...options,
      skipImport: true
    };
 
    return chain([
      externalSchematic('@schematics/angular', 'component', ngOptions),
      addToNgModule(options)
    ]);
  };
}
 
 
function addToNgModule(options: ComponentOptions): (host: Tree) => Tree {
  return (host: Tree) => {
    Iif (!options.module || options.skipImport)
      return host;
 
    const file = options.module;
 
    // If features module: warn & skip !
    if (file === '/src/app/features/features.module.ts') {
      console.log(yellow('Cannot declare component inside features module: skipping import.'));
      return host;
    }
 
    const sourceFile = CodeUtils.readSourceFile(host, file);
 
    const componentPath = `/${options.path}/`
      + (options.flat ? '' : strings.dasherize(options.name) + '/')
      + strings.dasherize(options.name)
      + '.component';
 
    const relativePath = buildRelativePath(options.module, componentPath);
    CodeUtils.addImport(sourceFile, `${strings.classify(options.name)}Component`, relativePath);
 
    CodeUtils.insertInVariableArray(sourceFile, "DECLARATIONS", `${strings.classify(options.name)}Component`);
    CodeUtils.writeSourceFile(host, file, sourceFile);
 
 
    let path = options.path as string;
 
    if (path.includes('src/app/features/')) {
      if (options.routing)
        updateFeatureRouting(options, host);
    }
 
    return host;
  };
}
 
 
 
function updateFeatureRouting(options: ComponentOptions, host: Tree): void {
  Iif (!options.module)
    return;
 
  // Ajouter la route
  const file = options.module.replace('.module.ts', '-routing.module.ts');
  const sourceFile = CodeUtils.readSourceFile(host, file);
 
  Iif (options.route === undefined) {
    options.route = strings.dasherize(options.name);
  }
 
 
  const componentPath = `/${options.path}/`
    + (options.flat ? '' : strings.dasherize(options.name) + '/')
    + strings.dasherize(options.name)
    + '.component';
 
  const relativePath = buildRelativePath(file, componentPath);
 
  CodeUtils.addImport(sourceFile, `${strings.classify(options.name)}Component`, relativePath);
  CodeUtils.insertInVariableArray(sourceFile, 'ROUTES',
    `{ path: '${options.route}', component: ${strings.classify(options.name)}Component }`
  );
 
  CodeUtils.writeSourceFile(host, file, sourceFile);
}
 
 
function yellow(str: string): string {
  return `\x1b[33m${str}\x1b[0m`;
}