All files / src/project index.ts

83.33% Statements 20/24
25% Branches 2/8
100% Functions 3/3
83.33% Lines 20/24

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 642x                     2x 2x   2x 2x 2x     2x 6x     6x       6x 6x 6x 6x 6x       6x 6x       6x                             6x         6x 6x    
import {
  chain,
  Rule,
  Tree,
  apply,
  url,
  template,
  mergeWith,
  branchAndMerge,
  SchematicContext,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask, RepositoryInitializerTask } from '@angular-devkit/schematics/tasks';
import { minVersion } from 'semver';
 
import * as strings from '../utils/angular/strings';
import { names } from '../utils/nrwl/name-utils';
import { libVersions, foundationVersion } from '../lib-versions';
import { Schema } from './schema';
 
export default function (options: Schema): Rule {
  Iif (!options.name) {
    throw new Error(`Invalid options, "name" is required.`);
  }
  Iif (!options.directory) {
    options.directory = options.name;
  }
 
  return (host: Tree, context: SchematicContext) => {
    addTasks(options, context);
    const schematicsVersion = require(`${__dirname}/../../package.json`).version;
    const parsedSchematicsVersion = minVersion(schematicsVersion);
    Iif (!parsedSchematicsVersion) {
      throw new Error('Unexpected error parsing schematics version');
    }
 
    const parsedFoundationMajorVersion = minVersion(foundationVersion);
    Iif (!parsedFoundationMajorVersion) {
      throw new Error('Unexpected error parsing foundation-ang version');
    }
 
    const templateSource = apply(url('./files'), [
      template({
        utils: strings,
        dot: '.',
        tmpl: '',
        ...libVersions,
        ...(options as object),
        ...names(options.name), // adds name, fileName, propertyName and className
        ...strings, // adds string utils like capitalize etc
        sourceDir: 'src',
        schematicsVersion,
        schematicsMajorVersion: parsedSchematicsVersion.major,
        foundationMajorVersion: parsedFoundationMajorVersion.major,
      }),
    ]);
    return chain([branchAndMerge(chain([mergeWith(templateSource)]))])(host, context);
  };
}
 
function addTasks(options: Schema, context: SchematicContext) {
  const packageTask = options.skipInstall ? undefined : context.addTask(new NodePackageInstallTask(options.directory));
  context.addTask(new RepositoryInitializerTask(options.directory), packageTask ? [packageTask] : []);
}