All files / src/migrations 5.3.3.ts

96.29% Statements 26/27
70% Branches 7/10
100% Functions 6/6
96.15% Lines 25/26

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 721x 1x 1x   1x                     1x                   1x 2x     2x   2x     2x             2x   2x     2x 1x       2x   2x   2x 2x   3x   2x 1x 1x 1x 1x   1x          
import { Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
import { IndentationText, Project, QuoteKind, SourceFile, SyntaxKind } from 'ts-morph';
import { tryRule } from './utils/rules';
 
const manualProcess = `## This migrates sets only libs/*/public_api.ts to the default scope path.
 
to apply this changes apply the following steps:
 
#### Update project \`karma.conf.js\` to include the following change
 
resolve the project url from  \`roots\`, instead of \`context\`:
\`
+    ./karma.conf.js
\`
`;
const additionalStatement = `
    function resolveContextPath(config) {
      const context = config.buildWebpack.webpackConfig.context;
      let urlList = '';
      if (Array.isArray(config.buildWebpack.webpackConfig.resolve.roots)) {
        urlList = config.buildWebpack.webpackConfig.resolve.roots.find(value => value.indexOf(context) !== -1);
      }
      return urlList ? urlList : context;
    }`;
 
export default function (): Rule {
  return tryRule((tree: Tree) => updatetsConfigCompilerOptions(tree), manualProcess);
 
  async function updatetsConfigCompilerOptions(tree: Tree) {
    const karmaConfigPath = './karma.conf.js';
 
    Iif (!tree.exists(karmaConfigPath)) {
      return;
    }
    const astProject = new Project({
      manipulationSettings: {
        indentationText: IndentationText.TwoSpaces,
        quoteKind: QuoteKind.Single,
      },
    });
 
    const buffer = tree.read(karmaConfigPath);
 
    const karmaFile = astProject.createSourceFile(karmaConfigPath, buffer?.toString('utf-8') || '', {
      overwrite: true,
    });
    replaceBasePath(karmaFile);
    tree.overwrite(karmaConfigPath, karmaFile.getFullText());
  }
 
  function replaceBasePath(source: SourceFile) {
    const mainFunction = source
      .getDescendantsOfKind(SyntaxKind.ExpressionStatement)
      .find((child) => child.getText().includes('module.exports'));
 
    if (mainFunction) {
      const variableInQuestion = mainFunction
        .getDescendantsOfKind(SyntaxKind.VariableDeclaration)
        .find((variable) => variable.getText().includes('config.buildWebpack.webpackConfig.context'));
 
      if (variableInQuestion) {
        const variableInQuestionName = variableInQuestion.getName();
        variableInQuestion.replaceWithText(`${variableInQuestionName} = resolveContextPath(config)`);
        source.addStatements(additionalStatement);
        source.formatText();
      } else {
        throw new SchematicsException('The variable context is not accessed from the webpack configuration');
      }
    }
  }
}