All files / src/migrations 4.0.0.ts

100% Statements 41/41
100% Branches 1/1
100% Functions 11/11
100% Lines 35/35

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 1451x 1x 1x   1x 1x 1x 1x   1x                                                                                                                             1x 7x 5x   2x     7x 7x 7x 7x 6x 6x   3x     7x     6x 3x 3x       7x 7x                                 7x 7x 7x 6x 6x   3x     7x     1x         1x 7x                    
import { Rule, chain, Tree } from '@angular-devkit/schematics';
import * as path from 'path';
import { dissocPath, assocPath } from 'ramda';
 
import { updateScript, updatePackageJson, updatePeerDependencyVersionsInLib } from './utils/package-json';
import { tryRule } from './utils/rules';
import { readJsonFile } from './utils/json';
import { getWorkspace, getLibProjects } from '../utils/backbase/package-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@10 @angular/core@10 @bb-cli/bb-ang@4
 
## Modify the "update" script in package.json
 
Change the update script from version ^3 to ^4 for @bb-cli/schematics:
 
  "scripts": {
    "update": "ng update @bb-cli/schematics@^4",
  }
 
## Make sure the version of @bb-cli/bb-ang is ~4.0.0
 
  $ npm install --save-dev @bb-cli/bb-ang@~4.0.0
 
## Remove ngPackage "languageLevel" configuration from all package.json files
 
Previously the supported language level for ngPackager was configured using the "ngPackage.lib" key
in the package.json. This information is now part of the Typescript configuration and must be
removed from the package.json of any libs in the project.
 
## 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: 10.0.0,
    @angular/common: 10.0.0,
    @angular/compiler: 10.0.0,
    @angular/core: 10.0.0,
    @angular/forms: 10.0.0,
    @angular/platform-browser: 10.0.0,
    @angular/platform-browser-dynamic: 10.0.0,
    @angular/router: 10.0.0,
    @backbase/foundation-ang: 6.0.0,
    @ngrx/effects: 10.0.0-beta.0,
    @ngrx/store: 10.0.0-beta.0,
    core-js: 2.6.5,
    rxjs: 6.5.5,
    zone.js: 0.10.3
 
Note: This *does not* mean that the version must be locked - the semver range should reflect the
most liberal version(s) the library supports.
 
## Update engines to support newer npm and node
 
 
    "engines": {
      "node": "^10.13.0 || ^12.16.1",
      "npm": "^6.11.0"
    }
 
`;
 
const updateUpdateScript: Rule = updateScript('update', (oldScript) => {
  if (!oldScript) {
    return 'ng update @bb-cli/schematics@^4';
  }
  return oldScript.replace('@bb-cli/schematics@^3', '@bb-cli/schematics@^4');
});
 
const removeNgPackageLanguageLevelConfigFromAllLibs: Rule = async (tree: Tree) => {
  const workspace = await getWorkspace(tree);
  const packageJsonPaths = getLibProjects(workspace).map((project) => path.join(project.root, 'package.json'));
  for (const packageJsonPath of packageJsonPaths) {
    try {
      removeNgPackageLanguageLevelConfig(tree, packageJsonPath);
    } catch (e) {
      continue;
    }
  }
  return () => tree;
 
  function removeNgPackageLanguageLevelConfig(tree: Tree, packageJsonPath: string): void {
    const packageJson = readJsonFile(tree, packageJsonPath);
    const newPackageJson = dissocPath(['ngPackage', 'lib', 'languageLevel'], packageJson);
    tree.overwrite(packageJsonPath, JSON.stringify(newPackageJson, null, 2));
  }
};
 
const updatePeerDependencyVersionsInAllLibs: Rule = async (tree: Tree) => {
  const targetPackageVersions = {
    '@angular/animations': '10.0.0',
    '@angular/common': '10.0.0',
    '@angular/compiler': '10.0.0',
    '@angular/core': '10.0.0',
    '@angular/forms': '10.0.0',
    '@angular/platform-browser': '10.0.0',
    '@angular/platform-browser-dynamic': '10.0.0',
    '@angular/router': '10.0.0',
    '@backbase/foundation-ang': '6.0.0',
    '@ngrx/effects': '10.0.0-beta.0',
    '@ngrx/store': '10.0.0-beta.0',
    'core-js': '2.6.5',
    rxjs: '6.5.5',
    'zone.js': '0.10.3',
  };
 
  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;
};
 
const updateEngines: Rule = chain([
  updatePackageJson(assocPath(['engines', 'node'], '^10.13.0 || ^12.16.1')),
  updatePackageJson(assocPath(['engines', 'npm'], '^6.11.0')),
]);
 
export default function (): Rule {
  return tryRule(
    chain([
      updateUpdateScript,
      removeNgPackageLanguageLevelConfigFromAllLibs,
      updatePeerDependencyVersionsInAllLibs,
      updateEngines,
    ]),
    manualProcess,
  );
}