All files / src/app index.ts

95.83% Statements 23/24
71.42% Branches 5/7
100% Functions 5/5
95.65% Lines 22/23

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 722x   2x   2x 2x 2x 2x 2x 2x 2x                 6x 6x             2x 6x     6x 6x   6x 6x       6x       2x 6x 6x 6x                                                  
import { chain, externalSchematic, Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
 
import { addBackbaseTemplates } from '../utils/backbase/package-utils';
import { Schema, NormalizedSchema } from './schema';
import { addAppModuleImports, removeUnusedFiles } from './rules/app.rules';
import { updateAngularJson } from './rules/angular-json.rules';
import { getDefaultPrefix } from '../utils/backbase/eslint';
import { addDependency } from '../migrations/utils/package-json';
import { toFileName } from '../utils/nrwl/name-utils';
import { normalize } from 'path';
import { projectAngularVersion } from '../lib-versions';
 
interface AppTemplate {
  name: string;
  selector: string;
  className?: string;
}
 
function normalizeSchema(tree: Tree, schema: Schema): NormalizedSchema {
  const prefix = getDefaultPrefix(tree);
  return {
    ...schema,
    prefix,
    projectRoot: schema.projectRoot || `apps/${schema.name}`,
  };
}
 
export const addNgLocalize = (schema: NormalizedSchema) => {
  return chain([addDependency('@angular/localize', projectAngularVersion), addNgLocalizePolyfillToApp]);
 
  function addNgLocalizePolyfillToApp(tree: Tree) {
    const appFolderName = toFileName(schema.name);
    const polyfillsPath = normalize(`apps/${appFolderName}/src/polyfills.ts`);
 
    const polyfills = tree.read(polyfillsPath)?.toString();
    Iif (!polyfills) {
      return;
    }
 
    tree.overwrite(polyfillsPath, `${polyfills}\nimport '@angular/localize/init';`);
  }
};
 
export default function (schema: Schema): Rule {
  return (tree: Tree, context: SchematicContext) => {
    const normalizedSchema = normalizeSchema(tree, schema);
    return chain([
      externalSchematic(
        '@angular-eslint/schematics',
        'application',
        {
          routing: false,
          style: 'css',
          ...normalizedSchema,
        },
        {
          interactive: false,
        },
      ),
      addBackbaseTemplates<AppTemplate & NormalizedSchema>({
        ...normalizedSchema,
        name: normalizedSchema.name,
        selector: `${normalizedSchema.prefix}-${normalizedSchema.name}`,
      }),
      addAppModuleImports(normalizedSchema),
      updateAngularJson(normalizedSchema, context),
      removeUnusedFiles(normalizedSchema),
      addNgLocalize(normalizedSchema),
    ])(tree, context);
  };
}