All files / src/app/rules angular-json.rules.ts

81.25% Statements 52/64
72.88% Branches 43/59
67.85% Functions 19/28
80.7% Lines 46/57

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 118 119 120 121 122 123 124 125 1262x 2x   2x 2x       2x 6x                     6x 6x 6x 6x 6x 6x             6x 6x 6x 6x 6x               6x             6x 6x 6x 6x                     6x 6x 6x 6x             6x 6x 6x 6x 6x 6x           2x                 6x 6x 6x 6x 6x               6x           2x 6x 6x 6x   6x 6x 6x          
import { isJsonArray } from '@angular-devkit/core';
import { chain, Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
 
import { getWorkspace, updateWorkspace } from '../../utils/backbase/package-utils';
import { hasMinimumPackageVersion } from '../../utils/backbase/versions';
 
import { NormalizedSchema } from '../schema';
 
export function updateAngularJson(schema: NormalizedSchema, context: SchematicContext): Rule {
  return chain([
    addStaticAssetsSection(schema),
    addBackbaseBuilder(schema, context),
    addSharedEnvironmentConfiguration(schema),
    amendStylingConfiguration(schema),
    amendTestingConfiguration(schema),
    updateBudget(schema, '5mb'),
  ]);
}
 
function addStaticAssetsSection(schema: NormalizedSchema): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    const buildTarget = workspace.projects.get(schema.name)?.targets.get('build');
    if (buildTarget && buildTarget.options && buildTarget.options.assets && isJsonArray(buildTarget.options.assets)) {
      buildTarget.options.assets.unshift({ input: 'dist/custom-project-assets', glob: '**/*', output: 'assets' });
      return updateWorkspace(workspace);
    }
    return () => {};
  };
}
 
function addSharedEnvironmentConfiguration(schema: NormalizedSchema): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    const buildConfigurations = workspace.projects.get(schema.name)?.targets.get('build')?.configurations;
    if (buildConfigurations) {
      buildConfigurations.shared = {
        fileReplacements: [
          {
            replace: `apps/${schema.name}/src/environments/environment.ts`,
            with: `apps/${schema.name}/src/environments/environment.shared.ts`,
          },
        ],
      };
      return updateWorkspace(workspace);
    }
    return () => {};
  };
}
 
function addBackbaseBuilder(schema: NormalizedSchema, context: SchematicContext): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    const minimumBbAngVersion = '3.8.0';
    Iif (!hasMinimumPackageVersion('@bb-cli/bb-ang', minimumBbAngVersion, true)) {
      context.logger.error(`## Important Note
      
It looks like that the current project is generated with an older version of @bb-cli/schematics.
This resulted in having an older version of @bb-cli/bb-ang as a devDependency,
which does not support backbase builder.
 
Please update your global backbase-schematics to the latest version
and local @bb-cli/bb-ang to at least version ${minimumBbAngVersion}.\n\n`);
    }
 
    const buildTarget = workspace.projects.get(schema.name)?.targets.get('build');
    if (buildTarget) {
      buildTarget.builder = '@bb-cli/bb-ang:browser';
      return updateWorkspace(workspace);
    }
    return () => {};
  };
}
 
function amendStylingConfiguration(options: NormalizedSchema): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    const buildOptions = workspace.projects.get(options.name)?.targets.get('build')?.options;
    if (buildOptions) {
      buildOptions.styles = [];
      return updateWorkspace(workspace);
    }
    return () => {};
  };
}
 
export function removeExistingE2EConfiguration(options: NormalizedSchema): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    workspace.projects.get(options.name)?.targets.delete('e2e');
    return updateWorkspace(workspace);
  };
}
 
function amendTestingConfiguration(options: NormalizedSchema): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    const testTarget = workspace.projects.get(options.name)?.targets.get('test');
    if (testTarget) {
      testTarget.options = {
        main: `apps/${options.name}/src/test.ts`,
        karmaConfig: './karma.conf.js',
        polyfills: `apps/${options.name}/src/polyfills.ts`,
        tsConfig: `apps/${options.name}/src/../../../tsconfig.spec.json`,
        codeCoverage: true,
        codeCoverageExclude: ['test.ts', '**/polyfills.ts'],
      };
      return updateWorkspace(workspace);
    }
    return () => {};
  };
}
 
export function updateBudget(options: NormalizedSchema, budget: string): Rule {
  return async (tree: Tree) => {
    const workspace = await getWorkspace(tree);
    const configurations = workspace.projects.get(options.name)?.targets.get('build')?.configurations;
 
    if (configurations?.production?.budgets) {
      configurations.production.budgets[0].maximumError = budget;
      return updateWorkspace(workspace);
    }
    return () => {};
  };
}