All files / directive index.ts

96.97% Statements 32/33
95.83% Branches 23/24
100% Functions 5/5
96.88% Lines 31/32

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  2x 2x 2x 2x 2x 2x   2x 10x 10x 9x   9x 3x     9x 9x 9x 9x   9x 9x 9x   9x         9x                 9x 9x     9x 9x   9x         9x   9x 9x   9x 9x      
import {Schema as DirectiveOptions} 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: DirectiveOptions): Rule {
  return async (host: Tree) => {
    const rootPath = getSourceRoot(host, options);
    smartPath(rootPath, options, 'directives');
 
    if (!options.path) {
      options.path = `${rootPath}/shared/directives`;
    }
 
    const parsedPath = parseName(options.path, options.name);
    options.name = parsedPath.name;
    options.path = parsedPath.path;
    options.module = findClosestModule(host, options, 'shared');
 
    const schematic = await getProjectSchematic(host, options, '@schematics/angular:directive');
    options.flat = options.flat ?? schematic.flat ?? true;
    options.skipTests = options.skipTests ?? schematic.skipTests ?? false;
 
    const ngOptions = {
      ...options,
      skipImport: true
    };
 
    return chain([
      externalSchematic('@schematics/angular', 'directive', ngOptions),
      addNgModule(options)
    ]);
  };
}
 
 
function addNgModule(options: DirectiveOptions): (host: Tree) => Tree {
  return (host: Tree) => {
    Iif (options.skipImport || !options.module)
      return host;
 
    const file = options.module;
    const sourceFile = CodeUtils.readSourceFile(host, file);
 
    const directivePath = `/${options.path}/`
      + (options.flat ? '' : strings.dasherize(options.name) + '/')
      + strings.dasherize(options.name)
      + '.directive';
 
    const relativePath = buildRelativePath(options.module, directivePath);
 
    CodeUtils.addImport(sourceFile, `${strings.classify(options.name)}Directive`, relativePath);
    CodeUtils.insertInVariableArray(sourceFile, "DECLARATIONS", `${strings.classify(options.name)}Directive`);
 
    CodeUtils.writeSourceFile(host, file, sourceFile);
    return host;
  };
}