All files / pipe index.ts

100% Statements 33/33
100% Branches 24/24
100% Functions 5/5
100% Lines 32/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 63  2x 2x 2x 2x 2x 2x   2x 10x 10x 10x   10x 4x     10x 10x 10x   10x   10x 10x 10x   10x         10x                 10x 10x 1x   9x 9x   9x         9x   9x 9x   9x 9x      
import {Schema as PipeOptions} from './schema';
import {chain, externalSchematic, Rule, Tree} from '@angular-devkit/schematics';
import {strings} from '@angular-devkit/core';
import {CodeUtils} from '../utils/code-utils';
import {parseName} from '@schematics/angular/utility/parse-name';
import {buildRelativePath} from '@schematics/angular/utility/find-module';
import {findClosestModule, getProjectSchematic, getSourceRoot, smartPath} from '../utils/yang-utils';
 
export default function (options: PipeOptions): Rule {
  return async (host: Tree) => {
    const rootPath = getSourceRoot(host, options);
    smartPath(rootPath, options, 'pipes');
 
    if (!options.path) {
      options.path = `${rootPath}/shared/pipes`;
    }
 
    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:pipe');
    options.flat = options.flat ?? schematic.flat ?? true;
    options.skipTests = options.skipTests ?? schematic.skipTests ?? false;
 
    const ngOptions = {
      ...options,
      skipImport: true
    };
 
    return chain([
      externalSchematic('@schematics/angular', 'pipe', ngOptions),
      addNgModule(options)
    ]);
  };
}
 
 
function addNgModule(options: PipeOptions): (host: Tree) => Tree {
  return (host: Tree) => {
    if (options.skipImport || !options.module)
      return host;
 
    const file = options.module;
    const sourceFile = CodeUtils.readSourceFile(host, file);
 
    const pipePath = `/${options.path}/`
      + (options.flat ? '' : strings.dasherize(options.name) + '/')
      + strings.dasherize(options.name)
      + '.pipe';
 
    const relativePath = buildRelativePath(options.module, pipePath);
 
    CodeUtils.addImport(sourceFile, `${strings.classify(options.name)}Pipe`, relativePath);
    CodeUtils.insertInVariableArray(sourceFile, "DECLARATIONS", `${strings.classify(options.name)}Pipe`);
 
    CodeUtils.writeSourceFile(host, file, sourceFile);
    return host;
  };
}