All files / src/migrations 5.0.0.ts

92.72% Statements 51/55
69.56% Branches 16/23
100% Functions 12/12
95.83% Lines 46/48

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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 1521x 1x 1x   1x 1x 1x 1x 1x   1x                                                                                                       1x 5x 3x   2x     5x 5x 5x 1x 1x 1x 1x 1x             1x 1x 94x 94x   1x 1x         94x   94x         93x   1x       8x 1x       1x       1x     5x 5x                         5x 5x 5x 2x 2x   1x     5x     1x 5x          
import { Rule, chain, Tree } from '@angular-devkit/schematics';
import * as path from 'path';
import * as ts from 'typescript';
 
import { updateScript, updatePeerDependencyVersionsInLib } from './utils/package-json';
import { tryRule } from './utils/rules';
import { getWorkspace, getLibProjects, appType, getAppProjects } from '../utils/backbase/package-utils';
import { Change, InsertChange } from '../utils/angular/change';
import { insert } from '../utils/nrwl/ast-utils';
 
const manualProcess = `
## Ensure dependencies are up to date
 
Update all important dependencies using \`ng update\`. This will allow any these packages to
perform any migrations that they deem necessary.
 
    ng update @angular/cli@11 @angular/core@11 @bb-cli/bb-ang@5
 
## Modify the "update" script in package.json
 
Change the update script from version ^4 to ^5 for @bb-cli/schematics:
 
  "scripts": {
    "update": "ng update @bb-cli/schematics@^5",
  }
 
## Make sure the version of @bb-cli/bb-ang is ~5.0.0
 
  $ npm install --save-dev @bb-cli/bb-ang@~5.0.0
 
## Update peer dependency versions in all public libs
 
The peerDependencies of all libs in a project should be aligned with the package versions actually
installed in the project. This means that development and tests can be performed against compatible
versions of the target dependencies.
 
Any peerDependencies in package.json files inside libs packages should be updated to be compatible
with the following versions:
 
    @angular/animations: 11.1.0,
    @angular/common: 11.1.0,
    @angular/compiler: 11.1.0,
    @angular/core: 11.1.0,
    @angular/forms: 11.1.0,
    @angular/platform-browser: 11.1.0,
    @angular/platform-browser-dynamic: 11.1.0,
    @angular/router: 11.1.0,
    @backbase/foundation-ang: 6.0.0,
    rxjs: 6.6.2,
 
Note: This *does not* mean that the version must be locked - the semver range should reflect the
most liberal version(s) the library supports.
 
## Add support for native async/await to protractor config
 
To allow async/await in e2e tests the protractor promise manager needs to be disabled by adding
 
    SELENIUM_PROMISE_MANAGER: false
 
to the 'protractor.conf.js' for each 'e2e' project.
`;
 
const updateUpdateScript: Rule = updateScript('update', (oldScript) => {
  if (!oldScript) {
    return 'ng update @bb-cli/schematics@^5';
  }
  return oldScript.replace('@bb-cli/schematics@^4', '@bb-cli/schematics@^5');
});
 
const updateProtractorConfig = async (tree: Tree) => {
  const workspace = await getWorkspace(tree);
  getAppProjects(workspace, appType.e2eApps).forEach((project) => {
    const protractorConfig = project.targets.get('e2e')?.options?.protractorConfig;
    Iif (!protractorConfig || typeof protractorConfig !== 'string') return;
    const content = tree.read(protractorConfig);
    Iif (!content) return;
    const sourceFile = ts.createSourceFile(
      'protractor-config.js',
      content.toString('utf-8'),
      ts.ScriptTarget.ES2020,
      true,
    );
 
    const changes: Change[] = [];
    const visitor = (node: ts.Node) => {
      changes.push(...disableSeleniumPromiseManager(node, protractorConfig));
      ts.forEachChild(node, visitor);
    };
    ts.forEachChild(sourceFile, visitor);
    insert(tree, protractorConfig, changes);
  });
};
 
function disableSeleniumPromiseManager(node: ts.Node, protractorConfig: string): Array<Change> {
  const changes: Array<Change> = [];
 
  if (
    !ts.isBinaryExpression(node) ||
    !ts.isPropertyAccessExpression(node.left) ||
    node.left.getText() !== 'exports.config'
  ) {
    return changes;
  }
  Iif (!ts.isObjectLiteralExpression(node.right)) {
    return changes;
  }
 
  const existingProp = node.right.properties.find((prop) => prop.name?.getText() === 'SELENIUM_PROMISE_MANAGER');
  Iif (existingProp) {
    return changes;
  }
 
  changes.push(
    new InsertChange(protractorConfig, node.right.properties[0].getFullStart(), '\n  SELENIUM_PROMISE_MANAGER: false,'),
  );
 
  return changes;
}
 
const updatePeerDependencyVersionsInAllLibs: Rule = async (tree: Tree) => {
  const targetPackageVersions = {
    '@angular/animations': '11.1.0',
    '@angular/common': '11.1.0',
    '@angular/compiler': '11.1.0',
    '@angular/core': '11.1.0',
    '@angular/forms': '11.1.0',
    '@angular/platform-browser': '11.1.0',
    '@angular/platform-browser-dynamic': '11.1.0',
    '@angular/router': '11.1.0',
    '@backbase/foundation-ang': '6.0.0',
    rxjs: '6.6.2',
  };
 
  const workspace = await getWorkspace(tree);
  const packageJsonPaths = getLibProjects(workspace).map((project) => path.join(project.root, 'package.json'));
  for (const packageJsonPath of packageJsonPaths) {
    try {
      updatePeerDependencyVersionsInLib(tree, packageJsonPath, targetPackageVersions);
    } catch (e) {
      continue;
    }
  }
  return () => tree;
};
 
export default function (): Rule {
  return tryRule(
    chain([updateUpdateScript, updatePeerDependencyVersionsInAllLibs, updateProtractorConfig]),
    manualProcess,
  );
}